Skip to content

Commit 07d7e69

Browse files
[charts] Add a default value formatter for continuous scales (#18178)
1 parent eaef983 commit 07d7e69

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

packages/x-charts-pro/src/ChartZoomSlider/internals/ChartAxisZoomSliderActiveTrack.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export function ChartAxisZoomSliderActiveTrack({
330330
function getZoomSliderTooltipsText(axis: ComputedAxis, drawingArea: ChartDrawingArea) {
331331
const formatValue = (value: Date | number | null) => {
332332
if (axis.valueFormatter) {
333-
return axis.valueFormatter(value, { location: 'zoom-slider-tooltip' });
333+
return axis.valueFormatter(value, { location: 'zoom-slider-tooltip', scale: axis.scale });
334334
}
335335

336336
return `${value}`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AxisValueFormatterContext, ContinuousScaleName, D3ContinuousScale } from '../models/axis';
2+
3+
/**
4+
* Creates a default formatter function for continuous scales (e.g., linear, sqrt, log).
5+
* @returns A formatter function for continuous values.
6+
*/
7+
export function createScalarFormatter(tickNumber: number, zoomScale: D3ContinuousScale) {
8+
return function defaultScalarValueFormatter<S extends ContinuousScaleName = ContinuousScaleName>(
9+
value: any,
10+
context: AxisValueFormatterContext<S>,
11+
): string {
12+
if (context.location === 'tick') {
13+
return context.scale.tickFormat(tickNumber)(value);
14+
}
15+
16+
if (context.location === 'zoom-slider-tooltip') {
17+
return zoomScale.tickFormat(2)(value);
18+
}
19+
20+
return `${value}`;
21+
};
22+
}

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/computeAxisValue.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scaleBand, scalePoint } from '@mui/x-charts-vendor/d3-scale';
2+
import { createScalarFormatter } from '../../../defaultValueFormatters';
23
import { AxisConfig, ScaleName } from '../../../../models';
34
import {
45
ChartsXAxisProps,
@@ -10,6 +11,7 @@ import {
1011
DefaultedXAxis,
1112
DefaultedYAxis,
1213
DefaultedAxis,
14+
AxisValueFormatterContext,
1315
} from '../../../../models/axis';
1416
import { CartesianChartSeriesType, ChartSeriesType } from '../../../../models/seriesType/config';
1517
import { getColorScale, getOrdinalColorScale } from '../../../colorScale';
@@ -208,6 +210,19 @@ export function computeAxisValue<T extends ChartSeriesType>({
208210
scale: finalScale.domain(domain) as any,
209211
tickNumber,
210212
colorScale: axis.colorMap && getColorScale(axis.colorMap),
213+
valueFormatter:
214+
axis.valueFormatter ??
215+
(createScalarFormatter(
216+
tickNumber,
217+
getScale(
218+
scaleType,
219+
range.map((v) => scale.invert(v)),
220+
range,
221+
),
222+
) as <TScaleName extends ScaleName>(
223+
value: any,
224+
context: AxisValueFormatterContext<TScaleName>,
225+
) => string),
211226
};
212227
});
213228
return {

packages/x-charts/src/models/axis.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,17 @@ export type AxisValueFormatterContext<S extends ScaleName = ScaleName> =
326326
* - `'legend'` The value is displayed in the legend when using color legend.
327327
* - `'zoom-slider-tooltip'` The value is displayed in the zoom slider tooltip.
328328
*/
329-
location: 'legend' | 'zoom-slider-tooltip';
329+
location: 'legend';
330330
}
331331
| {
332332
/**
333333
* Location indicates where the value will be displayed.
334334
* - `'tick'` The value is displayed on the axis ticks.
335335
* - `'tooltip'` The value is displayed in the tooltip when hovering the chart.
336336
* - `'legend'` The value is displayed in the legend when using color legend.
337+
* - `'zoom-slider-tooltip'` The value is displayed in the zoom slider tooltip.
337338
*/
338-
location: 'tick' | 'tooltip';
339+
location: 'tick' | 'tooltip' | 'zoom-slider-tooltip';
339340
/**
340341
* The d3-scale instance associated to the axis.
341342
*/

0 commit comments

Comments
 (0)