javascript - 如果列只有一项,如何删除标签

标签 javascript jquery html charts highcharts

我有堆栈 Highcharts ,如果列只有一个堆栈值,我想隐藏标签文本。

在本例中,我想隐藏列中的Board文本。

enter image description here

我尝试在 dataLabels 格式化程序中进行处理,但没有成功:

dataLabels: {
        formatter: function() {
          console.log(this);
          debugger;
          let countValue = this.series.userOptions.data.filter(x => x != 0).length;
          if(countValue <= 1){
             return '';
          }
          return this.series.name;
        },

这是我的源代码

JSFiddle https://jsfiddle.net/viethien/ak2h1sfq/11/

谢谢

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        categories: ['A', 'B', 'C', 'D', 'E']
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total fruit consumption'
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                textOutline: false,
                color: ( // theme
                    Highcharts.defaultOptions.title.style &&
                    Highcharts.defaultOptions.title.style.color
                ) || 'gray'
            }
        }
    },
    legend: {
        align: 'center',
        x: 0,
        verticalAlign: 'bottom',
        y: 5,
        
        backgroundColor:
            Highcharts.defaultOptions.legend.backgroundColor || 'white',
        borderColor: '#CCC',
        borderWidth: 0,
        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
            }
        },
         series: {
      //stacking: 'normal',
      dataLabels: {
        formatter: function() {
          console.log(this);
          debugger;
          let countValue = this.series.userOptions.data.filter(x => x != 0).length;
          if(countValue <= 1){
             return '';
          }
          return this.series.name;
        },
        enabled: true,
        //allowOverlap: true,
        //align: 'right',
        color: '#444',
        textOutline: false,
        shadow: false,
        //x:-50,
        style: {
          fontSize: "8px",
          textShadow: "0px"
        }
      },
      //pointPadding: 0.1,
      pointWidth: 50,
      groupPadding: 0.2,
      stacking: 'normal',
      //colorByPoint: true,
      //showInLegend: false
    }
    },
    series: [{
        name: 'Component',
        data: [4, 7, 3],
        stack: 'Forecast'
    }, {
        name: 'Module',
        data: [3, 2, 2],
        stack: 'Forecast'
    },
    {
        name: 'Board',
        data: [5, 5, 3, 2],
        stack: 'Forecast'
    },
    {
        name: 'Component',
        data: [6, 4, 5, 8],
        stack: 'Real'
    }, {
        name: 'Module',
        data: [3, 3, 4, 3],
        stack: 'Real'
    },
    {
        name: 'Board',
        data: [4, 6, 6, 4],
        stack: 'Real'
    }
 ]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

<figure class="highcharts-figure">
    <div id="container"></div>
</figure>

最佳答案

您可以将ytotal进行比较,如果它们相等,则该列只有一个堆栈。像这样的东西:

formatter: function() {
  if(this.y == this.total){
    return '';
  }
  return this.series.name;
}

JSfiddle

编辑:如果您将 0 作为数据中的值传递,则条件将为 if(!this.y || this.y == this.total)...

JSfiddle

关于javascript - 如果列只有一项,如何删除标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61241417/

相关文章:

html - 大数据表添加scroll-x并改变页面宽度

javascript - 清除两个标签之间的内容

javascript - JQuery:图像无法在图像标签中绑定(bind)

javascript - 在给定字符串中找到最佳子串集

jquery - 迭代 JSON 数组 : Get name of the parameter

jquery - 使用 Flexslider 播放 Youtube 视频

javascript - 限制 HTML 输入只允许粘贴

javascript - 用零填充时无法使 JavaScript 循环工作

javascript - 如何在 PHP 后获取 td 值?

jquery - 如何使鼠标指针仅悬停在数据表中的特定列上