打破堆积的高图的json

时间:2014-08-23 08:28:30

标签: javascript json highcharts

我将Json传递给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]}]}

并使用此代码细分并传递给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 ;
  })()
}]

但没有工作......

2 个答案:

答案 0 :(得分:0)

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

此代码显示您将数组(仅数字索引)视为对象(字符串索引)。尝试使用:

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

答案 1 :(得分:0)

首先构造您的JSON数据以排除键“0”和“1”(如果可能的话)。完成后,您可以绘制图表,如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;
        })()
    });
});

但是如果您的JSON数据必须与当前一样,那么您可以将系列更改为:

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