如何防止 Highstock 右范围句柄在添加数据时不断滑出最大值

How do I prevent Highstock right range handle keep slipping off the max value when adding data

我有一张包含三条线的 Highstock 图表。当我添加数据时,它会在半常规情况下让最大范围手柄从右侧移开,然后保持不动,而不是像预期的那样粘在右侧。这种行为是不希望的。

滑倒之前: 滑倒后:

我做了一个简化的例子 https://jsfiddle.net/eskil_saatvedt/rdwdbht1/3/

HTML

<script src="https://code.highcharts.com/stock/highstock.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>

Javascript

$(function() {

  Highcharts.setOptions({
    global: {
      useUTC: false
    }
  });

  // Create the chart
  $('#container').highcharts('StockChart', {
    chart: {
        type: 'line',
    },

    rangeSelector: {
      buttons: [{
        count: 1,
        type: 'minute',
        text: '1M'
      }, {
        count: 5,
        type: 'minute',
        text: '5M'
      }, {
        type: 'all',
        text: 'All'
      }],
      inputEnabled: false,
      selected: 2
    },

    title: {
      text: 'Live random data'
    },

    exporting: {
      enabled: false
    },

    series: [{
      name: 'Random data',
      data: (function() {
        // generate an array of random data
        var data = [],
          time = (new Date()).getTime();
        data.push([
          time +1 * 1000,
          Math.round(Math.random() * 100)
        ]);

        return data;
      }())
    },
    {
      name: 'Random data2',
      data: (function() {
        // generate an array of random data
        var data = [],
          time = (new Date()).getTime();
        data.push([
          time +1 * 1000,
          Math.round(Math.random() * 100)
        ]);

        return data;
      }())
    },
    {
      name: 'Random data3',
      data: (function() {
        // generate an array of random data
        var data = [],
          time = (new Date()).getTime();
        data.push([
          time +1 * 1000,
          Math.round(Math.random() * 100)
        ]);

        return data;
      }())
    }
    ]
  });

});

function UpdateData() {
  var chart = $('#container').highcharts();
  var x = (new Date()).getTime(), // current time
    y = Math.round(Math.random() * 100),
    k = Math.round(Math.random() * 100)+50,
    z = Math.round(Math.random() * 100)+20;
  chart.series[0].addPoint([x, y], false, false);
  chart.series[1].addPoint([x, z], false, false);
  chart.series[2].addPoint([x, k], false, false);
  chart.redraw();
}

setInterval(function() {
  UpdateData();
}, 1000);

通常我会显示2个温度和增益,用它来控制房间的热量。

添加新范围的点后需要调用setExtremes()](http://api.highcharts.com/highcharts#Axis.setExtremes)函数。

function UpdateData() {
  var chart = $('#container').highcharts(),
  min = chart.xAxis[0].dataMin,
  max = chart.xAxis[0].dataMax,
  x = (new Date()).getTime(), // current time
    y = Math.round(Math.random() * 100),
    k = Math.round(Math.random() * 100) + 50,
    z = Math.round(Math.random() * 100) + 20;
  chart.series[0].addPoint([x, y], false, false);
  chart.series[1].addPoint([x, z], false, false);
  chart.series[2].addPoint([x, k], false, false);

  chart.xAxis[0].setExtremes(min,x);
}

示例: - https://jsfiddle.net/xh1sjk3L/

感谢塞巴斯蒂安。

通过使用 max = chart.xAxis[0].userMax 我能够将最大值设置为 max 如果它靠近侧面。在示例中 1 秒

function UpdateData() {
  var chart = $('#container').highcharts();

  var userMax = chart.xAxis[0].userMax; // <- change

  var x = (new Date()).getTime(), // current time
    y = Math.round(Math.random() * 100),
    k = Math.round(Math.random() * 100)+50,
    z = Math.round(Math.random() * 100)+20;
  chart.series[0].addPoint([x, y], false, false);
  chart.series[1].addPoint([x, z], false, false);
  chart.series[2].addPoint([x, k], false, false);  
  chart.redraw(); // Redraw it again, as the left handle position I want is most often correct after the redraw.

// ----- change --------
  var deltaX = x - userMax;
  if (deltaX < 1000){ // the handle was closer to the end than 1 second
    chart.xAxis[0].setExtremes(chart.xAxis[0].min,x);
  } 
}