Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 859
kamaci Avatar asked Sep 06 '12 21:09

kamaci


1 Answers

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.

like image 161
Ricardo Alvaro Lohmann Avatar answered Oct 14 '22 16:10

Ricardo Alvaro Lohmann