Open a command prompt. Create a directory for SPFx solution.
md spfx-React-HighChartDrilldown
Navigate to the above created directory.
cd spfx-React-HighChartDrilldown
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have default name (spfx-React-HighChartDrilldown 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)
Place 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: Highchart
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 below command to open the solution in the code editor of your choice.
code .
NPM Packages Used,
On the command prompt, run below command.
npm install highcharts highcharts-react-official
in Highchart.tsx
- import * as React from 'react';
- import * as ReactDom from 'react-dom';
- import { IHighchartProps } from './IHighchartProps';
- import drilldown from 'highcharts/modules/drilldown';
- import * as Highcharts from 'highcharts';
- import HighchartsReact from 'highcharts-react-official';
- drilldown(Highcharts);
-
- export default class Highchart extends React.Component<IHighchartProps, {}> {
- private _myhighchart: HTMLElement = undefined;
- public render(): React.ReactElement<IHighchartProps> {
- return (
- <div id="mycontainer">
- <HighchartsReact constructorType={'chart'} highcharts={Highcharts} options={options} />
- </div>
- );
- }
- }
- const options: Highcharts.Options = {
- chart: {
- type: 'pie'
- },
- title: {
- text: 'My chart'
- },
- plotOptions: {
- series: {
- dataLabels: {
- enabled: true,
- format: '{point.name}: {point.y:.1f}%'
- }
- }
- },
-
- tooltip: {
- headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
- pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
- },
-
- series: [
- {
- name: "Browsers",
- type: "pie",
- data: [
- {
- name: "Chrome",
- y: 62.74,
- drilldown: "Chrome"
- },
- {
- name: "Firefox",
- y: 10.57,
- drilldown: "Firefox"
- },
- {
- name: "Internet Explorer",
- y: 7.23,
- drilldown: "Internet Explorer"
- },
- {
- name: "Safari",
- y: 5.58,
- drilldown: "Safari"
- },
- {
- name: "Edge",
- y: 4.02,
- drilldown: "Edge"
- },
- {
- name: "Opera",
- y: 1.92,
- drilldown: "Opera"
- },
- {
- name: "Other",
- y: 7.62,
- drilldown: null
- }
- ]
- }
- ],
- drilldown: {
- series: [
- {
- name: "Chrome",
- id: "Chrome",
- data: [
- [
- "v65.0",
- 0.1
- ],
- [
- "v64.0",
- 1.3
- ],
- [
- "v63.0",
- 53.02
- ],
- [
- "v62.0",
- 1.4
- ],
- [
- "v61.0",
- 0.88
- ],
- [
- "v60.0",
- 0.56
- ],
- [
- "v59.0",
- 0.45
- ],
- [
- "v58.0",
- 0.49
- ],
- [
- "v57.0",
- 0.32
- ],
- [
- "v56.0",
- 0.29
- ],
- [
- "v55.0",
- 0.79
- ],
- [
- "v54.0",
- 0.18
- ],
- [
- "v51.0",
- 0.13
- ],
- [
- "v49.0",
- 2.16
- ],
- [
- "v48.0",
- 0.13
- ],
- [
- "v47.0",
- 0.11
- ],
- [
- "v43.0",
- 0.17
- ],
- [
- "v29.0",
- 0.26
- ]
- ],type:undefined
- },
- {
- name: "Opera",
- id: "Opera",
- data: [
- [
- "v50.0",
- 0.96
- ],
- [
- "v49.0",
- 0.82
- ],
- [
- "v12.1",
- 0.14
- ]
- ],type:undefined
- }, {
- name: "Firefox",
- id: "Firefox",
- data: [
- [
- "v58.0",
- 1.02
- ],
- [
- "v57.0",
- 7.36
- ],
- [
- "v56.0",
- 0.35
- ],
- [
- "v55.0",
- 0.11
- ],
- [
- "v54.0",
- 0.1
- ],
- [
- "v52.0",
- 0.95
- ],
- [
- "v51.0",
- 0.15
- ],
- [
- "v50.0",
- 0.1
- ],
- [
- "v48.0",
- 0.31
- ],
- [
- "v47.0",
- 0.12
- ]
- ],type:undefined
- }, {
- name: "Internet Explorer",
- id: "Internet Explorer",
- data: [
- [
- "v11.0",
- 6.2
- ],
- [
- "v10.0",
- 0.29
- ],
- [
- "v9.0",
- 0.27
- ],
- [
- "v8.0",
- 0.47
- ]
- ],type:undefined
- },{
- name: "Safari",
- id: "Safari",
- data: [
- [
- "v11.0",
- 3.39
- ],
- [
- "v10.1",
- 0.96
- ],
- [
- "v10.0",
- 0.36
- ],
- [
- "v9.1",
- 0.54
- ],
- [
- "v9.0",
- 0.13
- ],
- [
- "v5.1",
- 0.2
- ]
- ],type:undefined
- },{
- name: "Edge",
- id: "Edge",
- data: [
- [
- "v16",
- 2.6
- ],
- [
- "v15",
- 0.92
- ],
- [
- "v14",
- 0.4
- ],
- [
- "v13",
- 0.1
- ]
- ],type:undefined
- }
- ]
- }
-
- };
Challenges
We cannot use options directly like this, it causes type error
- const options = {
- title: {
- text: 'My chart'
- },
- series: [{
- data: [1, 2, 3]
- }]
- }
Instead we use this
- const options: Highcharts.Options = {
- title: {
- text: 'My chart'
- },
- series: [{
- data: [1, 2, 3]
- }]
- }
Importing statement looks like below
- import drilldown from 'highcharts/modules/drilldown';
- import * as Highcharts from 'highcharts';
- import HighchartsReact from 'highcharts-react-official';
- drilldown(Highcharts)
Because in every sample available javascript is defined as,
- import Highcharts from 'highcharts';(Here no default export available so its causes error)
For drilleddown we have to specify type:undefined for every data,or else it causes error,
- Sample Output
- Normal PieChart
- Drilldown Chart
for Official Documentation
Visit Here
Happy Coding :)