【发布时间】:2018-06-28 19:36:51
【问题描述】:
// To load chart
loadChart(): void {
let initialCardiacSamples = this.cardiacSamples;
this.ecgChart = Highcharts.chart(this.el.nativeElement, {
chart: {
type: 'line',
backgroundColor: '#4d2700',
},
title: {
text: 'ECG graph using Highcharts JS',
style: {
color: '#ffffff'
}
},
xAxis: {
type: 'number',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Amplitude',
style: {
color: '#ffffff'
}
},
gridLineWidth: 0,
minorGridLineWidth: 0,
max: 600
},
series: [{
name: 'ECG data',
data: initialCardiacSamples,
color: '#ffffff'
}]
});
}
// To update chart/ Calling update for every 300mili seconds here to take next data point
updateChart(): void {
let xAxisPoint = this.ecgChunkSize;
this.interval = setInterval(() => {
// Call addData() function to add next trace point to the graph
this.addDataPoint(this.ecgChunkSize, xAxisPoint);
this.ecgChunkSize = this.ecgChunkSize + 1;
xAxisPoint = xAxisPoint + 1;
if(this.ecgChunkSize == this.ecgFileSize) {
this.ecgChunkSize = 0;
}
}, 300);
}
// Method to add data and update the chart to make the graph real time
addDataPoint(index: number, xAxisPoint: number) : void {
let iy = this.cardiacSamples[index].y;
this.ecgChart.series[0].addPoint([xAxisPoint, iy], true, true);
//Calling addDataPoint() above with redraw chart and shift as true
}
.chart-container {
display: block;
margin:0;
position: relative;
height: calc(100vh - 450px - 40px);
padding-top: 0;
}
<div #highchartsEcg class="chart-container"></div>
在这里,我每 300 毫秒用 addPoint() 调用 updateChart()。有了这个间隔,它会非常慢,但会保持一定的图形形状。但是,如果我以 Normal graph shape with 300ms interval Graph shape collapse with 100ms interval
【问题讨论】:
-
这里没有放完整的代码。但是插入的代码足以理解初始加载和定期更新图表的场景。实际上是通过signalR从后端获取数据。
标签: javascript angular highcharts highcharts-ng angular2-highcharts