Skip to content

Commit 026725a

Browse files
committed
Adjust the format of tooltip
1 parent d1f9ee2 commit 026725a

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

src/lib/components/date/FormattedDateTime.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export const FormattedDateTime = ({
206206
case 'chart-date':
207207
return <>{DAY_MONTH_FORMATER.format(value).replace(/[ ,]/g, '')}</>;
208208
case 'long-term-date':
209-
return <>{LONG_TERM_DATE_FORMATER.format(value).toUpperCase()}</>;
209+
return <>{LONG_TERM_DATE_FORMATER.format(value)}</>;
210210
default:
211211
return <></>;
212212
}

src/lib/components/linetimeseriechart/linetimeseriechart.component.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ export type LineChartProps = (
130130
label: string;
131131
}[];
132132
isLoading?: boolean;
133-
xAxisFormat?: 'with-time' | 'long-term';
133+
/**
134+
* The format of the x axis, default is 'date-time' which is like 01 Sep 16:00
135+
* If you want to display the date only, you can set it to 'date' which is like 2025-09-01
136+
* This will affect the format of the tooltip as well
137+
*/
138+
timeFormat?: 'date-time' | 'date';
134139
yAxisTitle?: string;
135140
helpText?: string;
136141
};
@@ -140,6 +145,7 @@ const CustomTooltip = ({
140145
payload,
141146
label,
142147
unitLabel,
148+
timeFormat,
143149
}: {
144150
active?: boolean;
145151
payload?: Array<{
@@ -150,6 +156,7 @@ const CustomTooltip = ({
150156
}>;
151157
label?: string;
152158
unitLabel?: string;
159+
timeFormat?: 'date-time' | 'date';
153160
}) => {
154161
if (!active || !payload || !payload.length || !label) return null;
155162
// We can't use the default itemSorter method because it's a custom tooltip.
@@ -171,7 +178,11 @@ const CustomTooltip = ({
171178
<TooltipContainer>
172179
<TooltipTime>
173180
<FormattedDateTime
174-
format="day-month-abbreviated-hour-minute-second"
181+
format={
182+
timeFormat === 'date-time'
183+
? 'day-month-abbreviated-hour-minute-second'
184+
: 'long-date'
185+
}
175186
value={new Date(label)}
176187
/>
177188
</TooltipTime>
@@ -207,7 +218,7 @@ export function LineTimeSerieChart({
207218
duration,
208219
unitRange,
209220
isLoading = false,
210-
xAxisFormat = 'with-time',
221+
timeFormat = 'date-time',
211222
yAxisType = 'default',
212223
yAxisTitle,
213224
helpText,
@@ -399,13 +410,13 @@ export function LineTimeSerieChart({
399410
const formatXAxisLabel = useCallback(
400411
(timestamp: number) => {
401412
const date = new Date(timestamp);
402-
return xAxisFormat === 'with-time'
413+
return timeFormat === 'date-time'
403414
? DAY_MONTH_ABBREVIATED_HOUR_MINUTE.format(date).replace(',', '')
404-
: xAxisFormat === 'long-term'
415+
: timeFormat === 'date'
405416
? LONG_TERM_DATE_FORMATER.format(date)
406417
: '';
407418
},
408-
[xAxisFormat],
419+
[timeFormat],
409420
);
410421

411422
return (
@@ -480,7 +491,11 @@ export function LineTimeSerieChart({
480491
}}
481492
tickFormatter={(value) => Math.round(value).toString()}
482493
/>
483-
<Tooltip content={<CustomTooltip unitLabel={unitLabel} />} />
494+
<Tooltip
495+
content={
496+
<CustomTooltip unitLabel={unitLabel} timeFormat={timeFormat} />
497+
}
498+
/>
484499
{/* Add horizontal line at y=0 for symmetrical charts */}
485500
{yAxisType === 'symmetrical' && (
486501
<ReferenceLine y={0} stroke={theme.border} />

stories/linetimeseriechart.stories.tsx

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ const meta: Meta<typeof LineTimeSerieChart> = {
6464
options: ['default', 'percentage', 'symmetrical'],
6565
},
6666
yAxisTitle: { control: 'text' },
67-
xAxisFormat: {
67+
timeFormat: {
6868
control: 'select',
69-
options: ['with-time', 'long-term'],
69+
options: ['date-time', 'date'],
7070
},
7171
},
7272
};
@@ -409,6 +409,74 @@ const irregularIntervalData = [
409409
[1740424900, '46.92'], // +1100 seconds (18.3 minutes) - under 2 intervals
410410
];
411411

412+
// Data spanning several months - multi-month dataset
413+
const longTermPrometheusData: [number, string][] = [
414+
// January 2025
415+
[1735689600, '42.15'], // Jan 1, 2025
416+
[1735776000, '38.92'], // Jan 2, 2025
417+
[1735862400, '55.33'], // Jan 3, 2025
418+
[1735948800, '67.88'], // Jan 4, 2025
419+
[1736035200, '44.21'], // Jan 5, 2025
420+
[1736121600, '72.15'], // Jan 6, 2025
421+
[1736208000, '39.67'], // Jan 7, 2025
422+
[1736294400, '58.44'], // Jan 8, 2025
423+
[1736380800, '63.92'], // Jan 9, 2025
424+
[1736467200, '41.33'], // Jan 10, 2025
425+
[1736553600, '76.88'], // Jan 11, 2025
426+
[1736640000, '52.15'], // Jan 12, 2025
427+
[1736726400, '48.67'], // Jan 13, 2025
428+
[1736812800, '69.33'], // Jan 14, 2025
429+
[1736899200, '35.88'], // Jan 15, 2025
430+
// February 2025
431+
[1738368000, '81.15'], // Feb 1, 2025
432+
[1738454400, '47.67'], // Feb 2, 2025
433+
[1738540800, '54.33'], // Feb 3, 2025
434+
[1738627200, '62.88'], // Feb 4, 2025
435+
[1738713600, '38.15'], // Feb 5, 2025
436+
[1738800000, '73.67'], // Feb 6, 2025
437+
[1738886400, '45.33'], // Feb 7, 2025
438+
[1738972800, '59.88'], // Feb 8, 2025
439+
[1739059200, '51.15'], // Feb 9, 2025
440+
[1739145600, '66.67'], // Feb 10, 2025
441+
[1739232000, '43.33'], // Feb 11, 2025
442+
[1739318400, '78.88'], // Feb 12, 2025
443+
[1739404800, '36.15'], // Feb 13, 2025
444+
[1739491200, '64.67'], // Feb 14, 2025
445+
[1739577600, '49.33'], // Feb 15, 2025
446+
// March 2025
447+
[1740787200, '71.88'], // Mar 1, 2025
448+
[1740873600, '42.15'], // Mar 2, 2025
449+
[1740960000, '57.67'], // Mar 3, 2025
450+
[1741046400, '68.33'], // Mar 4, 2025
451+
[1741132800, '34.88'], // Mar 5, 2025
452+
[1741219200, '75.15'], // Mar 6, 2025
453+
[1741305600, '48.67'], // Mar 7, 2025
454+
[1741392000, '53.33'], // Mar 8, 2025
455+
[1741478400, '61.88'], // Mar 9, 2025
456+
[1741564800, '37.15'], // Mar 10, 2025
457+
[1741651200, '74.67'], // Mar 11, 2025
458+
[1741737600, '44.33'], // Mar 12, 2025
459+
[1741824000, '58.88'], // Mar 13, 2025
460+
[1741910400, '52.15'], // Mar 14, 2025
461+
[1741996800, '67.67'], // Mar 15, 2025
462+
// April 2025
463+
[1743465600, '41.33'], // Apr 1, 2025
464+
[1743552000, '76.88'], // Apr 2, 2025
465+
[1743638400, '35.15'], // Apr 3, 2025
466+
[1743724800, '63.67'], // Apr 4, 2025
467+
[1743811200, '50.33'], // Apr 5, 2025
468+
[1743897600, '72.88'], // Apr 6, 2025
469+
[1743984000, '39.15'], // Apr 7, 2025
470+
[1744070400, '56.67'], // Apr 8, 2025
471+
[1744156800, '65.33'], // Apr 9, 2025
472+
[1744243200, '33.88'], // Apr 10, 2025
473+
[1744329600, '77.15'], // Apr 11, 2025
474+
[1744416000, '46.67'], // Apr 12, 2025
475+
[1744502400, '54.33'], // Apr 13, 2025
476+
[1744588800, '60.88'], // Apr 14, 2025
477+
[1744675200, '49.22'], // Apr 15, 2025
478+
];
479+
412480
export const PercentageChartExample: Story = {
413481
args: {
414482
series: [
@@ -429,7 +497,7 @@ export const PercentageChartExample: Story = {
429497
yAxisTitle: '',
430498
interval: SAMPLE_FREQUENCY_LAST_TWENTY_FOUR_HOURS,
431499
duration: SAMPLE_DURATION_LAST_TWENTY_FOUR_HOURS,
432-
xAxisFormat: 'long-term',
500+
xAxisFormat: 'date-time',
433501
},
434502
};
435503
const UNIT_RANGE_BS = [
@@ -542,3 +610,27 @@ export const IrregularIntervalsExample: Story = {
542610
duration: SAMPLE_DURATION_LAST_TWENTY_FOUR_HOURS,
543611
},
544612
};
613+
614+
export const DateFormatExample: Story = {
615+
args: {
616+
series: [
617+
{
618+
data: longTermPrometheusData,
619+
resource: 'ip-10-160-122-207.eu-north-1.compute.internal',
620+
metricPrefix: 'instance:10.160.122.207:9100',
621+
getTooltipLabel: (prefix, resource) => `${resource}`,
622+
},
623+
],
624+
title: 'Long term data',
625+
height: 200,
626+
startingTimeStamp: longTermPrometheusData[0][0],
627+
isLoading: false,
628+
isLegendHidden: false,
629+
helpText: 'This is the help text',
630+
interval: 60 * 60 * 24, // 1 day
631+
duration:
632+
longTermPrometheusData[longTermPrometheusData.length - 1][0] -
633+
longTermPrometheusData[0][0],
634+
timeFormat: 'date',
635+
},
636+
};

0 commit comments

Comments
 (0)