Skip to content

Commit 80a1d9d

Browse files
committed
don't call value formatter on categories
1 parent be9fa7d commit 80a1d9d

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/core/__tests__/chart-core-axes.test.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,17 @@ describe("CoreChart: axes", () => {
250250
});
251251
});
252252

253-
test("uses custom category axes formatters", () => {
254-
const valueFormatter = (value: null | number) => ["a", "b"][value!].toUpperCase();
253+
test("does not call custom formatter on categorical values", () => {
254+
const valueFormatter = vi.fn();
255255
renderChart({
256256
highcharts,
257257
options: {
258258
series,
259-
xAxis: { type: "category", title: { text: "X" } },
260-
yAxis: { type: "category", title: { text: "Y" } },
259+
xAxis: { type: "category", categories: ["A", "B", "C"], title: { text: "X" }, valueFormatter },
260+
yAxis: { type: "category", categories: ["A", "B", "C"], title: { text: "Y" }, valueFormatter },
261261
},
262262
});
263-
getAxisOptionsFormatters().forEach((formatter) => {
264-
expect(formatter.call(mockAxisContext({ type: "category", valueFormatter, value: 0 }))).toBe("A");
265-
expect(formatter.call(mockAxisContext({ type: "category", valueFormatter, value: 1 }))).toBe("B");
266-
});
263+
expect(valueFormatter).not.toHaveBeenCalled();
267264
});
268265

269266
test("uses custom datetime axes formatters", () => {

src/core/formatters.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export function getFormatter(axis?: Highcharts.Axis) {
1818
if (!axis) {
1919
return `${value}`;
2020
}
21+
if (axis.options.type === "category") {
22+
return axis.categories?.[value] ?? value.toString();
23+
}
2124
const axisOptions = axis.userOptions as InternalXAxisOptions | InternalYAxisOptions;
2225
if (axisOptions.valueFormatter) {
2326
return axisOptions.valueFormatter(value);
2427
}
25-
if (axis.options.type === "category") {
26-
return axis.categories?.[value] ?? value.toString();
27-
}
2828
if (axis.options.type === "datetime") {
2929
const extremes = axis.getExtremes();
3030
const formatter = getDefaultDatetimeFormatter([extremes.dataMin, extremes.dataMax]);

0 commit comments

Comments
 (0)