Format maxLabel on axis differently than other labels

Viewed 234

I am hoping to use a different format specifically for the top axis label on my y-axis. For example, if I have a percentage value axis, I would like to see the % symbol only on the top axis label, while the rest of the labels beneath it remain formatted as numbers without a symbol.

So far, I've been using the formatter option and have created a function that receives value and index per the ECharts docs. The example link below does what I need it to do, but does it for the bottom of the axis rather than the top. I need a way to use this same logic, but select the maximum index on the axis.

Link to example in docs:
https://echarts.apache.org/en/option.html#yAxis.axisLabel.formatter

Code from example in docs:
The if (index === 0) is the logic I'm referring to

// Use callback function; function parameters are axis index
formatter: function (value, index) {
    // Formatted to be month/day; display year only in the first label
    var date = new Date(value);
    var texts = [(date.getMonth() + 1), date.getDate()];
    if (index === 0) {
        texts.unshift(date.getYear());
    }
    return texts.join('/');
}

Here's what I've tried:

  • I attempted to use the index argument to figure out the max index # in my dataset, but couldn't figure out how to do that given that the function is called once per value and includes only the data for that value.
  • I tried passing params to the function since that's how formatter works in the series config, but it looks like that's not possible in the axis config
  • One (unattractive) alternative is to use the y-axis axisName to display any symbols I want to have appended to the top label, and manually position it using padding. This probably wouldn't work because I'll also need to use the axisName separately from time to time.

A Possible Hint
This might not be possible yet, but at least behind the scenes I know ECharts is able to select the top label through the showMaxLabel option in axisLabel.

Is there a way to check if a label is the maximum label in the formatter function for an axis?

0 Answers
Related