Skip to content

Commit ee0cb48

Browse files
Elessar1802chith-git
authored andcommitted
feat: add xScaleTickFormat
1 parent 83f4b72 commit ee0cb48

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

src/Shared/Components/Charts/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export const CHART_AXIS_LABELS_COLOR: Record<AppThemeType, string> = {
310310

311311
export const CHART_CANVAS_BACKGROUND_COLORS: Record<AppThemeType, string> = {
312312
[AppThemeType.light]: 'rgb(255, 255, 255)',
313-
[AppThemeType.dark]: 'rgb(21, 22, 31)',
313+
[AppThemeType.dark]: 'rgb(30, 31, 40)',
314314
}
315315

316316
export const LINE_DASH = [6, 6]

src/Shared/Components/Charts/types.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ type XYAxisMax = {
6161

6262
type OnChartClickHandler = (datasetName: string, value: number) => void
6363

64+
type ScaleTickFormatCallbacks = Partial<{
65+
xScaleTickFormat: (label: string, index: number) => string | string[] | number | number[]
66+
yScaleTickFormat: (value: number, index: number) => string | string[] | number | number[]
67+
}>
68+
6469
export type TypeAndDatasetsType =
6570
| ({
6671
type: 'pie'
@@ -69,27 +74,27 @@ export type TypeAndDatasetsType =
6974
*/
7075
datasets: SimpleDatasetForPie
7176
onChartClick?: OnChartClickHandler
72-
yScaleTickFormat?: never
73-
} & Never<XYAxisMax>)
77+
} & Never<XYAxisMax> &
78+
Never<ScaleTickFormatCallbacks>)
7479
| ({
7580
type: 'line'
7681
datasets: SimpleDatasetForLineAndArea[]
7782
onChartClick?: never
78-
yScaleTickFormat?: (value: number) => string
79-
} & XYAxisMax)
83+
} & XYAxisMax &
84+
ScaleTickFormatCallbacks)
8085
| ({
8186
type: 'area'
8287
datasets: SimpleDatasetForLineAndArea
8388
/* onChartClick is not applicable for area charts */
8489
onChartClick?: never
85-
yScaleTickFormat?: (value: number) => string
86-
} & XYAxisMax)
90+
} & XYAxisMax &
91+
ScaleTickFormatCallbacks)
8792
| ({
8893
type: Exclude<ChartType, 'pie' | 'line' | 'area'>
8994
datasets: SimpleDataset[]
9095
onChartClick?: OnChartClickHandler
91-
yScaleTickFormat?: (value: number) => string
92-
} & XYAxisMax)
96+
} & XYAxisMax &
97+
ScaleTickFormatCallbacks)
9398

9499
export type ChartProps = {
95100
id: string

src/Shared/Components/Charts/utils.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export const getDefaultOptions = ({
190190
xScaleTitle,
191191
yScaleTitle,
192192
yScaleTickFormat,
193+
xScaleTickFormat,
194+
xAxisLabels,
193195
} = chartProps
194196
const baseOptions: ChartOptions = {
195197
responsive: true,
@@ -253,28 +255,42 @@ export const getDefaultOptions = ({
253255
},
254256
} satisfies ScaleOptions<'linear'>
255257

256-
const ticksWithCallback = {
257-
...commonScaleConfig.ticks,
258-
callback: (value) => yScaleTickFormat(Number(value)),
259-
} satisfies ScaleOptions<'linear'>['ticks']
260-
261258
const commonXScaleConfig = {
262259
...commonScaleConfig,
263260
max: xAxisMax,
264261
title: getScaleTickTitleConfig(xScaleTitle, appTheme),
265-
...(typeof yScaleTickFormat === 'function' && type === 'stackedBarHorizontal'
266-
? { ticks: ticksWithCallback }
267-
: {}),
262+
ticks: {
263+
...commonScaleConfig.ticks,
264+
...((type !== 'stackedBarHorizontal' && typeof xScaleTickFormat === 'function') ||
265+
(type === 'stackedBarHorizontal' && typeof yScaleTickFormat === 'function')
266+
? {
267+
callback:
268+
type === 'stackedBarHorizontal'
269+
? (value, index) => yScaleTickFormat(Number(value), index)
270+
: (_, index) => xScaleTickFormat(xAxisLabels[index], index),
271+
}
272+
: {}),
273+
autoSkip: false,
274+
},
268275
} satisfies ScaleOptions<'linear'>
269276

270277
const commonYScaleConfig = {
271278
...commonScaleConfig,
272279
max: yAxisMax,
273280
title: getScaleTickTitleConfig(yScaleTitle, appTheme),
274281
// for stackedBarHorizon
275-
...(typeof yScaleTickFormat === 'function' && type !== 'stackedBarHorizontal'
276-
? { ticks: ticksWithCallback }
277-
: {}),
282+
ticks: {
283+
...commonScaleConfig.ticks,
284+
...((type === 'stackedBarHorizontal' && typeof xScaleTickFormat === 'function') ||
285+
(type !== 'stackedBarHorizontal' && typeof yScaleTickFormat === 'function')
286+
? {
287+
callback:
288+
type !== 'stackedBarHorizontal'
289+
? (value, index) => yScaleTickFormat(Number(value), index)
290+
: (_, index) => xScaleTickFormat(xAxisLabels[index], index),
291+
}
292+
: {}),
293+
},
278294
} satisfies ScaleOptions<'linear'>
279295

280296
switch (type) {
@@ -555,7 +571,7 @@ export const buildChartTooltipFromContext = ({
555571
<div className="flexbox-col dc__gap-2">
556572
{titleLines.map((titleLine, index) => (
557573
// eslint-disable-next-line react/no-array-index-key
558-
<h6 key={index} className="m-0 fs-12 fw-6 lh-20 dc__truncate">
574+
<h6 key={index} className="m-0 fs-12 fw-6 lh-20 dc__word-break-all">
559575
{titleLine}
560576
</h6>
561577
))}

0 commit comments

Comments
 (0)