如何使用Highcharts创建组合图

How to create combo chart with Highcharts

本文关键字:创建组 组合图 创建 Highcharts 何使用      更新时间:2023-09-26

需要创建一个类似于您在这里看到的图表:http://imgbin.org/index.php?page=image&id=20802。到目前为止,我们成功地制作了一个显示两个图表的jsfiddle,但它还远远不够完整。

如何正确地将底部图表(列)放在第一个图表上,以达到您在示例中看到的效果?http://jsfiddle.net/e106L47h/6/

$(function () {
    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {
        // split the data set into ohlc and volume
        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
            ]);
            volume.push([
                data[i][0], // the date
                data[i][5] // the volume
            ]);
        }

        // create the chart
        $('#container').highcharts('StockChart', {
            navigator: {
                enabled: false
            },
            rangeSelector: {
                selected: 1,
                inputEnabled: false
            },
            credits: {
                enabled: false
            },           
            yAxis: [{
                height: '60%',
                lineWidth: 0
            }, {
               top: '65%',
                height: '35%',
                offset: 0,
                lineWidth: 0,
               // gridLineWidth: 0,
                labels:
                {
                  //enabled: false
                }
            }],
           series: [{
                type: 'candlestick',
                name: 'AAPL',
                data: ohlc,
                dataGrouping: {
                    units: groupingUnits
                }
            }, {
                type: 'column',
                name: 'Volume',
                data: volume,
                yAxis: 1,
                dataGrouping: {
                    units: groupingUnits
                }
            }]
        });
    });
});

我会删除高度35%/65%的分割,并显示具有相同基线的两个系列。

然后我会隐藏音量轴以减少混乱。

您可以通过设置第二个隐藏轴的最大值而不是高度来修改音量条的高度(我使用了maxValue*3来近似35%的高度值)。

在卷序列中查找最大值,如下所示:

    var maxVolume = Math.max.apply(Math, volume.map(function(v) { return v[1]}))

示例:http://jsfiddle.net/cvezpup7/