Skip to content

Commit ad15be8

Browse files
authored
fix(charts): allow formatting y/x-axis ticks w/o changing measure formatter (#3803)
Fixes #3802
1 parent da9ba46 commit ad15be8

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

packages/charts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"recharts": "2.1.16"
2424
},
2525
"peerDependencies": {
26-
"@ui5/webcomponents-react": "^1.1.0",
27-
"@ui5/webcomponents-react-base": "^1.1.0",
26+
"@ui5/webcomponents-react": "workspace:^1.1.0",
27+
"@ui5/webcomponents-react-base": "workspace:^1.1.0",
2828
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
2929
"react-jss": "^10.9.0"
3030
},

packages/charts/src/interfaces/ICartesianChartConfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ export interface ICartesianChartConfig {
5858
*
5959
* __Note:__ It is possible to overwrite internally used props. Please use with caution!
6060
*/
61-
yAxisConfig?: YAxisProps;
61+
yAxisConfig?: Omit<YAxisProps, 'mirror'>;
6262
/**
6363
* Defines possible configurations for the `XAxis`.
6464
*
6565
* __Note:__ It is possible to overwrite internally used props. Please use with caution!
6666
*/
67-
xAxisConfig?: XAxisProps;
67+
xAxisConfig?: Omit<XAxisProps, 'mirror'>;
6868
/**
6969
* Defines possible configurations for the second `YAxis`.
7070
*
7171
* __Note:__ It is possible to overwrite internally used props. Please use with caution!
7272
*/
73-
secondYAxisConfig?: YAxisProps;
73+
secondYAxisConfig?: Omit<YAxisProps, 'mirror'>;
7474
/**
7575
* Defines possible configurations for the second `YAxis`.
7676
*
7777
* __Note:__ It is possible to overwrite internally used props. Please use with caution!
7878
*/
79-
secondXAxisConfig?: XAxisProps;
79+
secondXAxisConfig?: Omit<XAxisProps, 'mirror'>;
8080
}

packages/charts/src/internal/XAxisTicks.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ interface XAxisTicksProps {
1313
secondYAxisConfig?: {
1414
color: string;
1515
};
16+
tickFormatter?: (value: any, index: number) => string;
17+
index?: number;
1618
}
1719

1820
export const XAxisTicks = (props: XAxisTicksProps) => {
19-
const { x, y, payload, config, visibleTicksCount, width, secondYAxisConfig } = props;
21+
const { x, y, payload, config, visibleTicksCount, width, secondYAxisConfig, tickFormatter, index } = props;
2022

2123
const bandWidth = width / visibleTicksCount;
2224
const shouldRotate = bandWidth <= 100;
2325

24-
const formattedValue = config.formatter(payload.value);
26+
const formattedValue = tickFormatter?.(payload.value, index) ?? config.formatter(payload.value);
2527
let textToDisplay = formattedValue;
2628
if (shouldRotate) {
2729
textToDisplay = truncateLongLabel(formattedValue, 11);

packages/charts/src/internal/YAxisTicks.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ interface YAxisTicksProps {
1212
secondYAxisConfig?: {
1313
color: string;
1414
};
15+
tickFormatter?: (value: any, index: number) => string;
16+
index?: number;
1517
}
1618

1719
export const YAxisTicks = (props: YAxisTicksProps) => {
18-
const { x, y, payload, config, secondYAxisConfig } = props;
20+
const { x, y, payload, config, secondYAxisConfig, tickFormatter, index } = props;
1921

20-
const formattedValue = config.formatter(payload.value);
22+
const formattedValue = tickFormatter?.(payload.value, index) ?? config.formatter(payload.value);
2123
let textToDisplay = formattedValue;
2224
if (getTextWidth(formattedValue) > defaultMaxYAxisWidth) {
2325
for (let i = `${formattedValue}`.length; i > 0; i--) {

packages/main/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@tanstack/react-virtual": "3.0.0-beta.18",
26-
"@ui5/webcomponents-react-base": "^1.6.0",
26+
"@ui5/webcomponents-react-base": "workspace:^",
2727
"clsx": "1.2.1",
2828
"react-jss": "^10.9.0",
2929
"react-table": "7.8.0"

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6342,7 +6342,7 @@ __metadata:
63426342
languageName: node
63436343
linkType: hard
63446344

6345-
"@ui5/webcomponents-react-base@^1.5.0, @ui5/webcomponents-react-base@workspace:packages/base":
6345+
"@ui5/webcomponents-react-base@workspace:^, @ui5/webcomponents-react-base@workspace:packages/base":
63466346
version: 0.0.0-use.local
63476347
resolution: "@ui5/webcomponents-react-base@workspace:packages/base"
63486348
peerDependencies:
@@ -6367,8 +6367,8 @@ __metadata:
63676367
react-content-loader: 6.2.0
63686368
recharts: 2.1.16
63696369
peerDependencies:
6370-
"@ui5/webcomponents-react": ^1.1.0
6371-
"@ui5/webcomponents-react-base": ^1.1.0
6370+
"@ui5/webcomponents-react": "workspace:^1.1.0"
6371+
"@ui5/webcomponents-react-base": "workspace:^1.1.0"
63726372
react: ^16.14.0 || ^17.0.0 || ^18.0.0
63736373
react-jss: ^10.9.0
63746374
languageName: unknown
@@ -6379,7 +6379,7 @@ __metadata:
63796379
resolution: "@ui5/webcomponents-react@workspace:packages/main"
63806380
dependencies:
63816381
"@tanstack/react-virtual": 3.0.0-beta.18
6382-
"@ui5/webcomponents-react-base": ^1.5.0
6382+
"@ui5/webcomponents-react-base": "workspace:^"
63836383
clsx: 1.2.1
63846384
react-jss: ^10.9.0
63856385
react-table: 7.8.0

0 commit comments

Comments
 (0)