Fill chart with dynamic json data - Javascript highcharts

Javascript examples for highcharts:Chart Json Data

Description

Fill chart with dynamic json data

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-compat-git.js"></script> 
      <script type="text/javascript" src="https://code.highcharts.com/highcharts.js"></script> 
      <script type="text/javascript">
    $(window).on('load', function() {
HighChartObject = function (obj) {
    if (typeof obj === 'string') obj = JSON.parse(obj);
    for (var key in obj) {//from w  ww .  j a  va 2  s.c om
        this[key] = obj[key];
    }
}
HighChartObject.prototype.getDataArray = function () {
    var arr = [];
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            arr.push(obj[key]);
        }
    }
    return arr;
}
HighChartObject.prototype.getCategoriesArray = function () {
    var arr = [];
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            arr.push(key);
        }
    }
    return arr;
}
var json = '{ "A": 5, "B": 12, "C": 5, "D": 11, "E": 6 }';
var obj = new HighChartObject(json);
$("#container").highcharts({
    xAxis: {
        categories: obj.getCategoriesArray()
    },
    series: [
        {
            name: "MyName",
            data: obj.getDataArray()
        }
    ]
});
    });

      </script> 
   </head> 
   <body> 
      <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials