SharePoint list data displayed using PnP Chart in the SharePoint Framework (SPFx) webpart

This article provide steps to displaying SharePoint list data using PnP Chart in the SharePoint Framework (SPFx), PnP chart controles makes easy to integrate Chart.js charts into web part.

Create a new web part project

Open power shell and run following comment to create a new web part by running the Yeoman SharePoint Generator

yo @microsoft/sharepoint

When prompted:

Enter the webpart name as your solution name, and then select Enter.
Select Create a subfolder with solution name for where to place the files.
Select Y to allow the solution to be deployed to all sites immediately.
Select N on the question if solution contains unique permissions.
Select WebPart as the client-side component type to be created.

The next set of prompts ask for specific information about your web part:

Enter your web part name, and then select Enter.
Enter your web part description, and then select Enter.
Select React framework as the framework you would like to use, and then select Enter.

Start Visual Studio Code (or your favorite code editor) within the context of the newly created project folder.

cd .\spfx-pnp-carousel\
code .

Install the library and required dependencies

npm install @pnp/sp --save
npm install @pnp/spfx-controls-react --save --save-exact

you need to update the render method of the client-side web part to create a properly configured instance of the React component for rendering. The following code shows the updated method definition.

  public render(): void {
    const element: React.ReactElement<ISpfxPnpChartProps> = React.createElement(
      SpfxPnpChart,
      {
        description: this.properties.description,
        context:this.context
      }
    );

    ReactDom.render(element, this.domElement);
  }

Update the SpfxPnpChart.tsx file. First, add some import statements to import the types you defined earlier. Notice the import for the Office UI Fabric components used to render the UI of the React component and pnp sp imports.

import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'SpfxPnpChartWebPartStrings';
import SpfxPnpChart from './components/SpfxPnpChart';
import { ISpfxPnpChartProps } from './components/ISpfxPnpChartProps';

Replace this render function with the following code.

protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }

Add a constructor, as shown in the following example.

  constructor(props: ISpfxPnpChartProps) {
    super(props);
    sp.setup({
      spfxContext: this.props.context
    });
  }

place the below code after the react component code, these functions using PnPjs to get chart data from the SharePoint list

 @autobind
  private async _loadAsyncData(): Promise<Chart.ChartData> {
    const items: any[] = await sp.web.lists.getByTitle("Sales").items.select("Title", "Sales").get();
    let lblarr: string[] = [];
    let dataarr: number[] = [];
    items.forEach(element => {
      lblarr.push(element.Title);
      dataarr.push(element.Sales);
    });
    let chartdata: Chart.ChartData = {
      labels: lblarr,
      datasets: [{
        label: 'My Sales',
        data: dataarr
      }]
    };
    return chartdata;
  }

Deploy the solution

You’re now ready to build, bundle, package, and deploy the solution.

Run the gulp commands to verify that the solution builds correctly.

gulp build

Use the following command to bundle and package the solution.

gulp bundle --ship
gulp package-solution --ship

Browse to the app catalog of your target tenant and upload the solution package. You can find the solution package under the sharepoint/solution folder of your solution. It is the .sppkg file. After you upload the solution package in the app catalog. you can find and the web part anywhere across the tenant.

Sharing is caring!

If you have any questions, feel free to let me know in the comments section.
Happy coding!!!

10 thoughts on “SharePoint list data displayed using PnP Chart in the SharePoint Framework (SPFx) webpart

  1. Mani August 19, 2020 / 3:43 am

    @ auto bind where to keep in .tsx file or . ts file?

    Like

  2. Mani August 19, 2020 / 3:48 am

    sp.setup({ also giving error like SP not able to find, Please let us know i am new to this

    Like

  3. Mani August 19, 2020 / 4:57 am

    Hi Ravichandran,

    Can i download that SPFX pnp Chart folder , i am not getting any option?

    Like

  4. Mani August 19, 2020 / 5:02 am

    Yes Ravichandran, Its downloading all the solutions but i want only the SPFX pnp Chart solution

    Like

  5. Mani August 19, 2020 / 7:16 am

    k Thank you RaviChandran , Can you please let me know Pros and Cons of Pnp Charts in Sharepoint?
    Actually requirement is we have one sharepoint list which consists of more than 80,000 items and we have items like from 2017 , i want to show year wise count files , how can i achive that one?

    and can we do filteration and redirection in Pnp charts?

    Liked by 1 person

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s