2017-07-05 38 views
1

我需要包括我的JSON字符串輸出到highcharts --pie圖表類 但圖表不正確加載加載JSON字符串中highcharts餅圖

這是我的JSON字符串

var json = {"{\"name\":\"BillToMobile\"}":{"y":2.35},"{\"name\":\"Telenav\"}":{"y":13.59}} 


Highcharts.chart('container', { 
    chart: { 
     plotBackgroundColor: null, 
     plotBorderWidth: null, 
     plotShadow: false, 
     type: 'pie' 
    }, 
    title: { 
     text: '' 
    }, 
    tooltip: { 
     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
    }, 
    plotOptions: { 
     pie: { 
      allowPointSelect: true, 
      cursor: 'pointer', 
      dataLabels: { 
       enabled: true, 
       format: '<b>{point.name}</b>: {point.percentage:.1f} %', 
       style: { 
        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' 
       } 
      } 
     } 
    }, 
    series: [{ 
     name: 'Brands', 
     colorByPoint: true, 
     data: json 
    }] 
}); 

下面是我在加載這個jsonstring時得到的圖表。 任何人都可以幫助我,因爲我是新手。感謝提前。

This is the chart error output

+0

您的JSON是有效的,但它不包含您認爲它的值。 – evolutionxbox

回答

2

嘗試格式化你的JSON這樣

var json = [{name: "BillToMobile", y: 2.35}, {name: "Telenav", y: 13.59}] 

要轉換你有,你可以用這個JSON:

ES5或更早

var properJson = []; 
for (var i in json) { 
    var item = JSON.parse(i); 
    for (var j in json[i]) { 
     item[j] = json[i][j]; 
    } 
    properJson.push(item); 
} 

ES6

var properJson = []; 
for (var i in json) { 
    var item = JSON.parse(i); 
    Object.assign(item, json[i]); 
    properJson.push(item); 
} 
+0

感謝您的回答。我可以在JavaScript內部格式化嗎?有沒有什麼方法或提示? –

+0

@IsuruLakshan檢查更新的答案。 – H77

+0

非常感謝。我對你的快速反應非常滿意。現在工作正常 –