Highcharts 多系列动态柱形图更新
Highcharts Multiple Series Dynamic Column Chart Update
我在一个项目中使用 highcharts,我在创建动态更新的多个系列数据时遇到了问题。
我不确定我做错了什么,下面是示例数据和代码。
请注意以下代码适用于单系列数据,但我需要对多个系列进行哪些更改?
示例数据
{"BleedEnthalpy":{"0":1.495308553,"1":0.4441197889,"2":0.8609127688,"3":1.0913122458,"4":1.2085670076},"BypassRatio":{ "0":0.0602932228,"1":0.0020143045,"2":0.1111420462,"3":0.0017957639,"4":0.0665590016}}
代码
Highcharts.chart('other', {
chart: {
type: 'column',
backgroundColor: null,
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
var iter=0;
// set up the updating of the chart each second
var series = this.series[0];
myInterval = setInterval(function() {
var len = Object.keys(BleedEnthalpy).length;
if (iter < len) {
series.addPoint([(new Date()).getTime(), BleedEnthalpy[iter]], true, true);
iter++;
} else {
clearInterval(myInterval);
}
}, 1000);
}
}
},
title: {
text: null
},
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, 4);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
rangeSelector: {
enabled: false
},
navigator: {
enabled: false
},
scrollbar: {
enabled: false
},
series: [{
name: 'R data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: BleedEnthalpy
});
}
return data;}()) }] });
您必须创建另一个系列,然后以与第一个系列相同的方式为其添加点。
如果你有两组数据:
var BleedEnthalpy = {"0":1.495308553,"1":0.4441197889,"2":0.8609127688,"3":1.0913122458,"4":1.2085670076};
var BypassRatio = { "0":0.0602932228,"1":0.0020143045,"2":0.1111420462,"3":0.0017957639,"4":0.0665590016};
创建两个系列:
series: [{
...
}, {
...
}]
然后在时间间隔回调中为两个系列添加点
load: function() {
var iter = 0;
// set up the updating of the chart each second
var series = this.series[0],
series2 = this.series[1];
myInterval = setInterval(function() {
var len = Object.keys(BleedEnthalpy).length;
var len2 = Object.keys(BypassRatio).length,
x = new Date().getTime();
if (iter < len) {
series.addPoint([x, BleedEnthalpy[iter]], false, true);
series2.addPoint([x, BypassRatio[iter]], true, true);
iter++;
} else {
clearInterval(myInterval);
}
}, 1000);
}
第二个 y 轴不是必需的,但如果系列具有不同的比例,它很有用。
我在一个项目中使用 highcharts,我在创建动态更新的多个系列数据时遇到了问题。 我不确定我做错了什么,下面是示例数据和代码。 请注意以下代码适用于单系列数据,但我需要对多个系列进行哪些更改?
示例数据
{"BleedEnthalpy":{"0":1.495308553,"1":0.4441197889,"2":0.8609127688,"3":1.0913122458,"4":1.2085670076},"BypassRatio":{ "0":0.0602932228,"1":0.0020143045,"2":0.1111420462,"3":0.0017957639,"4":0.0665590016}}
代码
Highcharts.chart('other', {
chart: {
type: 'column',
backgroundColor: null,
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
var iter=0;
// set up the updating of the chart each second
var series = this.series[0];
myInterval = setInterval(function() {
var len = Object.keys(BleedEnthalpy).length;
if (iter < len) {
series.addPoint([(new Date()).getTime(), BleedEnthalpy[iter]], true, true);
iter++;
} else {
clearInterval(myInterval);
}
}, 1000);
}
}
},
title: {
text: null
},
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, 4);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
rangeSelector: {
enabled: false
},
navigator: {
enabled: false
},
scrollbar: {
enabled: false
},
series: [{
name: 'R data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: BleedEnthalpy
});
}
return data;}()) }] });
您必须创建另一个系列,然后以与第一个系列相同的方式为其添加点。
如果你有两组数据:
var BleedEnthalpy = {"0":1.495308553,"1":0.4441197889,"2":0.8609127688,"3":1.0913122458,"4":1.2085670076};
var BypassRatio = { "0":0.0602932228,"1":0.0020143045,"2":0.1111420462,"3":0.0017957639,"4":0.0665590016};
创建两个系列:
series: [{
...
}, {
...
}]
然后在时间间隔回调中为两个系列添加点
load: function() {
var iter = 0;
// set up the updating of the chart each second
var series = this.series[0],
series2 = this.series[1];
myInterval = setInterval(function() {
var len = Object.keys(BleedEnthalpy).length;
var len2 = Object.keys(BypassRatio).length,
x = new Date().getTime();
if (iter < len) {
series.addPoint([x, BleedEnthalpy[iter]], false, true);
series2.addPoint([x, BypassRatio[iter]], true, true);
iter++;
} else {
clearInterval(myInterval);
}
}, 1000);
}
第二个 y 轴不是必需的,但如果系列具有不同的比例,它很有用。