Highcharts to populate data for pie chart using json object

后端 未结 2 1320
深忆病人
深忆病人 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条回答
  •  猫巷女王i
    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
        }]
    
    });
    

提交回复
热议问题