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

岁酱吖の 提交于 2020-01-06 03:48:26

问题


I intend to have a button on my web page so that upon clicking a button, a point will be added to a chart without redrawing the whole chart (the chart is a modified version of this http://www.highcharts.com/demo/dynamic-update). However, my current code is not working.

Here is the code in concern: 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;
                            }()))
                            }]
                        });
                    });
                });

When I tries to run it in chrome, I got the following error report: highcharts.js:Uncaught TypeError: i.splice is not a function addPoint @ highcharts.js:...

How should I use the function "addPoint" in this case? Or should I use other method to achieve the purpose?


回答1:


There is something wrong with the function generating your initial data, but addPoint is working fine:

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>



回答2:


I am not sure what you are trying to do in your series.data function but removing all that code and defining your chart in your click event so it knows what series is does add the point [3,3].

Series declaration now:

  series: [{
    name: 'Random data',
    data: []
  }]

And the click function (no moved after the chart code):

  $("#b").click(function() {
    var chart = $('#container').highcharts();
    chart.series[0].addPoint([3, 3]);
  })

Live demo.



来源:https://stackoverflow.com/questions/38788146/highchart-how-to-update-a-dynamic-chart-by-clicking-on-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!