Highcharts to populate data for pie chart using json object

后端 未结 2 1302
深忆病人
深忆病人 2021-01-03 17:01

hi my json object looks like this

[
  {\"name\":\"Tokyo\",\"data\":3.0},
  {\"name\":\"NewYork\",\"data\":2.0},
  {\"name\":\"Berlin\",\"data\":3.5},
  {\"n         


        
相关标签:
2条回答
  • 2021-01-03 17:40

    Try this below code. This will parse all the values and create an array called dataArrayFinal .

    var d = [{"name":"Tokyo","data":3.0},{"name":"NewYork","data":2.0}, {"name":"Berlin","data":3.5},{"name":"London","data":1.5}]
    var name = Array();
    var data = Array();
    var dataArrayFinal = Array();
    for(i=0;i<d.length;i++) { 
       name[i] = d[i].name; 
       data[i] = d[i].data;  
    }
    
    for(j=0;j<name.length;j++) { 
       var temp = new Array(name[j],data[j]); 
       dataArrayFinal[j] = temp;     
    }
    

    And your series stuff should look like below. i.e, you should pass the array dataArrayFinal like below.

    series: [{
          type: 'pie',
          name: 'Browser share',
          data: dataArrayFinal
    }]
    
    0 讨论(0)
  • 2021-01-03 17:47

    Actually the only difference between your data definition and the format that Highcharts requires, is that yours has a property called "data" where Highcharts expects "y". So you just need to loop over the data and set that property. See it live at http://jsfiddle.net/highcharts/uTyZk/.

    // Original data
    var data = [{
        "name": "Tokyo",
        "data": 3.0
    }, {
        "name": "NewYork",
        "data": 2.0
    }, {
        "name": "Berlin",
        "data": 3.5
    }, {
        "name": "London",
        "data": 1.5
    }];
    
    // Highcharts requires the y option to be set
    $.each(data, function (i, point) {
        point.y = point.data;
    });
    
    
    var chart = new Highcharts.Chart({
    
        chart: {
            renderTo: 'container',
            type: 'pie'
        },
    
        series: [{
            data: data
        }]
    
    });
    
    0 讨论(0)
提交回复
热议问题