How can I change the unit at y axis dynamically at

2019-04-21 21:24发布

问题:

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.

回答1:

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.