简体   繁体   中英

How to get access to previous value on formatter of xAxis label , Highcharts?

I need to format the label(date format) depending on current value and "the previous value" as well as if it is the first value.

I debug into this on format callback, I could use this.value for "the current value" , this.isFirst for the "if it is the first value".

Then what about the previous value? Can I get access to it at the format callback?

xAxis: {
            categories: [20141231, 20150101, 20150102],

            labels: {
                formatter: function () {
                    return ....//how to get access to the previous value?
                }
            }
        },

The simplest way is get category index of current label and refer to index-1 to extract previous name.

labels:{
            formatter:function() {
                var cat = this.axis.categories,
                    output = this.value,
                    index, prevCat;

                if(!this.isFirst) {                        
                   index = cat.indexOf(this.value),
                   prevCat = cat[index-1];
                   output += ' prev: ' + prevCat; 
                }

                return output;
            }
        }

Example: http://jsfiddle.net/d13x1xxb/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM