echarts icon indicating copy to clipboard operation
echarts copied to clipboard

[Feature] Better integration of time axis and axisPointer label formatter

Open wherget opened this issue 9 months ago • 0 comments

What problem does this feature solve?

I use time axes. My datasets usually have a granularity of days. When there are only few entries in a dataset, the default axisPointer label apparently chooses the second formatter for lack of a better range. That suggests to the viewer a higher granularity in the data than is actually present.

You currently have very nice functionality to specify the formatting of dates for axis ticks in axisLabel.formatter. It would be great if there was a string-formatter way to use that syntax for the axisPointer.label.formatter as well.

As it stands I think I would have to write a custom-code formatter to call TimeScale.getFormattedLabel() myself.

What does the proposed API look like?

From a cursory glance through the source I think there are several ways to go about implementing somthing of sorts. Minimally, I think I might be satisfied with being able to ~~specify the default granularity of the TimeScale:~~ have precision percolate appropriately:

//...
    getLabel(tick: TimeScaleTick, opt?: {precision?: string}): string {
        const useUTC = this.getSetting('useUTC');
        const chosenFormat = opt?.precision || getDefaultFormatPrecisionOfInterval(getPrimaryTimeUnit(this._minLevelUnit));
        return format(
            tick.value,
            fullLeveledFormatter[chosenFormat] || fullLeveledFormatter.second,
            useUTC,
            this.getSetting('locale')
        );
    }

leading to

option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      label: {
        precision: "day"
      }
    }
  },
  xAxis: {
    type: 'time',
  },
 //...
}

Ideally I could just use leveledFormat() syntax in the axisPointer.label.formatter, if the triggering axis was a time axis:

option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      label: {
        formatter: "{yyyy}-{MM}-{dd}"
      }
    }
  },
  xAxis: {
    type: 'time',
  },
 //...
}

Couldn't that be achieved by using makeLabelFormatter in getValueLabel, that seems to cover most of the functionality anyway?

wherget avatar May 26 '25 16:05 wherget