HighCharts With Drilldown In Spfx

In this blog, you will learn about HighCharts with Drilldown in Spfx.

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
  1. import * as React from 'react';  
  2. import * as ReactDom from 'react-dom';  
  3. import { IHighchartProps } from './IHighchartProps';  
  4. import drilldown  from 'highcharts/modules/drilldown';  
  5. import * as Highcharts from 'highcharts';  
  6. import HighchartsReact from 'highcharts-react-official';  
  7. drilldown(Highcharts);  
  8.   
  9. export default class Highchart extends React.Component<IHighchartProps, {}> {  
  10.   private _myhighchart: HTMLElement = undefined;  
  11.   public render(): React.ReactElement<IHighchartProps> {   
  12.     return (  
  13.       <div id="mycontainer">  
  14. <HighchartsReact constructorType={'chart'} highcharts={Highcharts} options={options} />  
  15.       </div>  
  16.     );  
  17.   }  
  18. }  
  19. const options: Highcharts.Options = {  
  20.   chart: {  
  21.     type: 'pie'  
  22. },  
  23.   title: {  
  24.       text: 'My chart'  
  25.   },  
  26.   plotOptions: {  
  27.     series: {  
  28.         dataLabels: {  
  29.             enabled: true,  
  30.             format: '{point.name}: {point.y:.1f}%'  
  31.         }  
  32.     }  
  33. },  
  34.   
  35. tooltip: {  
  36.     headerFormat: '<span style="font-size:11px">{series.name}</span><br>',  
  37.     pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'  
  38. },  
  39.   
  40.  series: [  
  41.         {  
  42.             name: "Browsers",  
  43.             type: "pie",  
  44.             data: [  
  45.                 {  
  46.                     name: "Chrome",  
  47.                     y: 62.74,  
  48.                     drilldown: "Chrome"  
  49.                 },  
  50.                 {  
  51.                     name: "Firefox",  
  52.                     y: 10.57,  
  53.                     drilldown: "Firefox"  
  54.                 },  
  55.                 {  
  56.                     name: "Internet Explorer",  
  57.                     y: 7.23,  
  58.                     drilldown: "Internet Explorer"  
  59.                 },  
  60.                 {  
  61.                     name: "Safari",  
  62.                     y: 5.58,  
  63.                     drilldown: "Safari"  
  64.                 },  
  65.                 {  
  66.                     name: "Edge",  
  67.                     y: 4.02,  
  68.                     drilldown: "Edge"  
  69.                 },  
  70.                 {  
  71.                     name: "Opera",  
  72.                     y: 1.92,  
  73.                     drilldown: "Opera"  
  74.                 },  
  75.                 {  
  76.                     name: "Other",  
  77.                     y: 7.62,  
  78.                     drilldown: null  
  79.                 }  
  80.             ]  
  81.         }  
  82.     ],  
  83. drilldown: {  
  84.     series: [  
  85.       {  
  86.         name: "Chrome",  
  87.                 id: "Chrome",  
  88.         data: [  
  89.             [  
  90.                 "v65.0",  
  91.                 0.1  
  92.             ],  
  93.             [  
  94.                 "v64.0",  
  95.                 1.3  
  96.             ],  
  97.             [  
  98.                 "v63.0",  
  99.                 53.02  
  100.             ],  
  101.             [  
  102.                 "v62.0",  
  103.                 1.4  
  104.             ],  
  105.             [  
  106.                 "v61.0",  
  107.                 0.88  
  108.             ],  
  109.             [  
  110.                 "v60.0",  
  111.                 0.56  
  112.             ],  
  113.             [  
  114.                 "v59.0",  
  115.                 0.45  
  116.             ],  
  117.             [  
  118.                 "v58.0",  
  119.                 0.49  
  120.             ],  
  121.             [  
  122.                 "v57.0",  
  123.                 0.32  
  124.             ],  
  125.             [  
  126.                 "v56.0",  
  127.                 0.29  
  128.             ],  
  129.             [  
  130.                 "v55.0",  
  131.                 0.79  
  132.             ],  
  133.             [  
  134.                 "v54.0",  
  135.                 0.18  
  136.             ],  
  137.             [  
  138.                 "v51.0",  
  139.                 0.13  
  140.             ],  
  141.             [  
  142.                 "v49.0",  
  143.                 2.16  
  144.             ],  
  145.             [  
  146.                 "v48.0",  
  147.                 0.13  
  148.             ],  
  149.             [  
  150.                 "v47.0",  
  151.                 0.11  
  152.             ],  
  153.             [  
  154.                 "v43.0",  
  155.                 0.17  
  156.             ],  
  157.             [  
  158.                 "v29.0",  
  159.                 0.26  
  160.             ]  
  161.         ],type:undefined  
  162.     },  
  163.     {  
  164.       name: "Opera",  
  165.       id: "Opera",  
  166.       data: [  
  167.           [  
  168.               "v50.0",  
  169.               0.96  
  170.           ],  
  171.           [  
  172.               "v49.0",  
  173.               0.82  
  174.           ],  
  175.           [  
  176.               "v12.1",  
  177.               0.14  
  178.           ]  
  179.       ],type:undefined  
  180.   },  {  
  181.     name: "Firefox",  
  182.     id: "Firefox",  
  183.     data: [  
  184.         [  
  185.             "v58.0",  
  186.             1.02  
  187.         ],  
  188.         [  
  189.             "v57.0",  
  190.             7.36  
  191.         ],  
  192.         [  
  193.             "v56.0",  
  194.             0.35  
  195.         ],  
  196.         [  
  197.             "v55.0",  
  198.             0.11  
  199.         ],  
  200.         [  
  201.             "v54.0",  
  202.             0.1  
  203.         ],  
  204.         [  
  205.             "v52.0",  
  206.             0.95  
  207.         ],  
  208.         [  
  209.             "v51.0",  
  210.             0.15  
  211.         ],  
  212.         [  
  213.             "v50.0",  
  214.             0.1  
  215.         ],  
  216.         [  
  217.             "v48.0",  
  218.             0.31  
  219.         ],  
  220.         [  
  221.             "v47.0",  
  222.             0.12  
  223.         ]  
  224.     ],type:undefined  
  225. }, {  
  226.   name: "Internet Explorer",  
  227.   id: "Internet Explorer",  
  228.   data: [  
  229.       [  
  230.           "v11.0",  
  231.           6.2  
  232.       ],  
  233.       [  
  234.           "v10.0",  
  235.           0.29  
  236.       ],  
  237.       [  
  238.           "v9.0",  
  239.           0.27  
  240.       ],  
  241.       [  
  242.           "v8.0",  
  243.           0.47  
  244.       ]  
  245.   ],type:undefined  
  246. },{  
  247.   name: "Safari",  
  248.   id: "Safari",  
  249.   data: [  
  250.       [  
  251.           "v11.0",  
  252.           3.39  
  253.       ],  
  254.       [  
  255.           "v10.1",  
  256.           0.96  
  257.       ],  
  258.       [  
  259.           "v10.0",  
  260.           0.36  
  261.       ],  
  262.       [  
  263.           "v9.1",  
  264.           0.54  
  265.       ],  
  266.       [  
  267.           "v9.0",  
  268.           0.13  
  269.       ],  
  270.       [  
  271.           "v5.1",  
  272.           0.2  
  273.       ]  
  274.   ],type:undefined  
  275. },{  
  276.   name: "Edge",  
  277.   id: "Edge",  
  278.   data: [  
  279.       [  
  280.           "v16",  
  281.           2.6  
  282.       ],  
  283.       [  
  284.           "v15",  
  285.           0.92  
  286.       ],  
  287.       [  
  288.           "v14",  
  289.           0.4  
  290.       ],  
  291.       [  
  292.           "v13",  
  293.           0.1  
  294.       ]  
  295.   ],type:undefined  
  296. }  
  297.     ]  
  298. }  
  299.     
  300. };  
Challenges

We cannot use options directly like this, it causes type error
  1. const options = {  
  2.   title: {  
  3.     text: 'My chart'  
  4.   },  
  5.   series: [{  
  6.     data: [1, 2, 3]  
  7.   }]  
  8. }  
Instead we use this 
  1. const options: Highcharts.Options = {  
  2.   title: {  
  3.     text: 'My chart'  
  4.   },  
  5.   series: [{  
  6.     data: [1, 2, 3]  
  7.   }]  
  8. }  
  Importing statement looks like below
  1. import drilldown from 'highcharts/modules/drilldown';
  2. import * as Highcharts from 'highcharts';  
  3. import HighchartsReact from 'highcharts-react-official';  
  4. drilldown(Highcharts)  
Because in every sample available javascript is defined as,
  1. 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 :)