本文介绍了如何在Highcharts动态地改变y轴的单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个用于y轴的格式化程序:

  yAxis:{
title:{
text:'自定义标题'
},
标签:{
formatter:function(){
var maxElement; //我想将它设置为y轴标签处的最大值

if(maxElement> gb){
return(this .value / gb).toFixed(1)+GB;
} else if(maxElement> mb){
return(this.value / mb).toFixed(1)+MB;
} else if(maxElement> kb){
return(this.value / kb).toFixed(1)+KB;
} else {
return(this.value)+B;
}
}
},
...

我在这里问了一个问题: 。


How can I change the unit at y axis dynamically at Highcharts?

I have a formatter for y axis:

 yAxis:{
        title:{
            text:'Custom Title'
        },
        labels: {
            formatter: function () {
                var maxElement;//I want to set it to max value at y axis labels

                if(maxElement > gb){
                    return (this.value / gb).toFixed(1) + " GB";
                }else if(maxElement > mb){
                    return (this.value / mb).toFixed(1) + " MB";
                }else if(maxElement > kb){
                    return (this.value / kb).toFixed(1) + " KB";
                } else {
                    return (this.value) + " B";
                }
            }
        },
      ...

I asked a question here: How can I get the max value of a y axis at highcharts? about how to get the max value at y axis labels. However I think I should have sth like beforeLoad and beforeRedraw methods.

How can I change dynamically units of y axis labels (because if you disable/close series at right side max y axis label changes)?

Other solutions about my problem is welcome.

解决方案

This case you can get the max by the following line:
this.axis.max;

So your function should be:

formatter: function() {
    var maxElement = this.axis.max;
    if (maxElement > gb) {
        return (this.value / gb).toFixed(1) + " GB";
    } else if (maxElement > mb) {
        return (this.value / mb).toFixed(1) + " MB";
    } else if (maxElement > kb) {
        return (this.value / kb).toFixed(1) + " KB";
    } else {
        return (this.value) + " B";
    }
}​

You can see it working here.

这篇关于如何在Highcharts动态地改变y轴的单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 03:29