У меня есть пример кода, где я хочу обновлять свою диаграмму каждый раз при событии «onmessage», но она не обновляет диаграмму.
Console.log () показывает мне, что обратный вызов выполняется с правильными значениями, но на диаграмме ничего не отображается.
HTML часть
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Часть JavaScript:
настройка параметров ....
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
},
plotOptions: {
series: {
turboThreshold: 0 // 0 to disable https://api.highcharts.com/highcharts/plotOptions.series.turboThreshold
}
}
});
создание графика с интервалом в 1 час:
var chart = Highcharts.chart('container', {
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10
},
title: {
text: 'Live Test.sensor.val'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
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: true
},
exporting: {
enabled: true
},
series: [{
name: 'Test.sensor.val',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -60 * 60; i <= 0; i += 1) { //defines the visible timespan on x axis
data.push({
x: time + i * 1000,
y: null
});
}
return data;
}())
}]
});
здесь я надеюсь получить значения на графике, но это не работает ...
Просто console.log () показывает мне значения, но на графике ничего не обновляется.
var ev = new EventSource("/incommingEventHandler.ashx");
ev.onmessage = function (e) {
var dat = e.data;
var x = (new Date()).getTime(), // current time
y = dat;
console.log(x, y); //showing values correctly but no update on chart? Why?
chart.series[0].addPoint([x, y], true, true);
};
});
Спасибо, Дитер