Skip to content

Commit 8bed121

Browse files
authored
Merge pull request #929 from devtron-labs/feat/finops-uat
feat: add formatter for tooltip label values
2 parents 46587b4 + 9148d9a commit 8bed121

7 files changed

Lines changed: 69 additions & 25 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.20.6-pre-13",
3+
"version": "1.20.6-alpha-13",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ export const DATE_TIME_FORMATS = {
475475
DD_MMM_YYYY: 'DD MMM YYYY',
476476
'DD/MM/YYYY': 'DD/MM/YYYY',
477477
DD_MMM: 'DD MMM',
478+
TWENTY_FOUR_HOUR_FORMAT_HOUR: 'HH',
479+
ABBREVIATED_MONTH: 'MMM',
480+
DATE_WITH_ABBREVIATED_MONTH: 'DD MMM',
481+
WEEKDAY_WITH_DATE_MONTH_AND_YEAR: 'ddd, DD MMM YYYY',
482+
WEEKDAY_DATE_MONTH_YEAR_AND_HOUR: 'ddd, DD MMM YYYY, HH:00',
483+
DAY_OF_MONTH_WITH_ORDINAL: 'Do',
484+
ABBREVIATED_WEEKDAY: 'ddd',
485+
DAY_OF_MONTH: 'DD',
478486
}
479487

480488
export const SEMANTIC_VERSION_DOCUMENTATION_LINK = 'https://semver.org/'

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: 14 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
@@ -109,6 +114,7 @@ export type ChartProps = {
109114
* @default 'top'
110115
*/
111116
placement?: TooltipProps['placement']
117+
datasetValueFormatter?: (value: number) => string | number
112118
}
113119
/** A title for x axis */
114120
xScaleTitle?: string

src/Shared/Components/Charts/utils.tsx

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ export const getDefaultOptions = ({
190190
xScaleTitle,
191191
yScaleTitle,
192192
yScaleTickFormat,
193+
xScaleTickFormat,
194+
xAxisLabels,
195+
tooltipConfig,
193196
} = chartProps
197+
198+
const { datasetValueFormatter } = tooltipConfig ?? {}
199+
194200
const baseOptions: ChartOptions = {
195201
responsive: true,
196202
devicePixelRatio: 3,
@@ -211,6 +217,16 @@ export const getDefaultOptions = ({
211217
enabled: false,
212218
position: 'nearest',
213219
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+
: {}),
214230
},
215231
},
216232
elements: {
@@ -253,28 +269,42 @@ export const getDefaultOptions = ({
253269
},
254270
} satisfies ScaleOptions<'linear'>
255271

256-
const ticksWithCallback = {
257-
...commonScaleConfig.ticks,
258-
callback: (value) => yScaleTickFormat(Number(value)),
259-
} satisfies ScaleOptions<'linear'>['ticks']
260-
261272
const commonXScaleConfig = {
262273
...commonScaleConfig,
263274
max: xAxisMax,
264275
title: getScaleTickTitleConfig(xScaleTitle, appTheme),
265-
...(typeof yScaleTickFormat === 'function' && type === 'stackedBarHorizontal'
266-
? { ticks: ticksWithCallback }
267-
: {}),
276+
ticks: {
277+
...commonScaleConfig.ticks,
278+
...((type !== 'stackedBarHorizontal' && typeof xScaleTickFormat === 'function') ||
279+
(type === 'stackedBarHorizontal' && typeof yScaleTickFormat === 'function')
280+
? {
281+
callback:
282+
type === 'stackedBarHorizontal'
283+
? (value, index) => yScaleTickFormat(Number(value), index)
284+
: (_, index) => xScaleTickFormat(xAxisLabels[index], index),
285+
}
286+
: {}),
287+
autoSkip: false,
288+
},
268289
} satisfies ScaleOptions<'linear'>
269290

270291
const commonYScaleConfig = {
271292
...commonScaleConfig,
272293
max: yAxisMax,
273294
title: getScaleTickTitleConfig(yScaleTitle, appTheme),
274295
// for stackedBarHorizon
275-
...(typeof yScaleTickFormat === 'function' && type !== 'stackedBarHorizontal'
276-
? { ticks: ticksWithCallback }
277-
: {}),
296+
ticks: {
297+
...commonScaleConfig.ticks,
298+
...((type === 'stackedBarHorizontal' && typeof xScaleTickFormat === 'function') ||
299+
(type !== 'stackedBarHorizontal' && typeof yScaleTickFormat === 'function')
300+
? {
301+
callback:
302+
type !== 'stackedBarHorizontal'
303+
? (value, index) => yScaleTickFormat(Number(value), index)
304+
: (_, index) => xScaleTickFormat(xAxisLabels[index], index),
305+
}
306+
: {}),
307+
},
278308
} satisfies ScaleOptions<'linear'>
279309

280310
switch (type) {
@@ -555,7 +585,7 @@ export const buildChartTooltipFromContext = ({
555585
<div className="flexbox-col dc__gap-2">
556586
{titleLines.map((titleLine, index) => (
557587
// eslint-disable-next-line react/no-array-index-key
558-
<h6 key={index} className="m-0 fs-12 fw-6 lh-20 dc__truncate">
588+
<h6 key={index} className="m-0 fs-12 fw-6 lh-20 dc__word-break-all">
559589
{titleLine}
560590
</h6>
561591
))}

src/Shared/Components/Header/PageHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const PageHeader = ({
238238
<InfoIconTippy
239239
infoText={tippyMessage}
240240
heading={headerName || tippyHeader}
241-
iconClassName="icon-dim-20 ml-8 fcn-5"
241+
iconClassName="icon-dim-20 ml-8 fcn-7"
242242
documentationLink={tippyRedirectLink}
243243
documentationLinkText="View Documentation"
244244
additionalContent={additionalContent}

0 commit comments

Comments
 (0)