Javascript 当类别的所有条形图值等于0时,Highcharts条形图将删除类别

Javascript 当类别的所有条形图值等于0时,Highcharts条形图将删除类别,javascript,jquery,highcharts,Javascript,Jquery,Highcharts,当一个类别的所有条形图值都等于0时,如何删除该类别 见:() 如果只有一个条等于0,则不应删除该条。 但如果所有3个条都等于0,则应删除该类别 chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'column' }, title: { text: 'Stacked column chart'

当一个类别的所有条形图值都等于0时,如何删除该类别

见:()

如果只有一个条等于0,则不应删除该条。 但如果所有3个条都等于0,则应删除该类别

chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        title: {
            text: 'Stacked column chart'
        },
        xAxis: {
            categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Total fruit consumption'
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'blue'
                }
            }
        },
        legend: {
            align: 'right',
            x: -100,
            verticalAlign: 'top',
            y: 20,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'gray',
            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: 'John',
            data: [5, 3, 4, 7, 2]
        }, {
            name: 'Jane',
            data: [2, 2, 3, 2, 1]
        }, {
            name: 'Joe',
            data: [3, 4, 4, 2, 5]
        }]
    });
});
chart=新高度图表。图表({
图表:{
renderTo:'容器',
类型:“列”
},
标题:{
文本:“堆叠柱形图”
},
xAxis:{
类别:[‘苹果’、‘橘子’、‘梨’、‘葡萄’、‘香蕉’]
},
亚克斯:{
分:0,,
标题:{
正文:“水果总消费量”
},
堆叠标签:{
启用:对,
风格:{
fontWeight:'粗体',
颜色:(Highcharts.theme&&Highcharts.theme.textColor)| |“蓝色”
}
}
},
图例:{
对齐:“右”,
x:-100,
垂直排列:“顶部”,
y:20,
浮动:是的,
backgroundColor:(Highcharts.theme&&Highcharts.theme.legendBackgroundColorSolid)| |灰色,
边框颜色:“#CCC”,
边框宽度:1,
影子:错
},
工具提示:{
格式化程序:函数(){
返回“+this.x+”
+ this.series.name+':'+this.y+'
+ “总计:”+this.point.stackTotal; } }, 打印选项:{ 专栏:{ 堆叠:“正常”, 数据标签:{ 启用:对, 颜色:(Highcharts.theme&&Highcharts.theme.dataLabelsColor)| |“白色” } } }, 系列:[{ 姓名:'约翰', 数据:[5,3,4,7,2] }, { 姓名:'简', 数据:[2,2,3,2,1] }, { 名字:'乔', 数据:[3,4,4,2,5] }] }); });
您需要在初始化图表之前过滤数据,如下所示:

var data = [{
    name: 'Year 1800',
    data: [0, 0, 0, 0, 0]
}, {
    name: 'Year 1900',
    data: [0, 156, 947, 408, 6]
}, {
    name: 'Year 2008',
    data: [0, 914, 4054, 732, 34]
}];

data = $.grep(data, function (category) {
    return $.grep(category.data, function (item) {
        return item > 0;
    }).length > 0;
});

问题在于,当您想要动态删除类别(通过setCategories函数设置类别)时,您还应该注意数据系列中的元素。换句话说,当您想要修改类别时,还应该删除数据点。在使用highcharts中的数据之前,最好先准备不带零的数据。

谢谢Sebastian,我最终按照您的建议重新构建了我的数据。。。Justin确实有一个很棒的解决方案,但我已经发布了我的代码。。。再次感谢!