过去几天我遇到了一些问题,他们从api中获取一些示例json数据,使用Highcharts库填充图表。
我试图在我的ajax回调中使用chart.series [0] .data = json和类似的东西,但没有任何作用。
我的json是一个月中每天的数据数组。
"{"month_mentions_graphic":[521,49,81,0,101,0,0,0,21,3071,0,0,0,0,0,1479,6124,2409,2608,0,0,3457,2057,2580,5876,4638,0,0,3337,3479,430]}"
这是我的代码:
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25,
events: {
load: requestData
}
},
title: {
text: 'Menções Mensais',
x: -20 //center
},
xAxis: {
categories: [1,2,3,4,5]
},
yAxis: {
title: {
text: 'Menções'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [
{
name: 'mentions',
data: []
}
]
});
});
function requestData() {
$.ajax({
url: 'api/v1/dashboard/month_mention_graphic',
type: "GET",
dataType: "json",
data : {username : "demo"},
success: function(data) {
chart.series[0].data = data;
},
cache: false
});
}
答案 0 :(得分:17)
调用chart.addSeries一次性添加整个系列,而不是仅将点数组添加到初始空系列中:
function requestData() {
$.ajax({
url: 'api/v1/dashboard/month_mention_graphic',
type: "GET",
dataType: "json",
data : {username : "demo"},
success: function(data) {
chart.addSeries({
name: "mentions",
data: data.month_mentions_graphic
});
},
cache: false
});
}