javascript Highcharts.js 不会呈现图表,它会显示错误“无法读取未定义的属性‘系列’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17867479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highcharts.js will not render the chart, it says error "Cannot read property 'series' of undefined"
提问by rrey
I think it is because my global var chart
hasn't been set yet when my function requestData
is called.
我认为这是因为var chart
我的函数requestData
被调用时我的全局还没有设置。
This is my code of highcharts inside a $(document).ready(function()
这是我的 highcharts 代码 $(document).ready(function()
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
marginRight: 130,
marginBottom: 25,
events: {
load: requestData()
}
},
title: {
text: 'Reporte PMU',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom : 20 * 1000,
title: {
text: 'tiempo'
},
labels : { y : 0, rotation: -60 }
},
yAxis: {
title: {
text: 'Amount'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{
name: 'serieTension',
data: []
}]
});
});
and this is my requestData ()
这是我的 requestData ()
$.ajax({
url: 'data2.php',
success: function(point) {
var series =chart.series[0],
shift = series.data.length > 20; //shift if the series is longer than 20
//add point
chart.series[0].addPoint(point, true, shift);
setTimeout(requestData, 1000);
},
cache : false
});
}
采纳答案by DeweyOx
Just place
就地
var chart;
outside all functions, next to your document ready handler to make it global.
在所有功能之外,在您的文档就绪处理程序旁边,使其成为全球性的。
EDIT:
编辑:
Also, add a reference inside the load call
另外,在加载调用中添加一个引用
load: function() {
chart = this; // `this` is the reference to the chart
requestData();
}