如何在HIGHCHARTS中的一个系列中包含两个数据?

时间:2013-02-19 21:16:24

标签: highcharts

我正在使用highcharts stacked和group column来制作以下图表:

IMAGE

我无法使其正常工作,我有以下代码:

$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'column'
            },
            title: {
                text: 'Types of answer'
            },
            subtitle: {
                text: 'SORT BY: Ages'
            },
            xAxis: {
                categories: ['First Test','Second Test','Third Test']
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Numero de pacientes'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -150,
                y: -13,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                borderColor: '#CCC',
                borderWidth: 1,
                shadow: false
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b><br/>'+
                        this.series.name +': '+ this.y +'<br/>'+
                        'Total: '+ this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                    }
                }
            },
            series: [{
                name: 'Answer 1',
                data: [variable1, variable7, variable13],
                stack: 'Less than 18',
                data: [variable19, variable25, variable31],
                stack: 'More than 18'
            },{
                name: 'Answer 2',
                data: [variable2, variable8, variable14],
                stack: 'Less than 18',
                data: [variable20, variable26, variable32],
                stack: 'More than 18'
            },{
                name: 'Answer 3',
                data: [variable3, variable9, variable15],
                stack: 'Less than 18',
                data: [variable21, variable27, variable33],
                stack: 'More than 18'
            }]
        });
    });

});

我不想使用另一个系列,因为我不希望它们出现在图例中,我希望图例只列出

3 个答案:

答案 0 :(得分:3)

您可以将多个系列链接到一个图例项,以隐藏和显示系列中未使用linkedTo属性的内容 -

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/arearange-line/

series: [{
    name: 'Temperature',
    data: averages,
    zIndex: 1,
    marker: {
        fillColor: 'white',
        lineWidth: 2,
        lineColor: Highcharts.getOptions().colors[0]
    }
}, {
    name: 'Range',
    data: ranges,
    type: 'arearange',
    lineWidth: 0,
    linkedTo: ':previous',
    color: Highcharts.getOptions().colors[0],
    fillOpacity: 0.3,
    zIndex: 0
}]

答案 1 :(得分:1)

您需要分离系列中的堆栈组。你的系列看起来应该类似于:

series: [ {
            name: 'Answer 1',
            data: [variable1, variable7, variable13],
            stack: 'Less than 18'
        },
        {
            name: 'Answer 1',
            data: [variable19, variable25, variable31],
            stack: 'More than 18'
        },
        {
            name: 'Answer 2',
            data: [variable2, variable8, variable14],
            stack: 'Less than 18'
        },
        {
            name: 'Answer 2',
            data: [variable20, variable26, variable32],
            stack: 'More than 18'
        },
        {
            name: 'Answer 3',
            data: [variable3, variable9, variable15],
            stack: 'Less than 18'
        },
        {
            name: 'Answer 3',
            data: [variable21, variable27, variable33],
            stack: 'More than 18'
        }
        ]

答案 2 :(得分:0)

在这里您可以找到示例,如何使用legendItemClick将两个系列连接到一个图例项目:http://jsfiddle.net/5m9JW/326/