简体   繁体   English

将新系列添加到 highcharts 堆积柱形图的顶部

[英]Add new series to the top of a highcharts stacked column chart

I'm trying to control the order in which new (after first render) series are added to a stacked column chart in Highcharts.我正在尝试控制将新(首次渲染后)系列添加到 Highcharts 中堆积柱形图的顺序。 Right now, if you simply use addSeries, the newly added series is added to the bottom of the stack.现在,如果您只是使用 addSeries,则新添加的系列将添加到堆栈底部。 Here's a simplified version of what I'm doing这是我正在做的事情的简化版本

var chart = new Highcharts.Chart({

    chart: {
        type: 'column',
        renderTo: 'container'
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar']
    },

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

    series: [{
        name: 'base',
        data: [10, 20, 30]
    }, {
        name: 'sec',
        data: [30, 20, 10]
    }]
});

var i = 0;
$('#add').on('click', function (e) {
    chart.addSeries({
        data: [32, 43, 42],
        name: ++i,
        index: 0 //??????
    });
});

Here's a fiddle of this: http://jsfiddle.net/6bCBf/这是一个小提琴: http : //jsfiddle.net/6bCBf/

Any one can think of a way to reverse/control where Highcharts inserts the new series?任何人都可以想出一种方法来反转/控制 Highcharts 插入新系列的位置?

I've tried setting the index of the new series to 0, but that does nothing.我试过将新系列的索引设置为 0,但这没有任何作用。 Setting it to -1 adds the new series to the bottom of the array, but then that 'stacking' does not work properly将其设置为 -1 会将新系列添加到数组的底部,但是“堆叠”无法正常工作

You can set index and zIndex to keep order between layers, then add serie with appropriate parameters.您可以设置 index 和 zIndex 以保持层之间的顺序,然后使用适当的参数添加 serie。

Example: http://jsfiddle.net/6bCBf/5/示例: http : //jsfiddle.net/6bCBf/5/

var chart = new Highcharts.Chart({

    chart: {
        type: 'column',
        renderTo: 'container'
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar']
    },

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

    series: [
        {
            name: 'base',
            data: [10, 20, 30],
            index:2,
            zIndex:10
        },
        {
            name: 'sec',
            data: [30, 20, 10],
            index:1,
            zIndex:9
        }
    ]
},function(chart){



    $('#add').on('click', function (e) {

        chart.addSeries({
            data: [32, 43, 42],
            index: 0,
            zIndex:1
        });
    });


});

Highcharts also supports a yAxis.reversedStacks property which, if set to false , will reverse the order of the stacking data points. Highcharts 还支持yAxis.reversedStacks属性,如果设置为false ,将反转堆叠数据点的顺序。 In the OP's JSFiddle, the first series, "base", appeared on the top of the chart, while the second series, "sec", appeared below it.在 OP 的 JSFiddle 中,第一个系列“base”出现在图表的顶部,而第二个系列“sec”出现在图表的下方。 Setting reversedStacks: false as in this updated JSFiddle reverses that order, and the new series appears on the top of the stack instead of the bottom.此更新的 JSFiddle 中设置reversedStacks: false反转该顺序,并且新系列出现在堆栈顶部而不是底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM