如何在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
相关产品推荐
相关产品推荐

