Highcharts结合图表类型

时间:2018-01-26 10:25:55

标签: highcharts

我使用的是Highcharts,想要合并两种类型的图表。

我想要一个带有负堆栈的栏(将图表类型更改为列)与带有负值的列相结合,因此对于每个类别,我都有正值和负值。

我找不到这样做的任何例子,所以我甚至不知道这是否可能。

我确实考虑过对嵌套系列这样的系列做一些事情,但是再次不知道这是否可行而且找不到一个例子。

enter image description here 如果我正在尝试做什么?

Column with negative values

// Age categories
var categories = [
    '0-4', '5-9', '10-14', '15-19',
    '20-24', '25-29', '30-34', '35-39', '40-44',
    '45-49', '50-54', '55-59', '60-64', '65-69',
    '70-74', '75-79', '80-84', '85-89', '90-94',
    '95-99', '100 + '
];

Highcharts.chart('container', {
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Population pyramid for Germany, 2015'
    },
    subtitle: {
        text: 'Source: <a href="http://populationpyramid.net/germany/2015/">Population Pyramids of the World from 1950 to 2100</a>'
    },
    xAxis: [{
        categories: categories,
        reversed: false,
        labels: {
            step: 1
        }
    }, { // mirror axis on right side
        opposite: true,
        reversed: false,
        categories: categories,
        linkedTo: 0,
        labels: {
            step: 1
        }
    }],
    yAxis: {
        title: {
            text: null
        },
        labels: {
            formatter: function () {
                return Math.abs(this.value) + '%';
            }
        }
    },

    plotOptions: {
        series: {
            stacking: 'normal'
        }
    },

    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' +
                'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);
        }
    },

    series: [{
        name: 'Male',
        data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2,
            -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4,
            -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0]
    }, {
        name: 'Female',
        data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9,
            3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9,
            1.8, 1.2, 0.6, 0.1, 0.0]
    }]
});

Bar with negative stack

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Column chart with negative values'
    },
    xAxis: {
        categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },
    credits: {
        enabled: false
    },
    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]
    }]
});

1 个答案:

答案 0 :(得分:1)

感谢@Pawel Fus,我能够做我想做的事情并删除重复的图例标签,我在系列中添加了showInLegend: false,我想隐藏传奇

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Column chart with negative values'
    },
    colors: Highcharts.getOptions().colors.splice(0, 3),
    xAxis: {
        categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },
    credits: {
        enabled: false
    },
    plotOptions: {
        series: {
        stacking: true
      }
    },
    series: [{
        stack: 'john',
        name: 'John',
        data: [5, 3, 14, 7, 2]
    }, {
        stack: 'jane',
        name: 'Jane',
        data: [2, 12, 3, 2, 1]
    }, {
        stack: 'joe',
        name: 'Joe',
        data: [3, 4, 4, 2, 5]
    }, {
        showInLegend: false,
        stack: 'john',
        name: 'John',
        data: [-5, -3, -4, -7, -2]
    }, {
        showInLegend: false,
        stack: 'jane',
        name: 'Jane',
        data: [-2, -2, -3, -2, -1]
    }, {
        showInLegend: false,
        stack: 'joe',
        name: 'Joe',
        data: [-3, -4, -4, -2, -5]
    }]
});