javascript - 当系列名称没有归因于它的值时隐藏类别 highchart

标签 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]
    }
]
});

谢谢你的帮助

最佳答案

您可以使用 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

关于javascript - 当系列名称没有归因于它的值时隐藏类别 highchart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324474/

相关文章:

javascript - 从街景中的像素获取航向和俯仰

javascript - 用于检查文本日期格式的正则表达式未按预期工作

javascript - 隐藏系列中的特定点

javascript - Highcharts 同步图表以在 snap 为 false 时显示十字准线

带有 angular2-highcharts 的 JSON 数据

javascript - JS Async/Await 不能与 forEach 结合使用

javascript - 如何增加FCKEditor文本区域的高度?

python - 使用 Django,如何从父类(super class)对象实例构造代理对象实例?

python - 使用 google appengine parent 属性将默认权限应用于对象是个好主意吗?

python - 神经网络的加权输入