Bar Chart in Spfx using PnpReusable React controls

This blog post explains how to create a Bar Chart in Spfx using PnpReusable React controls

Open a command prompt. Create a directory for SPFx solution.
md spfx-React-PnpBarChart
Navigate to the above created directory.
cd spfx-React-PnpBarChart
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution name
Hit Enter for a default name (spfx-pnp-DropDown in this case), or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
Location of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice: N (install on each site explicitly)
Permissions to access web APIs
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose web part option.
Selected choice: WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice: BarChart
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
npm shrinkwrap
In the command prompt, type rhe below command to open the solution in the code editor of your choice.
NPM Packages Used:
On the command prompt, run below command.
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
for Pollyfills:
npm install --save @pnp/polyfill-ie11
Reusable React controls:
npm install @pnp/spfx-controls-react --save --save-exact
Once the package is installed, you will have to configure the resource file of the property controls to be used in your project.
You can do this by opening the config/config.json and adding the following line to the localizedResources property:
"ControlStrings": "node_modules/@pnp/spfx-controls-react/lib/loc/{locale}.js"
In BarChart.tsx
  1. import { ChartControl, ChartType } from '@pnp/spfx-controls-react/lib/ChartControl';
  2. export default class BarChart extends React.Component<IBarChartProps, {}> {
  3. public render(): React.ReactElement<IBarChartProps> {
  4. return (
  5. <div>
  6. <ChartControl
  7. type={ChartType.Bar}
  8. data={data}
  9. options={options}
  10. /></div>
  11. );
  12. }
  13. }
  14. // set the data
  15. const data: Chart.ChartData = {
  16. labels:
  17. [
  18. 'January', 'February', 'March', 'April', 'May', 'June', 'July'
  19. ],
  20. datasets:
  21. [{
  22. label: 'Total Number of Tickets Resolved Per Cateogry',
  23. data:
  24. [
  25. 65, 59, 80, 81, 56, 55, 40
  26. ],
  27. borderWidth: 1
  28. }]
  29. };
  30. // set the options
  31. const options: Chart.ChartOptions = {
  32. scales:
  33. {
  34. yAxes:
  35. [
  36. {
  37. ticks:
  38. {
  39. beginAtZero: true
  40. }
  41. }
  42. ]
  43. }
  44. };
Sample OutPut
This control makes it easy to integrate Chart.js charts into your web parts. It offers most of the functionality available with Chart.js.
The control automatically renders responsive charts, uses the environment's theme colors, and renders a hidden table for users with impaired vision.
The majority of Chart.js options like data, options, type, and plugins will work the same way as is -- except for the use TypeScript syntax.
To find sample code that you can use, visit the Chart.Js documentation.
Specifying Data
The data property typically consist of:
labels: (Optional) An array of strings providing the data labels (e.g.: ['January', 'February', 'March', 'April', 'May', 'June', 'July'])
datasets: At least one dataset, which contains:
label: (Optional) A label for the data (e.g.: 'My First Dataset')
data: An array of numbers (e.g.: [65, 59, 80, 81, 56, 55, 40])
The ChartControl makes it easy to retrieve data asynchronously with the datapromise property.
You can provide full React controls within the loadingtemplate.

Theme Color Support
By default, the ChartControl will attempt to use the environment theme colors and fonts for elements such as the chart background color, grid lines, titles, labels, legends, and tooltips. This includes support for dark themes and high contrast themes.
If you wish, you can disable the use of themes by setting the useTheme property to false. This will display the standard Chart.js colors and fonts.
The ChartControl will automatically expand to fit its container. If you wish to control the size of the chart, set its parent container size, or use the className property to pass your own CSS class and override the dimensions within that class.
As long as you provide labels for all your data elements, the ChartControl will render a hidden table. Users who are visually impaired and use a screen reader will hear a description of the data in the chart.
You can improve the accessible table by adding an alternateText, a caption and a summary. If you do not provide a caption, the control will attempt to use the chart's title.
In this post, we learned how to create Bart chart in Spfx using PnpReusuable Controls 
I have attached the src file to explain creating new charts for: 
  • BarChart
  • StackChart
  • Piechart
  • Linechart
  • CurvedAreachart
For more detailed documentation, Visit
PFA, Happy Coding :)