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!!!

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

  1. 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

  2. HI Ravi, great to see all your solutions for every problem.

    Can we over ride and redirect utl from new subsite creation(which will redirect to _layouts/15/newsbweb.aspx) to any hard coded sharepoint url(sites/sitecollectionname/) ?

    Goal is : I want to override New subsite creation button(which will be avilable in site content page) url to my custom site provisioning creation page to avoid creation of all default templates.

    Can you please suggest me give me solution for this using SPFX?

    Like

Comments