在值为空时显示数据标签中的N / A - Highcharts [英] Show N/A in datalabels, when value is null - Highcharts

查看:530
本文介绍了在值为空时显示数据标签中的N / A - Highcharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据为空时,我想在列图中显示N / A。
这就是我目前所要做的:
http://jsfiddle.net/ 90 amxpc1/1/

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

但是,它不工作。我想达到的是类似于接受此问题的答案:
Highcharts:在数据标签中返回N / A值而不是0%
或此小提琴: http://jsfiddle.net/v5vJR/3/

我尝试修改列图,但它没有解决。

解决方案

$ bar(反转图表)和柱形图只有不同的逻辑,例如列:

  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 //填充
});

} else {
return this.y;


code
$ b现场演示

注意:

您可能希望存储 text 变量,以便在将图表隐藏一系列或缩放到图表时删除该文本。


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.

解决方案

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.

这篇关于在值为空时显示数据标签中的N / A - Highcharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆