从HIGHCHART中删除零值

7
我一直在使用Highcharts,遇到了一个问题。问题是当图表上某些数据没有任何值时,它会显示0,这看起来很糟。请查看以下jsfiddle链接,图表上的标签由以下函数填充,但我无法对其进行检查,以便仅在图表上显示那些值大于零的柱形。 http://jsfiddle.net/CzHyC/3/ [请检查图表上的“苹果”部分]
$(function () {
    var chart;
    $(document).ready(function() {
        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: [0, 3, 4, 7, 2]
            }, {
                name: 'Jane',
                data: [2, 2, 3, 2, 1]
            }, {
                name: 'Joe',
                data: [3, 4, 4, 2, 5]
            }]
        });
    });

});
1个回答

19

这是我处理该情况的方式。

当您创建数据点时,不要使用0,而应使用null。

例如,您的数据数组看起来像这样:

[null,3,4,7,2] 而不是 [0,3,4,7,2]

Fiddle


不错的技巧,但是我的图表数据是从数据库中填充的,不过我可以对其进行检查。 - soft genic
是的,在将数据交给highcharts之前,您需要以特定格式构建您的数据。 - c0deNinja
我会将值设置为null。请记住,-1是一个值,而null不是。 - wergeld

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接