Skip to content

Commit c549c4e

Browse files
author
Amrit Kashyap Borah
committed
feat: support for formatting scale ticks
1 parent 994dcb3 commit c549c4e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/Shared/Components/Charts/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ export type TypeAndDatasetsType =
6969
*/
7070
datasets: SimpleDatasetForPie
7171
onChartClick?: OnChartClickHandler
72+
yScaleTickFormat?: never
7273
} & Never<XYAxisMax>)
7374
| ({
7475
type: 'line'
7576
datasets: SimpleDatasetForLineAndArea[]
7677
onChartClick?: never
78+
yScaleTickFormat?: (value: number) => string
7779
} & XYAxisMax)
7880
| ({
7981
type: 'area'
8082
datasets: SimpleDatasetForLineAndArea
8183
/* onChartClick is not applicable for area charts */
8284
onChartClick?: never
85+
yScaleTickFormat?: (value: number) => string
8386
} & XYAxisMax)
8487
| ({
8588
type: Exclude<ChartType, 'pie' | 'line' | 'area'>
8689
datasets: SimpleDataset[]
8790
onChartClick?: OnChartClickHandler
91+
yScaleTickFormat?: (value: number) => string
8892
} & XYAxisMax)
8993

9094
export type ChartProps = {

src/Shared/Components/Charts/utils.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export const getDefaultOptions = ({
181181
yAxisMax,
182182
xScaleTitle,
183183
yScaleTitle,
184+
yScaleTickFormat,
184185
} = chartProps
185186
const baseOptions: ChartOptions = {
186187
responsive: true,
@@ -244,16 +245,28 @@ export const getDefaultOptions = ({
244245
},
245246
} satisfies ScaleOptions<'linear'>
246247

248+
const ticksWithCallback = {
249+
...commonScaleConfig.ticks,
250+
callback: (value) => yScaleTickFormat(Number(value)),
251+
} satisfies ScaleOptions<'linear'>['ticks']
252+
247253
const commonXScaleConfig = {
248254
...commonScaleConfig,
249255
max: xAxisMax,
250256
title: getScaleTickTitleConfig(xScaleTitle, appTheme),
257+
...(typeof yScaleTickFormat === 'function' && type === 'stackedBarHorizontal'
258+
? { ticks: ticksWithCallback }
259+
: {}),
251260
} satisfies ScaleOptions<'linear'>
252261

253262
const commonYScaleConfig = {
254263
...commonScaleConfig,
255264
max: yAxisMax,
256265
title: getScaleTickTitleConfig(yScaleTitle, appTheme),
266+
// for stackedBarHorizon
267+
...(typeof yScaleTickFormat === 'function' && type !== 'stackedBarHorizontal'
268+
? { ticks: ticksWithCallback }
269+
: {}),
257270
} satisfies ScaleOptions<'linear'>
258271

259272
switch (type) {

0 commit comments

Comments
 (0)