简体   繁体   中英

Difference between highcharts and highstock during real time trace and xAxis with max value

I got a problem with highstock. I'm trying to add real time data to my chart. But I noticed different behaviour from highchart and highstock when max xAxis is set.

For example, if the xAxis max is 100 and I add the value 20: - in the highcharts one, the value is added in the first half of the chart - in the highstock one, the value is added at the end, at the margin of the chart

Look this two running examples:

highcharts: http://jsfiddle.net/Jy83b/

highstock: http://jsfiddle.net/mE6XM/

I need the highcharts behaviour in the highstock chart.. How can I solve it?

Thank you!

highcharts code

  var pippo = (new Date()).getTime() + 60000;
  $(function () {
      $(document).ready(function() {
          Highcharts.setOptions({
              global: {
                  useUTC: false
              }
          });

          var chart;
          $('#container').highcharts({
              chart: {
                  type: 'spline',
                  animation: Highcharts.svg, // don't animate in old IE
                  marginRight: 10,
                  events: {
                      load: function() {

                          // set up the updating of the chart each second
                          var series = this.series[0];
                          setInterval(function() {
                              var x = (new Date()).getTime(), // current time
                                  y = Math.random();
                              series.addPoint([x, y], true, false);
                          }, 1000);
                      }
                  }
              },
              title: {
                  text: 'Live random data'
              },
              xAxis: {
                  type: 'datetime',
                  tickPixelInterval: 150,
                  max: pippo
              },
              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: false
              },
              exporting: {
                  enabled: false
              },
              series: [{
                  name: 'Random data',
                  data: (function() {
                      // generate an array of random data
                      var data = [],
                          time = (new Date()).getTime(),
                          i;

                      for (i = -19; i <= 0; i++) {
                          data.push({
                              x: time + i * 1000,
                              y: Math.random()
                          });
                      }
                      return data;
                  })()
              }]
          });
      });

  });

highstock code

  var pippo = (new Date()).getTime() + 60000;
  $(function () {
      $(document).ready(function() {
          Highcharts.setOptions({
              global: {
                  useUTC: false
              }
          });

          var chart;
          $('#container').highcharts('StockChart',{
              chart: {
                  type: 'spline',
                  animation: Highcharts.svg, // don't animate in old IE
                  marginRight: 10,
                  events: {
                      load: function() {

                          // set up the updating of the chart each second
                          var series = this.series[0];
                          setInterval(function() {
                              var x = (new Date()).getTime(), // current time
                                  y = Math.random();
                              series.addPoint([x, y], true, false);
                          }, 1000);
                      }
                  }
              },
              title: {
                  text: 'Live random data'
              },
        exporting: {
            enabled: false
        },
        rangeSelector: {
              enabled: false
        },
        navigator : {
            enabled : false
        },
          scrollbar : {
            enabled : false
        },
        legend : {
            enabled : false
        },
              xAxis: {
                  type: 'datetime',
                  tickPixelInterval: 150,
                  max: pippo
              },
              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: false
              },
              exporting: {
                  enabled: false
              },
              series: [{
                  name: 'Random data',
                  data: (function() {
                      // generate an array of random data
                      var data = [],
                          time = (new Date()).getTime(),
                          i;

                      for (i = -19; i <= 0; i++) {
                          data.push({
                              x: time + i * 1000,
                              y: Math.random()
                          });
                      }
                      return data;
                  })()
              }]
          });
      });

  });

Solved it!

Added ordinal: false to xAxis

Add ordinal: false to your xAxis settings:

xAxis: {
            type: 'datetime',
            tickPixelInterval: 150,
            max: pippo,
            ordinal: false 
        },

Then you'll have the same behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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