Highchart:如何通过单击按钮更新动态图表
Highchart: how to update a dynamic chart by clicking on a button?
我打算在我的网页上有一个按钮,这样在单击按钮时,一个点将被添加到图表中,而无需重新绘制整个图表(该图表是此的修改版本http://www.highcharts.com/demo/dynamic-update)。但是,我当前的代码不起作用。
以下是相关代码:http://jsfiddle.net/wtvaz9gc/7/
var series;
$(function drawCharts(numberOfPoint) {
// if(typeof chartData == undefined){
// chartData = $(dataStore.getXml());
// }
$("#b").click(function(){
series.addPoint([3,3]);
})
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
type: 'line',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
series = this.series[0];
// window.myGlobal1 = this.series[0].data[this.series[0].data.length - 1].y;
// console.log(window.myGlobal1 + " " + this.series[0].data[this.series[0].data.length - 1].y);
},
}
},
title: {
text: ''
},
xAxis: {
title: {
text: 'Jahre'
},
// gridLineWidth: 0,
// lineWidth:1,
startOnTick: true,
tickPixelInterval: 40,
min: 0,
max: 10,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
yAxis: {
title: {
text: 'Vermögen(in EUR)'
},
labels: {
enabled: true
},
min: 0,
max: 100,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
enabled : false,
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: ($(function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i, preValue;
for (i = 0; i < numberOfPoint; i += 1) {
if(i == 0){
data.push({
x: i,
y: 10
});
} else {
data.push({
x: i,
y: chartData["wealth"][0][i]
});
}
}
// showMsg(data);
// console.log(data);
return data;
}()))
}]
});
});
});
当我尝试在chrome中运行它时,我得到了以下错误报告:highcharts.js:未捕获类型错误:i.splice不是函数addPoint@highcharts.js:…
在这种情况下,我应该如何使用函数"addPoint"?还是应该使用其他方法来达到目的?
生成初始数据的函数有问题,但addPoint工作正常:
var series;
$(function drawCharts(numberOfPoint) {
// if(typeof chartData == undefined){
// chartData = $(dataStore.getXml());
// }
$("#b").click(function(){
series.addPoint([10,10]);
})
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
type: 'line',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
series = this.series[0];
// window.myGlobal1 = this.series[0].data[this.series[0].data.length - 1].y;
// console.log(window.myGlobal1 + " " + this.series[0].data[this.series[0].data.length - 1].y);
},
}
},
title: {
text: ''
},
xAxis: {
title: {
text: 'Jahre'
},
// gridLineWidth: 0,
// lineWidth:1,
startOnTick: true,
tickPixelInterval: 40,
min: 0,
max: 10,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
yAxis: {
title: {
text: 'Vermögen(in EUR)'
},
labels: {
enabled: true
},
min: 0,
max: 100,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
enabled : false,
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: [1,2,3]
}]
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<button id="b" href="#" >AddPoints</button>
我不确定你想在series.data
函数中做什么,但删除所有代码并在点击事件中定义图表,这样它就知道series
是什么,这确实增加了点[3,3]
。
系列声明现在:
series: [{
name: 'Random data',
data: []
}]
和点击功能(没有移动后的图表代码):
$("#b").click(function() {
var chart = $('#container').highcharts();
chart.series[0].addPoint([3, 3]);
})
现场演示。
相关文章:
- 更新动态JSP内容
- 添加动态文本框时更新动态高度
- jquery赢得't更新动态生成的html
- 如何使用实时数据更新(动态)标记图标
- Ajax可以更新动态导航菜单吗
- 使用select选项更新动态生成的两个框的价格
- 如何在Javascript中异步更新动态生成的HTML控件
- JQuery没有't更新:动态添加元素的最后一个筛选器
- 如何在service-worker.js中更新动态url
- 在javascript中使用函数更新动态变量
- 如何在jquery中更新动态标签后重定向第二个html页面
- 更新动态生成器$set
- 使用knockout更新动态生成的按钮文本
- $compile不更新动态生成的HTML运行时
- PHP -更新动态HTML表中的一行
- 使用Jquery连续删除和更新动态创建的表行id
- 使用指定的x和y值更新动态Dojo图表
- Highchart:如何通过单击按钮更新动态图表
- 正在更新动态HTML文件
- 使用AJAX在Rails中更新动态图表