highcharts 如何设置角高图中数据标签的格式

im9ewurl  于 2022-11-10  发布在  Highcharts
关注(0)|答案(2)|浏览(144)

我希望仅在dataLabels的值不为0时才显示它。并且不希望在UI图表上显示0。下面提到了ts代码:

plotOptions: {
    series: {
      states: {
        inactive: {
          opacity: 1,
        },
      },
      dataLabels: {
        enabled: true,
        inside: true,
        format: '{y}',
        showInLegend: false,
      },
      stacking: 'normal',
    },

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

i2byvkas1#

使用格式功能

plotOptions: {
        series: {
          states: {
            inactive: {
              opacity: 1,
            },
          },
          dataLabels: {
            enabled: true,
            inside: true,
            format: function(){
               return (this.y != 0) ? this.y : "";
            },
            showInLegend: false,
          },
          stacking: 'normal',
        },

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

ugmeyewa2#

数据标签需要使用formatter函数,例如:

dataLabels: {
    ...,
    formatter: function() {
      return this.y || '';
    }
  }

现场演示:http://jsfiddle.net/BlackLabel/w82kr9fx/
API引用:https://api.highcharts.com/highcharts/series.bar.dataLabels.formatter

相关问题