Add Categories and Series dynamically from JSON - Javascript highcharts

Javascript examples for highcharts:Chart Json Data

Description

Add Categories and Series dynamically from JSON

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Highcharts Demo</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from  ww  w.  j  av a2  s. c  o m
var jsondata = [{
  "Categories": "['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']"
}, {
  "SeiresName": "Ava",
  "Data": "[49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]"
}, {
  "SeiresName": "Nima",
  "Data": "[83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]"
}, {
  "SeiresName": "Arian",
  "Data": "[48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]"
}]
var categories = [];
var seriesData = [];
for (var i = 0; i < jsondata.length; i++) {
  if (jsondata[i].Categories) {
    categories = jsondata[i].Categories.replace(/\[|]|'/g, '').split(',')
  }
  if (jsondata[i].SeiresName) {
    seriesData.push({
      name: jsondata[i].SeiresName,
      data: JSON.parse(jsondata[i].Data)
    })
  }
}
Highcharts.chart('container', {
  chart: {
    type: 'column'
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Y-Axis Title'
    }
  },
  legend: {
    reversed: true
  },
  xAxis: {
    categories: categories
  },
  credits: {
    enabled: false
  },
  series: seriesData
});
    }

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <script src="https://code.highcharts.com/modules/exporting.js"></script> 
      <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials