简体   繁体   中英

breaking json for stacked highcharts

I am passing a Json to highcharts

json: {"0":[{"name":"normal","data":["1647","270905","412080","13609","17062","20889","16","765097","13424","14","2044","630686","104019","3097","733","1462"]},{"name":"rft","data":[1300177,4599692,957739,4177103,3436151,3752121,1978772,1850918,2538336,521542,2951953,4108915,1516107,3329831,661657,3447965]}]}

and used this code to breakdown and passing to highcharts:

series: [{
    data: (function() {
      var chart_data =[];
      $.each(obj[0], function (index, value) {
          var arra=[];
         // alert(value['name'])
          arra['name']=value['name'];
          arra['data']=[];
          //alert(value['data'].length);
          $.each(value['data'],function(ind,val){
            arra['data'].push(parseInt(val));
          });
          //console.log(arra);
          chart_data.push(arra);
          });
      return chart_data ;
  })()
}]

but not working....

     var arra=[];
     // alert(value['name'])
     arra['name']=value['name'];

This code shows you are treating an array (only number indices) as an object (string index). Try using:

     var arra={};
     // alert(value['name'])
     arra['name']=value['name'];

First construct your JSON Data to exclude the keys "0" and "1" if that will be possible. Once done you can plot your graph as can be seen on this JSFiddle

var obj=[{"name":"normal","data":["1647","270905","412080","13609","17062","20889","16","765097","13424","14","2044","630686","104019","3097","733","1462"]},{"name":"rft","data":[1300177,4599692,957739,4177103,3436151,3752121,1978772,1850918,2538336,521542,2951953,4108915,1516107,3329831,661657,3447965]}];
$(function () {
    $('#container').highcharts({
        chart: {
            type: 'spline'
        },
        title: {
            text: 'Snow depth at Vikjafjellet, Norway'
        },
        subtitle: {
            text: 'Irregular time data in Highcharts JS'
        },
        xAxis: {
            type: 'datetime',
            dateTimeLabelFormats: { // don't display the dummy year
                month: '%e. %b',
                year: '%b'
            },
            title: {
                text: 'Date'
            }
        },
        yAxis: {
            title: {
                text: 'Snow depth (m)'
            },
            min: 0
        },
        tooltip: {
            headerFormat: '<b>{series.name}</b><br>',
            pointFormat: '{point.x:%e. %b}: {point.y:.2f} m'
        },

        series: (function(){
            return obj;
        })()
    });
});

But if your JSON Data must be as it is currently, then you can change series to:

        series: (function(){
            return obj[0];
        })()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM