堆叠的Highchart图无法正确显示0个值

时间:2020-08-28 19:03:46

标签: javascript vue.js highcharts

我正在vue应用程序中使用highchart库在图形中显示数据。下面是我所面临的问题的代码片段。问题在于数据系列中,当数据数组的第一个元素包含0值时,它在映射中显示为0,但是如果0在任何其他位置,则对于特定名称它不显示为0。 / p>

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

在上述系列数组中,John,Jane和Joe的第一个值分别为5、0、0,第二个值分别为0、0、4。但是在下面的图形中,第一列图形显示为0,第二列图形显示为0。请帮助解决此问题。我也不想在第一栏中显示那个0。

enter image description here

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
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 0, 4, 0, 2]
    }, {
        name: 'Jane',
        data: [0, 0, 3, 0, 1]
    }, {
        name: 'Joe',
        data: [0, 4, 4, 2, 5]
    }]
});
#container {
    height: 400px; 
}

.highcharts-figure, .highcharts-data-table table {
    min-width: 310px; 
    max-width: 800px;
    margin: 1em auto;
}

.highcharts-data-table table {
    font-family: Verdana, sans-serif;
    border-collapse: collapse;
    border: 1px solid #EBEBEB;
    margin: 10px auto;
    text-align: center;
    width: 100%;
    max-width: 500px;
}
.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}
.highcharts-data-table th {
    font-weight: 600;
    padding: 0.5em;
}
.highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption {
    padding: 0.5em;
}
.highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}
.highcharts-data-table tr:hover {
    background: #f1f7ff;
}
<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>
    <p class="highcharts-description">
        Chart showing stacked columns for comparing quantities. Stacked charts
        are often used to visualize data that accumulates to a sum. This chart
        is showing data labels for each individual section of the stack.
    </p>
</figure>

1 个答案:

答案 0 :(得分:0)

plotOptions: {
              column: {
                  stacking: 'normal',
                  dataLabels: {
                    enabled: true,
                    formatter: function(){
                        var val = this.y;
                        if (val === 0) {
                            return '';
                        }
                        return val;
                    },
                  }
              }
          },

这是解决问题的方法

相关问题