当系列名称没有可归因于其的值时,隐藏类别highchart

时间:2019-08-02 10:18:16

标签: javascript python highcharts

出于演示目的,我在下面简化了图表,但我有很多类别,但并非所有系列名称都具有这些类别的值。因此,当我选择该系列名称时,我将如何使0值类别消失。

例如,在下面的示例中,选择人员1时,服务1类别应消失而不是不加限制

Highcharts.chart('container', {
chart : {type: 'column'},

xAxis: {
    categories: ["service1", "service2", "service3", "service"] ,
    showEmpty : true ,
    ordinal: false
    },

series: [{
    name: 'person1',
    data: [0,2,3],

    },
{   name : 'person2',
        data: [10,6,5]
}]
});

链接到代码https://jsfiddle.net/uroepk1j/

JSFiddle中的ppotaczek的代码

Highcharts.chart('container', {
chart: {
    type: 'column',
    ignoreHiddenSeries: true
},
plotOptions: {
    column: {

        pointPlacement: null,
        events: {
            legendItemClick: function() {
                var points = this.data,
                    hideCategory = false,
                    breaks = [],
                    stop,
                    series = this.chart.series;

                this.chart.xAxis[0].update({
                    breaks: []
                });

                this.visible = !this.visible;

                points.forEach(function(p, i) {
                    stop = false;

                    series.forEach(function(s) {

                        if (!stop && (!s.visible || s.data[i].y === 0)) {
                            hideCategory = true;
                        } else {
                            stop = true;
                            hideCategory = false;
                        }
                    }, this);

                    if (hideCategory) {
                        breaks.push({
                            from: i - 0.5,
                            to: i + 0.5,
                            breakSize: 0
                        })
                    }

                    hideCategory = false;
                }, this);

                this.visible = !this.visible;

                this.chart.xAxis[0].update({
                    breaks: breaks
                });
            }
        }
    },
},
xAxis: {
    categories: ['Col 1', 'Col 2', 'Col 3']
},
series: [{
        name: 'person1',
        data: [2, 0, 3],

    },
    {
        name: 'person2',
        data: [10, 1, 5]
    }
]
});

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您可以使用broken-axis模块,并在没有点的类别中插入中断,例如:

plotOptions: {
    column: {
        grouping: false,
        pointPlacement: null,
        events: {
            legendItemClick: function() {
                if (!this.visible) {
                    breaks[this.index] = {}
                    this.chart.xAxis[0].update({
                        breaks: breaks
                    });
                } else {
                    breaks[this.index] = {
                        from: this.xData[0] - 0.5,
                        to: this.xData[0] + 0.5,
                        breakSize: 0
                    }
                    this.chart.xAxis[0].update({
                        breaks: breaks
                    });
                }
            }
        }
    },
}

实时演示: http://jsfiddle.net/BlackLabel/4utq7e3n/

API参考: https://api.highcharts.com/highcharts/xAxis.breaks

相关问题