You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在angular-highcharts中格式化DataLabels并隐藏0值标签

实现仅非0数据值显示dataLabels的配置修改

要让图表仅在数据值不为0时显示dataLabels,不展示0值,可以通过自定义dataLabels的formatter函数来实现,修改后的配置代码如下:

plotOptions: {
  series: {
    states: {
      inactive: {
        opacity: 1,
      },
    },
    dataLabels: {
      enabled: true,
      inside: true,
      // 自定义格式化函数,仅非0值显示标签
      formatter: function() {
        return this.y !== 0 ? this.y : '';
      },
      showInLegend: false,
    },
    stacking: 'normal',
  },

  bar: {
    stacking: 'normal',
    borderWidth: 0,
    states: {
      hover: {
        enabled: false,
      },
    },
  },
},

关键修改说明:

  • 替换原有的format: '{y}'为formatter函数,通过逻辑判断控制标签内容
  • 当数据值this.y不等于0时,返回该数值作为标签内容;等于0时返回空字符串,从而隐藏0值的dataLabels

内容的提问来源于stack exchange,提问作者naman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:30:51