javascript - 如果我们有 1 个数据,则隐藏堆积柱形图中的数据标签

标签 javascript highcharts

如果只有一个大于零的数据集,我需要隐藏内部数据标签。我所说的数据集的意思是

series: [{
        name: 'John',
        data: [5, 3, 0, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 0, 2, 0]
    }, {
        name: 'Joe',
        data: [3, 4, 3, 2, 0]
    }]

如果 series.data[i] 除 1 外全部为零,则隐藏内部数据标签。在上述情况下,第三个和第五个数据集具有 0,0,3 和 2,0,0 值,只有 1 个非零值,因此隐藏内部数据标签。

Highcharts.chart('container', {
    chart: {
        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: ( // theme
                    Highcharts.defaultOptions.title.style &&
                    Highcharts.defaultOptions.title.style.color
                ) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        backgroundColor:
            Highcharts.defaultOptions.legend.backgroundColor || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
    },
    tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                formatter:function() {
                  if(this.y != 0) {
                    return this.y;
                  }
        }
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 3, 0, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 0, 2, 0]
    }, {
        name: 'Joe',
        data: [3, 4, 3, 2, 0]
    }]
});

在这里,我突出显示了应该从内部删除哪个数据标签。 datalabels .

最佳答案

在格式化程序函数中,您可以检查系列中是否至少有两个 y 值大于 0。

plotOptions: {
  column: {
    stacking: 'normal',
    dataLabels: {
      enabled: true,
      formatter: function() {
        var series = this.series.chart.series,
          xPos = this.point.x,
          filteredSeries;

        if (this.y != 0) {
          filteredSeries = series.filter((s) => (s.yData[xPos]));

          return filteredSeries.length > 1 ? this.y : '';
        }
      }
    }
  }
}

现场演示: http://jsfiddle.net/BlackLabel/6m4e8x0y/4971/

API引用: https://api.highcharts.com/highcharts/series.column.dataLabels.formatter

关于javascript - 如果我们有 1 个数据,则隐藏堆积柱形图中的数据标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61316124/

相关文章:

javascript - 此语法与 Railscast JQuery 上传教程中的什么有关

javascript - 核心 HTML/CSS/Javascript 框架

jquery - 我想向 Highcharts 柱形图中的每一列添加一条线

javascript - Highchart 中的分段标签

javascript - 在 Highcharts 股票图表上将最后一点的值显示为标签或工具提示

javascript - 这些 var 声明是样式问题还是功能问题?

javascript - 如何计算总和?

javascript - jQuery 改变行的值

php - 使用 PHP 从 MySQL 数据生成 Highcharts 不起作用

Highcharts v6.0.0 注释不工作