Generate JSON Based On Drill Down and Drill Up Events

Introduction

 
This article shows how to generate or create JSON dynamically based on the user's drill up and drill down event actions. We will create JSON when the user clicks on a drill-down chart series. We will also learn how to create a Drill Down Chart. Using the created JSON we can get the information on which series have been clicked by the user or which drill series have been seen by the user. We can do some more things with this JSON. 
 
Please see this article on my blog here
 
Background
 
For the past few weeks, I have been working with the High Chart. And I had a chance to work with Drill Down chart in High Chart. After implementing a drill-down chart in my application successfully, I got another requirement for creating JSON dynamically based on the user's drill down and drill up event action.
 
Practical Usage
 
Hereby I will share the practical usage of the dynamically created JSON. We have used this approach in our application. The task was simple but tough though. We have implemented a zooming functionality for each chart we have created. So we placed a zoom button above the chart. When the user clicks the zoom button, a dialogue box will open with the drill-down chart with maximum width and height.
 
The problem we encountered is when the user drills the chart to some level and clicks the zoom button, we were unable to maintain the same drill level in the chart that is loaded in the dialogue box. Since this functionality was not available in the HighChart itself, we thought to do this on our own. For this, we needed the drill level information in which the user clicked. So we maintained the information in the JSON format. And we take the values from this JSON when the new dialogue opens and is applied this to the chart.
 
Demo
 
You can see the demo here: Generate JSON According To Drill Down Drill Up Events – Sibeesh Passion.
 
Using the code
 
The first thing we need to do is to load the required files.
  1. <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>    
  2. <script src="http://sibeeshpassion.com/content/scripts/highcharts.js"></script>    
  3. <script src="http://sibeeshpassion.com/content/scripts/drilldown.js"></script>   
Now we need the data for the chart.
  1. data: [{    
  2.     name: 'Fruits',    
  3.     y: 10,    
  4.     drilldown: 'fruits'    
  5. }, {    
  6.     name: 'Cars',    
  7.     y: 12,    
  8.     drilldown: 'cars'    
  9. }, {    
  10.     name: 'Countries',    
  11.     y: 8    
  12. }]   
Since we are creating a drill down chart, we need to set the drill down data also for the chart.
  1. drilldown: {    
  2.     series: [{    
  3.         id: 'fruits',    
  4.         name: 'Fruits',    
  5.         data: [    
  6.             ['Apples', 4],    
  7.             ['Pears', 6],    
  8.             ['Oranges', 2],    
  9.             ['Grapes', 8]    
  10.         ]    
  11.     }, {    
  12.         id: 'cars',    
  13.         name: 'Cars',    
  14.         data: [{    
  15.                 name: 'Toyota',    
  16.                 y: 4,    
  17.                 drilldown: 'toyota'    
  18.             },    
  19.             ['Volkswagen', 3],    
  20.             ['Opel', 5]    
  21.         ]    
  22.     }, {    
  23.         id: 'toyota',    
  24.         name: 'Toyota',    
  25.         data: [    
  26.             ['RAV4', 3],    
  27.             ['Corolla', 1],    
  28.             ['Carina', 4],    
  29.             ['Land Cruiser', 5]    
  30.         ]    
  31.     }]    
  32. }   
So we have set the data and now we need to set complete options for our chart, we will set this in a variable.
  1. var options = {    
  2.     chart: {    
  3.         height: 300,    
  4.         events: {    
  5.             drilldown: function(e) {},    
  6.             drillup: function(e) {}    
  7.         }    
  8.     },    
  9.     title: {    
  10.         text: 'Highcharts Drilldown Plugin'    
  11.     },    
  12.     xAxis: {    
  13.         categories: true    
  14.     },    
  15.     drilldown: {    
  16.         series: [{    
  17.             id: 'fruits',    
  18.             name: 'Fruits',    
  19.             data: [    
  20.                 ['Apples', 4],    
  21.                 ['Pears', 6],    
  22.                 ['Oranges', 2],    
  23.                 ['Grapes', 8]    
  24.             ]    
  25.         }, {    
  26.             id: 'cars',    
  27.             name: 'Cars',    
  28.             data: [{    
  29.                     name: 'Toyota',    
  30.                     y: 4,    
  31.                     drilldown: 'toyota'    
  32.                 },    
  33.                 ['Volkswagen', 3],    
  34.                 ['Opel', 5]    
  35.             ]    
  36.         }, {    
  37.             id: 'toyota',    
  38.             name: 'Toyota',    
  39.             data: [    
  40.                 ['RAV4', 3],    
  41.                 ['Corolla', 1],    
  42.                 ['Carina', 4],    
  43.                 ['Land Cruiser', 5]    
  44.             ]    
  45.         }]    
  46.     },    
  47.     legend: {    
  48.         enabled: false    
  49.     },    
  50.     plotOptions: {    
  51.         series: {    
  52.             dataLabels: {    
  53.                 enabled: true    
  54.             },    
  55.             shadow: false    
  56.         },    
  57.         pie: {    
  58.             size: '80%'    
  59.         }    
  60.     },    
  61.     series: [{    
  62.         name: 'Overview',    
  63.         colorByPoint: true,    
  64.         data: [{    
  65.             name: 'Fruits',    
  66.             y: 10,    
  67.             drilldown: 'fruits'    
  68.         }, {    
  69.             name: 'Cars',    
  70.             y: 12,    
  71.             drilldown: 'cars'    
  72.         }, {    
  73.             name: 'Countries',    
  74.             y: 8    
  75.         }]    
  76.     }]    
  77. };   
Everything is set now, what else we need then? Yes we need to assign this option to our chart!
  1. // Drill Down Chart Implementation    
  2. options.chart.renderTo = 'container';    
  3. options.chart.type = 'column';    
  4. var chart1 = new Highcharts.Chart(options);   
     
As you can see in the preceding code, we are rendering the chart to a div called container. So we need to create a div.
  1. <body>    
  2.     Generate JSON According To Drill Down Drill Up Events - Sibeesh Passion (<a href="http://sibeeshpassion.com">Sibeesh Passion </a>)    
  3.     <br/>    
  4.     <br/>    
  5.     <br/>    
  6.     <div id="container" style="height: 300px"></div>    
  7.     <div id="jsonContent"></div>    
  8. </body>   
Now we are ready to run our chart. Shall we run it? When you run it you will get the following output.
 
 
It is time to create the JSON now. We will save the JSON value to sessionStorage. If you are new to sessionStorage and localStorage you can read it here: Storage Mechanism in HTML5.
 
Once we assign the value to the storage, you can see it in the browser console. We will also set this value to a div called jsonContent.
 
We will create a function for creating this JSON first.
  1. function generateDrillDownJSON(e, isDrillUp) {    
  2.     try {    
  3.         if (isDrillUp) {    
  4.             if (myJSON != null && myJSON.length > 0) {    
  5.                 removeArrayElementByIndex(myJSON, myJSON.length - 1);    
  6.             }    
  7.             sessionStorage.setItem('DrillDownJSON', JSON.stringify(myJSON));    
  8.             $("#jsonContent").html('JSON content is: ').append(JSON.stringify(myJSON));    
  9.         } else {    
  10.             myJSON.push({    
  11.                 name: e.point.name,    
  12.                 level: myJSON.length + 1,    
  13.             });    
  14.             sessionStorage.setItem('DrillDownJSON', JSON.stringify(myJSON));    
  15.             $("#jsonContent").html('JSON content is: ').append(JSON.stringify(myJSON));    
  16.         }    
  17.     } catch (e) {    
  18.         console.log('generateHierarchyJSON :' + e.message);    
  19.     }    
  20. }   
As you can see we are handling the drill events in this function. If it is a drill up event then we are removing the values from the JSON created. To remove, we are creating another function. 
  1. function removeArrayElementByIndex(myArray, index) {    
  2.    myArray.splice(index, 1);    
  3. }   
We will call our function generateDrillDownJSON in both drill down and drill up events.
  1. events: {    
  2.     drilldown: function(e) {    
  3.         generateDrillDownJSON(e, false);    
  4.     },    
  5.     drillup: function(e) {    
  6.         generateDrillDownJSON(e, true);    
  7.     }    
  8. }   
It is time to see our implementation and check whether or not it is working properly. So run your application now and click on the Cars series. You can see the JSON created in our div.
 
If you need to see the JSON value in the session storage, please click F12 and check-in session storage.
 
Now we will try the drill up event and check the JSON value.
 
 
You can see that when drill up, we are deleting those values from JSON.
 
Complete Code
  1. <!DOCTYPE html>    
  2. <html>    
  3.     
  4. <head>    
  5.     <title>Generate JSON According To Drill Down Drill Up Events Demo-Sibeesh Passion</title>    
  6.     <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>    
  7.     <script src="http://sibeeshpassion.com/content/scripts/highcharts.js"></script>    
  8.     <script src="http://sibeeshpassion.com/content/scripts/drilldown.js"></script>    
  9.     <script>    
  10.         var myJSON = [];    
  11.         $(document).ready(function() {    
  12.             // Internationalization      
  13.             Highcharts.setOptions({    
  14.                 lang: {    
  15.                     drillUpText: '◁ Back to {series.name}'    
  16.                 }    
  17.             });    
  18.             var options = {    
  19.                 chart: {    
  20.                     height: 300,    
  21.                     events: {    
  22.                         drilldown: function(e) {    
  23.                             generateDrillDownJSON(e, false);    
  24.                         },    
  25.                         drillup: function(e) {    
  26.                             generateDrillDownJSON(e, true);    
  27.                         }    
  28.                     }    
  29.                 },    
  30.                 title: {    
  31.                     text: 'Highcharts Drilldown Plugin'    
  32.                 },    
  33.                 xAxis: {    
  34.                     categories: true    
  35.                 },    
  36.                 drilldown: {    
  37.                     series: [{    
  38.                         id: 'fruits',    
  39.                         name: 'Fruits',    
  40.                         data: [    
  41.                             ['Apples', 4],    
  42.                             ['Pears', 6],    
  43.                             ['Oranges', 2],    
  44.                             ['Grapes', 8]    
  45.                         ]    
  46.                     }, {    
  47.                         id: 'cars',    
  48.                         name: 'Cars',    
  49.                         data: [{    
  50.                                 name: 'Toyota',    
  51.                                 y: 4,    
  52.                                 drilldown: 'toyota'    
  53.                             },    
  54.                             ['Volkswagen', 3],    
  55.                             ['Opel', 5]    
  56.                         ]    
  57.                     }, {    
  58.                         id: 'toyota',    
  59.                         name: 'Toyota',    
  60.                         data: [    
  61.                             ['RAV4', 3],    
  62.                             ['Corolla', 1],    
  63.                             ['Carina', 4],    
  64.                             ['Land Cruiser', 5]    
  65.                         ]    
  66.                     }]    
  67.                 },    
  68.                 legend: {    
  69.                     enabled: false    
  70.                 },    
  71.                 plotOptions: {    
  72.                     series: {    
  73.                         dataLabels: {    
  74.                             enabled: true    
  75.                         },    
  76.                         shadow: false    
  77.                     },    
  78.                     pie: {    
  79.                         size: '80%'    
  80.                     }    
  81.                 },    
  82.                 series: [{    
  83.                     name: 'Overview',    
  84.                     colorByPoint: true,    
  85.                     data: [{    
  86.                         name: 'Fruits',    
  87.                         y: 10,    
  88.                         drilldown: 'fruits'    
  89.                     }, {    
  90.                         name: 'Cars',    
  91.                         y: 12,    
  92.                         drilldown: 'cars'    
  93.                     }, {    
  94.                         name: 'Countries',    
  95.                         y: 8    
  96.                     }]    
  97.                 }]    
  98.             };    
  99.             // Drill Down Chart Implementation      
  100.             options.chart.renderTo = 'container';    
  101.             options.chart.type = 'column';    
  102.             var chart1 = new Highcharts.Chart(options);    
  103.         });    
  104.     
  105.         function generateDrillDownJSON(e, isDrillUp) {    
  106.             try {    
  107.                 if (isDrillUp) {    
  108.                     if (myJSON != null && myJSON.length > 0) {    
  109.                         removeArrayElementByIndex(myJSON, myJSON.length - 1);    
  110.                     }    
  111.                     sessionStorage.setItem('DrillDownJSON', JSON.stringify(myJSON));    
  112.                     $("#jsonContent").html('JSON content is: ').append(JSON.stringify(myJSON));    
  113.                 } else {    
  114.                     myJSON.push({    
  115.                         name: e.point.name,    
  116.                         level: myJSON.length + 1,    
  117.                     });    
  118.                     sessionStorage.setItem('DrillDownJSON', JSON.stringify(myJSON));    
  119.                     $("#jsonContent").html('JSON content is: ').append(JSON.stringify(myJSON));    
  120.                 }    
  121.             } catch (e) {    
  122.                 console.log('generateHierarchyJSON :' + e.message);    
  123.             }    
  124.         }    
  125.     
  126.         function removeArrayElementByIndex(myArray, index) {    
  127.             myArray.splice(index, 1);    
  128.         }    
  129.     </script>    
  130. </head>    
  131.     
  132. <body>    
  133.     Generate JSON According To Drill Down Drill Up Events - Sibeesh Passion (<a href="http://sibeeshpassion.com">Sibeesh Passion </a>)    
  134.     <br/>    
  135.     <br/>    
  136.     <br/>    
  137.     <div id="container" style="height: 300px"></div>    
  138.     <div id="jsonContent"></div>    
  139. </body>    
  140.     
  141. </html>   

Conclusion

 
I hope you liked my article. Please share your valuable feedback and suggestions. Thanks in advance.