如何在Highcharts的堆积柱形图中为每个类别添加不同的系列数据?

时间:2019-05-10 09:12:07

标签: javascript highcharts

在此堆积柱形图中,我想为x轴上的不同类别添加不同的系列。例如,在苹果类别中我要显示aaa = 1,bbb = 2,ccc = 3,而在橙子类别中我要显示eee = 5,fff = 4,ggg = 1等。有可能吗

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total fruit consumption'
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
    },
    tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 3, 4, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 3, 2, 1]
    }, {
        name: 'Joe',
        data: [3, 4, 4, 2, 5]
    }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

1 个答案:

答案 0 :(得分:1)

您需要设置x数据值,例如,作为数组中的第一个元素:

series: [{
    name: 'John',
    data: [
        [0, 5],
        [1, 3]
    ]
}, {
    name: 'Jane',
    data: [
        [1, 2],
        [2, 2]
    ]
}, {
    name: 'Joe',
    data: [
        [0, 3],
        [2, 4]
    ]
}]

实时演示: http://jsfiddle.net/BlackLabel/nh1toe8g/

API参考: https://api.highcharts.com/highcharts/series.column.data