Sac*_*hin 2 ajax json highcharts
过去几天我一直遇到问题,从外部文件中获取一些示例json数据,使用Highcharts库填充饼图.
这是我在file:data.json中的示例JSON数据
[
["Apples", 43.0],
["Pears", 57.0]
]
Run Code Online (Sandbox Code Playgroud)
这是我对highcharts和我的AJAX调用的实现:(我省略了不相关的代码)
<script type="text/javascript">
$(function() {
var options = {
chart: {
renderTo: 'Chart',
defaultSeriesType: 'pie'
},
title: {
text:'Fruits'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
}
}
},
series: [{
type: 'pie',
name: 'Fruits',
data: []
}]
};
$.getJSON('data.json', function(json) {
options.series.push(json);
var chart = new Highcharts.Chart(options);
}).error(function() {console.log('error');});
});
</script>
Run Code Online (Sandbox Code Playgroud)
基本上,我想将JSON传递给options.series [].data [].继续
options.series.push(json);
Run Code Online (Sandbox Code Playgroud)
我明白了:
[Object, Array[2]] // where the Object contains .name and .type and the Array[2] is my data
Run Code Online (Sandbox Code Playgroud)
我很确定我需要这个:
[Object] // which contains .data , .name, .type
Run Code Online (Sandbox Code Playgroud)
我实际上能够通过构造我的JSON来解决我的问题:
[
{
"type" : "pie",
"name" : "Fruits",
"data" : [
[
"Apple",
43.0
],
[
"Pear",
"57.0"
]
]
}
]
Run Code Online (Sandbox Code Playgroud)
而不是做数组推送,
我将series参数设置为JSON,如下所示:
$.getJSON("data.json", function(json)) {
options.series = json;
var chart = new Highcharts.chart(options);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18505 次 |
| 最近记录: |