Skip to content

Commit f0e4ac6

Browse files
Elessar1802chith-git
authored andcommitted
feat: add formatter for tooltip label values
1 parent ee0cb48 commit f0e4ac6

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Shared/Components/Charts/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export type ChartProps = {
114114
* @default 'top'
115115
*/
116116
placement?: TooltipProps['placement']
117+
datasetValueFormatter?: (value: number) => string | number
117118
}
118119
/** A title for x axis */
119120
xScaleTitle?: string

src/Shared/Components/Charts/utils.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ export const getDefaultOptions = ({
192192
yScaleTickFormat,
193193
xScaleTickFormat,
194194
xAxisLabels,
195+
tooltipConfig,
195196
} = chartProps
197+
198+
const { datasetValueFormatter } = tooltipConfig ?? {}
199+
196200
const baseOptions: ChartOptions = {
197201
responsive: true,
198202
devicePixelRatio: 3,
@@ -213,6 +217,16 @@ export const getDefaultOptions = ({
213217
enabled: false,
214218
position: 'nearest',
215219
external: externalTooltipHandler,
220+
...(datasetValueFormatter
221+
? {
222+
callbacks: {
223+
label({ dataset, raw }) {
224+
// only number values are expected in raw as input in our types for yAxisValues is number[]
225+
return `${dataset.label}: ${datasetValueFormatter(raw as number)}`
226+
},
227+
},
228+
}
229+
: {}),
216230
},
217231
},
218232
elements: {

0 commit comments

Comments
 (0)