echarts icon indicating copy to clipboard operation
echarts copied to clipboard

[Feature] Forward the axis object to formatter functions

Open ob-ARC opened this issue 3 years ago • 7 comments

What problem does this feature solve?

In a formatter callback function, it is currently not possible to change the returned format depending on the axis' state, since it is not known for which axis the formatting is requested. Use case: Only show mSecs on a date/time axis if the axis is zoomed in enough (i.e. the range is below a certain threshold).

What does the proposed API look like?

The solution would simply be to let getFormattedLabel forward "this" (= the axis to label) through leveledformat to the user's callback function.

ob-ARC avatar Jan 25 '22 10:01 ob-ARC

Do you mean time axis?

Ovilia avatar Jan 27 '22 05:01 Ovilia

Yes, my use case was a time axis. But I guess this feature could be useful for other axis types as well?

ob-ARC avatar Jan 27 '22 06:01 ob-ARC

I think maybe listening to dataZoom changes and call setOption may be a better idea. Even if the axis is provided in the label formatter, it will not be called when dataZoom changes.

Ovilia avatar Jan 27 '22 08:01 Ovilia

OK, but that would still leave me the problem that in the formatter callback itself, I would neither know which axis is being labelled, nor what the current state of the axis is. Or do you mean I should set a different callback function to the chart's option each time dataZoom changes?

ob-ARC avatar Jan 27 '22 08:01 ob-ARC

I definitely agree that this would offer a lot of flexibility, I've tried using 'time' axis but ran into a few issues (#14305, #16685) where it made sense to change back to 'category'

sjcobb avatar May 13 '22 13:05 sjcobb

I am once again confronted with the issue that I do not know which axis is being labelled in the formatter function. Is there any news/workaround for this?

ob-ARC avatar Apr 16 '24 07:04 ob-ARC

From the current codebase, it looks to be totally easy and also no compatibility issue to simply add the axis to the parameters given to the callback function in makeLabelFormatter(), where the axis in question is already present as input parameter.

ob-ARC avatar Apr 16 '24 08:04 ob-ARC