简体   繁体   English

Highcharts多系列动态柱形图更新

[英]Highcharts Multiple Series Dynamic Column Chart Update

I am using highcharts on a project, I am having trouble with the creation of multiple series of data updating dynamically. 我在一个项目上使用图表,在创建多个动态更新的数据系列时遇到麻烦。 I'm not sure what I'm doing wrong, below is the sample data and code. 我不确定自己在做什么错,下面是示例数据和代码。 Please note the below code is working with single series of data, but what changes I need to make for more than one series? 请注意,以下代码适用于单个数据系列,但是我需要对多个数据系列进行哪些更改?

Sample Data 样本数据

{"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}}

Code

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;}()) }]  });

You have to create another series and then add point to it in the same way as in the first series. 您必须创建另一个系列,然后以与第一个系列相同的方式向其添加点。

If you have two sets of 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};

create two series: 创建两个系列:

series: [{
  ...
}, {
  ...
}]

and then on in time interval callback add points to both 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);
  }

The second y axis is not necessary but it is useful if the series have different scales. 第二个y轴不是必需的,但是如果系列具有不同的比例,它将很有用。

example: https://jsfiddle.net/jv8a1955/ 例如: https//jsfiddle.net/jv8a1955/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM