Show N/A in datalabels, when value is null - Highc

2019-04-10 11:20发布

I want to show "N/A" in a column chart, when the data is null. This is what I am currently trying to do: http://jsfiddle.net/90amxpc1/1/

dataLabels: {
                formatter: function () {
                    if (this.y == null) {
                        return "N/A";
                    }
                    else {
                        return this.y;
                    }
                }
}

But, its not working. What I want to achieve is similar to the accepted Answer to this Question: Highcharts: Returning N/A value instead of 0% in data label or this fiddle: http://jsfiddle.net/v5vJR/3/

I tried modifying it for the column chart, but it doesnt works out.

1条回答
爷的心禁止访问
2楼-- · 2019-04-10 11:40

There is just different logic for bar (inverted chart) and column chart, example for column:

            formatter: function () {
                if (this.y == null) {
                    var chart = this.series.chart,
                        categoryWidth = chart.plotWidth / chart.xAxis[0].categories.length,
                        offset = (this.point.x) * categoryWidth + categoryWidth / 2,
                        text = chart.renderer.text('N/A', -999, -999).add();

                    text.attr({
                        x: chart.plotLeft + offset - text.getBBox().width / 2, //center label
                        y: chart.plotTop + chart.plotHeight - 8 // padding
                    });

                } else {
                    return this.y;
                }
            },

Live demo: http://jsfiddle.net/90amxpc1/4/

Note:
You may want to store text variable to remove that text when hiding series or zooming into the chart.

查看更多
登录 后发表回答