(axis: Axis)
| 177 | * return: {string} label string. |
| 178 | */ |
| 179 | export function makeLabelFormatter(axis: Axis): (tick: ScaleTick, idx?: number) => string { |
| 180 | const labelFormatter = axis.getLabelModel().get('formatter'); |
| 181 | |
| 182 | if (axis.type === 'time') { |
| 183 | const parsed = parseTimeAxisLabelFormatter(labelFormatter as TimeAxisLabelFormatterOption); |
| 184 | return function (tick: ScaleTick, idx: number) { |
| 185 | return (axis.scale as TimeScale).getFormattedLabel(tick, idx, parsed); |
| 186 | }; |
| 187 | } |
| 188 | else if (zrUtil.isString(labelFormatter)) { |
| 189 | return function (tick: ScaleTick) { |
| 190 | // For category axis, get raw value; for numeric axis, |
| 191 | // get formatted label like '1,333,444'. |
| 192 | const label = axis.scale.getLabel(tick); |
| 193 | const text = labelFormatter.replace('{value}', label != null ? label : ''); |
| 194 | return text; |
| 195 | }; |
| 196 | } |
| 197 | else if (zrUtil.isFunction(labelFormatter)) { |
| 198 | if (axis.type === 'category') { |
| 199 | return function (tick: ScaleTick, idx: number) { |
| 200 | // The original intention of `idx` is "the index of the tick in all ticks". |
| 201 | // But the previous implementation of category axis do not consider the |
| 202 | // `axisLabel.interval`, which cause that, for example, the `interval` is |
| 203 | // `1`, then the ticks "name5", "name7", "name9" are displayed, where the |
| 204 | // corresponding `idx` are `0`, `2`, `4`, but not `0`, `1`, `2`. So we keep |
| 205 | // the definition here for back compatibility. |
| 206 | return (labelFormatter as AxisLabelCategoryFormatter)( |
| 207 | getAxisRawValue<true>(axis, tick), |
| 208 | tick.value - axis.scale.getExtent()[0], |
| 209 | null // Using `null` just for backward compat. |
| 210 | ); |
| 211 | }; |
| 212 | } |
| 213 | const scaleBreakHelper = getScaleBreakHelper(); |
| 214 | return function (tick: ScaleTick, idx: number) { |
| 215 | // Using `null` just for backward compat. It's been found that in the `test/axis-customTicks.html`, |
| 216 | // there is a formatter `function (value, index, revers = true) { ... }`. Although the third param |
| 217 | // `revers` is incorrect and always `null`, changing it might introduce a breaking change. |
| 218 | let extra: AxisLabelFormatterExtraParams | null = null; |
| 219 | if (scaleBreakHelper) { |
| 220 | extra = scaleBreakHelper.makeAxisLabelFormatterParamBreak(extra, tick.break); |
| 221 | } |
| 222 | return (labelFormatter as AxisLabelValueFormatter)( |
| 223 | getAxisRawValue<false>(axis, tick), |
| 224 | idx, |
| 225 | extra |
| 226 | ); |
| 227 | }; |
| 228 | } |
| 229 | else { |
| 230 | return function (tick: ScaleTick) { |
| 231 | return axis.scale.getLabel(tick); |
| 232 | }; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | export function getAxisRawValue<TIsCategory extends boolean>(axis: Axis, tick: ScaleTick): |
no test coverage detected
searching dependent graphs…