在HighCharts中更改音量列的颜色(高/低)

时间:2012-09-29 14:38:10

标签: highcharts highstock

我有一个简单的图表,显示下面有音量列的烛台:http://jsfiddle.net/T83Xy/

基本上,我想对列使用黑色和红色,具体取决于它是否高于打开或不高。我通过推Y:数据,颜色:'#000000'作为参数看到了一些样本。问题是我正在推送日期和卷号。我试图推X:日期,Y:数据,颜色:'#000000'但是它会抛出错误并且没有给我预期的结果。

3 个答案:

答案 0 :(得分:8)

首先,如果您有大量积分,则需要将series.turboThreshold设置为0。这将禁用输入数据格式检查。

然后,根据蜡烛应用列颜色,我建议你这段代码:

Highcharts.seriesTypes.column.prototype.pointAttribs = (function(func) {
    return function(point, state) {
      var attribs = func.apply(this, arguments);
      
      var candleSeries = this.chart.series[0]; // Probably you'll need to change the index
      var candlePoint = candleSeries.points.filter(function(p) { return p.index == point.index; })[0];

      var color = (candlePoint.open < candlePoint.close) ? '#FF0000' : '#000000'; // Replace with your colors
      attribs.fill = state == 'hover' ? Highcharts.Color(color).brighten(0.3).get() : color;
      
      return attribs;
    };
}(Highcharts.seriesTypes.column.prototype.pointAttribs));

请注意,此代码会影响当前页面上的所有图表。但您可以轻松添加一些条件,仅在特定图表上运行此选项。 Here是默认的Highstock演示,其中包含上述代码。

答案 1 :(得分:2)

这对我很有用:

$(function () {
jQuery.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {

    // split the data set into ohlc and volume
    var volumeColor = '';
    var ohlc = [],
        volume = [],
        dataLength = data.length,
        // set the allowed units for data grouping
        groupingUnits = [[
            'week',                         // unit name
            [1]                             // allowed multiples
        ], [
            'month',
            [1, 2, 3, 4, 6]
        ]],

        i = 0;

    for (i; i < dataLength; i += 1) {
        ohlc.push([
            data[i][0], // the date
            data[i][1], // open
            data[i][2], // high
            data[i][3], // low
            data[i][4] // close
        ]);
         if (i==0) {
        volumeColor = '#CCCCCC';
         } else {         
            if (data[i][1] >= data[i-1][1]) {
               volumeColor = '#006633';
            } else {
               volumeColor = '#CC0033';
            }
         }

        volume.push({
            x: data[i][0], // the date
            y: data[i][5],
            color: volumeColor
        });
    }


    // create the chart
    $('#container').highcharts('StockChart', {

        rangeSelector: {
            selected: 1
        },

        title: {
            text: 'AAPL Historical'
        },

        yAxis: [{
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'OHLC'
            },
            height: '60%',
            lineWidth: 2
        }, {
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'Volume'
            },
            top: '65%',
            height: '35%',
            offset: 0,
            lineWidth: 2
        }],

        series: [{
            type: 'candlestick',
            name: 'AAPL',
            data: ohlc,
            dataGrouping: {
                units: groupingUnits
            }
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
            turboThreshold: Number.MAX_VALUE,
            dataGrouping: {
                enabled: false,
                units: groupingUnits
            }
        }]
    });
});
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height:400px;min-width:310px"></div>

答案 2 :(得分:0)

当前(HighCharts 7+)解决方案不需要覆盖任何方法,只需根据当前烛形点的开盘价与其烛形点之间的比较,为体积点设置color属性关闭:<为绿色,>为红色,相等则为黄色。

这是一个最小的例子。

// Return a color matching the candle by comparing the open (1) and close (4)
function volumeBarColor(point) {
  if (point[1] < point[4])
    return 'green';
  if (point[1] > point[4])
    return 'red';
  return 'yellow';
}

Highcharts.getJSON('https://www.highcharts.com/samples/data/aapl-ohlcv.json', data => {

  // Split the data set into ohlc and volume
  const ohlc = [],
      volume = [];

  for (let i = 0; i < data.length; i += 1) {
    ohlc.push([
      data[i][0], // the date
      data[i][1], // open
      data[i][2], // high
      data[i][3], // low
      data[i][4], // close
    ]);

    volume.push({
      x: data[i][0], // the date
      y: data[i][5], // the volume
      color: volumeBarColor(data[i]),
    });
  }


  // Create the chart
  Highcharts.stockChart('container', {

    title: {
      text: 'AAPL Historical'
    },

    yAxis: [{
      labels: {
        align: 'right',
        x: -3
      },
      height: '60%',
    }, {
      labels: {
        align: 'right',
        x: -3
      },
      top: '65%',
      height: '35%',
      offset: 0,
    }],

    series: [{
      type: 'candlestick',
      name: 'AAPL',
      data: ohlc,
    }, {
      type: 'column',
      name: 'Volume',
      data: volume,
      yAxis: 1,
    }]
  });
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>

<div id="container"></div>