Change Color of Volume Columns (High/Low) in HighCharts

前端 未结 3 1480
别那么骄傲
别那么骄傲 2021-02-15 23:32

I\'ve got a simple chart showing candlesticks with volume columns underneath: http://jsfiddle.net/T83Xy/

Basically, I want to use black and red for the columns depending

3条回答
  •  庸人自扰
    2021-02-16 00:14

    This worked perfectly for me:

    $(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
                }
            }]
        });
    });
    });
    
    
    
    

提交回复
热议问题