Skip to content

Commit 56f6791

Browse files
committed
refactor labelFormatter
1 parent 60faf04 commit 56f6791

File tree

10 files changed

+40
-26
lines changed

10 files changed

+40
-26
lines changed

packages/charts/src/components/BarChart/BarChart.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
189189
const [componentRef, chartRef] = useSyncRef<any>(ref);
190190

191191
const onItemLegendClick = useLegendItemClick(onLegendClick);
192-
const labelFormatter = useLabelFormatter(primaryDimension);
192+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
193193

194194
const onDataPointClickInternal = useCallback(
195195
(payload, i, event) => {
@@ -258,6 +258,7 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
258258
<XAxis
259259
interval={0}
260260
type="number"
261+
//todo why two times formatter? once in `tick` once in `tickFormatter`
261262
tick={<XAxisTicks config={primaryMeasure} />}
262263
axisLine={chartConfig.xAxisVisible}
263264
tickLine={tickLineConfig}
@@ -377,7 +378,7 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
377378
cursor={tooltipFillOpacity}
378379
formatter={tooltipValueFormatter}
379380
contentStyle={tooltipContentStyle}
380-
labelFormatter={labelFormatter}
381+
labelFormatter={tooltipLabelFormatter}
381382
{...tooltipConfig}
382383
/>
383384
)}

packages/charts/src/components/BulletChart/BulletChart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ const BulletChart = forwardRef<HTMLDivElement, BulletChartProps>((props, ref) =>
196196
chartConfig?.secondYAxis?.dataKey,
197197
);
198198

199-
const labelFormatter = useLabelFormatter(primaryDimension);
199+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
200200

201201
const dataKeys = sortedMeasures.map(({ accessor }) => accessor);
202202
const colorSecondY = chartConfig.secondYAxis
@@ -426,7 +426,7 @@ const BulletChart = forwardRef<HTMLDivElement, BulletChartProps>((props, ref) =>
426426
cursor={tooltipFillOpacity}
427427
formatter={tooltipValueFormatter}
428428
contentStyle={tooltipContentStyle}
429-
labelFormatter={labelFormatter}
429+
labelFormatter={tooltipLabelFormatter}
430430
{...tooltipConfig}
431431
/>
432432
)}

packages/charts/src/components/ColumnChart/ColumnChart.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ const ColumnChart = forwardRef<HTMLDivElement, ColumnChartProps>((props, ref) =>
180180
chartConfig?.secondYAxis?.dataKey,
181181
);
182182

183-
const labelFormatter = useLabelFormatter(primaryDimension);
183+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
184+
184185
const [componentRef, chartRef] = useSyncRef<any>(ref);
185186

186187
const dataKeys = measures.map(({ accessor }) => accessor);
@@ -371,7 +372,7 @@ const ColumnChart = forwardRef<HTMLDivElement, ColumnChartProps>((props, ref) =>
371372
cursor={tooltipFillOpacity}
372373
formatter={tooltipValueFormatter}
373374
contentStyle={tooltipContentStyle}
374-
labelFormatter={labelFormatter}
375+
labelFormatter={tooltipLabelFormatter}
375376
{...tooltipConfig}
376377
/>
377378
)}

packages/charts/src/components/ComposedChart/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const ComposedChart = forwardRef<HTMLDivElement, ComposedChartProps>((props, ref
197197
chartConfig?.secondYAxis?.dataKey,
198198
);
199199

200-
const labelFormatter = useLabelFormatter(primaryDimension);
200+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
201201

202202
const dataKeys = measures.map(({ accessor }) => accessor);
203203
const colorSecondY = chartConfig.secondYAxis
@@ -430,7 +430,7 @@ const ComposedChart = forwardRef<HTMLDivElement, ComposedChartProps>((props, ref
430430
cursor={tooltipFillOpacity}
431431
formatter={tooltipValueFormatter}
432432
contentStyle={tooltipContentStyle}
433-
labelFormatter={labelFormatter}
433+
labelFormatter={tooltipLabelFormatter}
434434
{...tooltipConfig}
435435
/>
436436
)}

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
169169
chartConfig?.secondYAxis?.dataKey,
170170
);
171171

172-
const labelFormatter = useLabelFormatter(primaryDimension);
172+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
173173

174174
const [componentRef, chartRef] = useSyncRef<any>(ref);
175175

@@ -345,7 +345,7 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
345345
cursor={tooltipFillOpacity}
346346
formatter={tooltipValueFormatter}
347347
contentStyle={tooltipContentStyle}
348-
labelFormatter={labelFormatter}
348+
labelFormatter={tooltipLabelFormatter}
349349
{...tooltipConfig}
350350
/>
351351
)}

packages/charts/src/components/RadarChart/RadarChart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const RadarChart = forwardRef<HTMLDivElement, RadarChartProps>((props, ref) => {
119119

120120
const primaryDimension = dimensions[0];
121121

122-
const labelFormatter = useLabelFormatter(primaryDimension);
122+
const tooltipLabelFormatter = useLabelFormatter(primaryDimension?.formatter);
123123

124124
const primaryDimensionAccessor = primaryDimension?.accessor;
125125

@@ -212,7 +212,7 @@ const RadarChart = forwardRef<HTMLDivElement, RadarChartProps>((props, ref) => {
212212
cursor={tooltipFillOpacity}
213213
formatter={tooltipValueFormatter}
214214
contentStyle={tooltipContentStyle}
215-
labelFormatter={labelFormatter}
215+
labelFormatter={tooltipLabelFormatter}
216216
{...tooltipConfig}
217217
/>
218218
)}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
import type { TooltipLabelFormatter } from '../interfaces/index.js';
12
import { useLabelFormatter } from './useLabelFormatter.js';
23

3-
// eslint-disable-next-line react/prop-types
4-
function LabelFormatterComponent({ options }) {
5-
const val = useLabelFormatter(options);
6-
return <span>{val(100)}</span>;
4+
function LabelFormatterComponent({ formatter }: { formatter?: TooltipLabelFormatter | string }) {
5+
const val = useLabelFormatter(formatter as any);
6+
return <span>{val(100, undefined)}</span>;
77
}
88

99
describe('useLabelFormatter', () => {
1010
it('should return value when no formatter is present', () => {
11-
cy.mount(<LabelFormatterComponent options={{ accessor: 'test' }} />);
11+
cy.mount(<LabelFormatterComponent formatter={null} />);
1212
cy.findByText('100').should('be.visible');
1313
});
1414

1515
it('should not crash on invalid formatter', () => {
16-
cy.mount(<LabelFormatterComponent options={{ accessor: 'test', formatter: 'abc' }} />);
16+
cy.mount(<LabelFormatterComponent formatter={'abc'} />);
1717
cy.findByText('100').should('be.visible');
1818
});
1919

2020
it('should format the value with a valid formatter', () => {
21-
cy.mount(<LabelFormatterComponent options={{ accessor: 'test', formatter: (val) => val / 10 }} />);
21+
cy.mount(<LabelFormatterComponent formatter={(val) => val / 10} />);
2222
cy.findByText('10').should('be.visible');
2323
});
2424
});
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { useCallback } from 'react';
2+
import type { DefaultTooltipContentProps } from 'recharts';
3+
import type { NameType, TooltipLabelFormatter, ValueType } from '../interfaces/index.js';
24

3-
export const useLabelFormatter = (primaryDimension) => {
4-
return useCallback(
5-
(name) => {
6-
if (primaryDimension && typeof primaryDimension.formatter === 'function') {
7-
return primaryDimension.formatter(name);
5+
export const useLabelFormatter = (formatter: TooltipLabelFormatter) => {
6+
return useCallback<TooltipLabelFormatter>(
7+
(label: DefaultTooltipContentProps<ValueType, NameType>['label'], payload) => {
8+
if (typeof formatter === 'function') {
9+
return formatter(label, payload);
810
}
9-
return name;
11+
return label;
1012
},
11-
[primaryDimension],
13+
[formatter],
1214
);
1315
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { TooltipProps } from 'recharts';
2+
3+
export type TooltipLabelFormatter = TooltipProps<
4+
number | string | Array<number | string>,
5+
number | string
6+
>['labelFormatter'];
7+
8+
export type ValueType = number | string | Array<number | string>;
9+
export type NameType = number | string;

packages/charts/src/internal/Utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const truncateLongLabel = (value: string, length = 13) => {
3232
return value;
3333
};
3434

35+
// todo: allow to explicitly set primary and secondary measure
3536
export const resolvePrimaryAndSecondaryMeasures = (measures: IChartMeasure[], secondaryAxisDataKey: string) => {
3637
const secondaryMeasure = measures.find((measure) => measure.accessor === secondaryAxisDataKey);
3738
const primaryMeasure = measures[0] === secondaryMeasure ? (measures[1] ?? measures[0]) : measures[0];

0 commit comments

Comments
 (0)