Highchart:如何通过单击按钮更新动态图表

Highchart: how to update a dynamic chart by clicking on a button?

本文关键字:更新 动态 按钮 单击 何通过 Highchart      更新时间:2023-09-26

我打算在我的网页上有一个按钮,这样在单击按钮时,一个点将被添加到图表中,而无需重新绘制整个图表(该图表是此的修改版本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]);
  })

现场演示。