3
votes

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

1
please add your solution as an answer and accept it so that the question may be closedbilal.haider

1 Answers

3
votes

Add ordinal: false to your xAxis settings:

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

Then you'll have the same behaviour.