【问题标题】:Set HighChart yAxis's min max to a specific series only仅将 HighChart yAxis 的最小最大值设置为特定系列
【发布时间】:2018-09-09 04:51:31
【问题描述】:

我有一个股价烛台系列和一个与烛台系列共享 yAxis 的线系列。默认情况下,API 自动将图表的 yAxis 范围设置为两个系列的 min/max(candlestick series, line series) 的 min/max

但我想展示烛台系列比折线图更重要。因此,图表的 yAxis min/max 应该仅限于烛台系列,即使它切断了线系列。

当图表动态滚动时,yAxis min/max 应该调整。

我该怎么做?

jsfiddle simple code

var chartData = [
  [1473687000000, 102.65, 105.72, 102.53, 105.44],
  [1473773400000, 107.51, 108.79, 107.24, 107.95],
  [1473859800000, 108.73, 113.03,  108.6, 111.77],
  [1473946200000, 113.86, 115.73, 113.49, 115.57]
];
    
var avgData = [
  [1473687000000, 250],
  [1473773400000, 300],
  [1473859800000, 280],
  [1473946200000, 290]
];

$(function() {
  Highcharts.stockChart('container', {
    // title: {text: '---'},
    rangeSelector: {
      buttons: [
        // { type: 'hour', count: 1, text: '1h' },
        { type: 'day', count: 1, text: '1d' },
        // { type: 'all', count: 1, text: 'All' }
      ],
      selected: 1,
      //inputEnabled: true
    },
    xAxis: [{
      type: 'datetime',
    
    }],
    yAxis: [{
      labels: {
        align: 'right',
        x: -3
      },
      title: {text: 'OHLC'},
      height: '100%',
      lineWidth: 2,
      resize: {
        enabled: true
      }
    }], 

    plotOptions: {
      candlestick: {
        downColor: 'blue',
        upColor: 'red',
        dataGrouping: {
          enabled: false,
        }
      }
    },

    series: [{
      name: 'ohlc',
      type: 'candlestick',
      data: chartData,
    }, {
      name: 'avg',
      type: 'line',
      data: avgData,
    }]
  });
});
dic#container {height: 100%; width: 100%;}
<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="//code.highcharts.com/stock/highstock.js"></script>
<script src="//code.highcharts.com/stock/modules/drag-panes.js"></script>
<script src="//code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container"></div>

【问题讨论】:

  • 您能否添加有问题的脚本以及一些示例数据?
  • @MartinZeitler 我添加了一个 jsfiddle 链接并简化了数据。

标签: javascript highcharts candlestick-chart


【解决方案1】:

在 'afterDrawChartBox' 事件中,您可以使用setExtremes 方法,其最小值和最大值来自candlestick 系列:

Highcharts.addEvent(Highcharts.Chart, 'afterDrawChartBox', function(e) {
    var candlestick = this.series[0];
    this.yAxis[0].setExtremes(candlestick.dataMin, candlestick.dataMax, true, false);
});

现场演示:https://jsfiddle.net/BlackLabel/520km1wv/

API 参考:https://api.highcharts.com/class-reference/Highcharts.Axis#setExtremes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 2017-09-17
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2017-07-28
    • 2011-10-14
    相关资源
    最近更新 更多