我正在尝试创建一个函数,该函数将根据传递的参数动态构建 Highcharts 图表。我按照以下方式进行:
function makeChart(name, title, series)
{
var options = {
chart: {
type: 'areaspline',
renderTo: name
},
credits: { enabled: false },
legend: { enabled: true },
title: {
text: title
},
xAxis: {
type: 'datetime'
},
yAxis: {
gridLineDashStyle: 'dot',
title: {
text: 'Quantity'
}
},
plotOptions: {
areaspline: {
animation: false,
stacking: '',
lineWidth: 1,
marker: { enabled: false }
}
},
series: [] //chart does not display except title. It will draw if I paste the data here manually
};
this.chart = new Highcharts.Chart(options);
for (index = 0; index < series.length; ++index) {
options.series[index] = {'name':series[index][0], 'data':series[index][1], 'color':series[index][2], 'fillOpacity': .3};
}
}
makeChart('container2', 'second chart', [['thisisname1', [20,21,22,23,24,25,26,27,28], '#d8d8d8']]);//calling function with test parameters
但我能看到的只是图表标题。我猜问题在于将数据添加到系列数组中。我尝试用多种方式添加它,但它不起作用,尽管我看到如果我 console.log(options.series) 则数据已添加。有什么想法如何解决这个问题吗?谢谢。
最佳答案
将 this.chart = new Highcharts.Chart(options);
放在 for
循环之后。
您要在图表初始化后添加数据,要以这种方式工作,您需要告诉 HighCharts 重新绘制自身,更简单的选择是在循环后初始化。 :)
关于javascript highcharts 构建器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261218/