Skip to content

Commit 43252c6

Browse files
RYGRITgraphieros
andauthored
fix: add date in chart tooltip (#1831)
Co-authored-by: Alec Lloyd Probert <55991794+graphieros@users.noreply.github.com>
1 parent e939665 commit 43252c6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

app/components/Package/TrendsChart.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,17 @@ const datetimeFormatterOptions = computed(() => {
11001100
}[selectedGranularity.value]
11011101
})
11021102
1103+
// Cached date formatter for tooltip
1104+
const tooltipDateFormatter = computed(() => {
1105+
const granularity = displayedGranularity.value
1106+
return new Intl.DateTimeFormat(locale.value, {
1107+
year: 'numeric',
1108+
month: granularity === 'yearly' ? undefined : 'short',
1109+
day: granularity === 'daily' || granularity === 'weekly' ? 'numeric' : undefined,
1110+
timeZone: 'UTC',
1111+
})
1112+
})
1113+
11031114
const sanitise = (value: string) =>
11041115
value
11051116
.replace(/^@/, '')
@@ -1598,11 +1609,23 @@ const chartConfig = computed<VueUiXyConfig>(() => {
15981609
borderColor: 'transparent',
15991610
backdropFilter: false,
16001611
backgroundColor: 'transparent',
1601-
customFormat: ({ datapoint: items }) => {
1612+
customFormat: ({ datapoint: items, absoluteIndex }) => {
16021613
if (!items || pending.value) return ''
16031614
16041615
const hasMultipleItems = items.length > 1
16051616
1617+
// Format date for multiple series datasets
1618+
let formattedDate = ''
1619+
if (hasMultipleItems && absoluteIndex !== undefined) {
1620+
const index = Number(absoluteIndex)
1621+
if (Number.isInteger(index) && index >= 0 && index < chartData.value.dates.length) {
1622+
const timestamp = chartData.value.dates[index]
1623+
if (typeof timestamp === 'number') {
1624+
formattedDate = tooltipDateFormatter.value.format(new Date(timestamp))
1625+
}
1626+
}
1627+
}
1628+
16061629
const rows = items
16071630
.map((d: Record<string, any>) => {
16081631
const label = String(d?.name ?? '').trim()
@@ -1635,6 +1658,7 @@ const chartConfig = computed<VueUiXyConfig>(() => {
16351658
.join('')
16361659
16371660
return `<div class="font-mono text-xs p-3 border border-border rounded-md bg-[var(--bg)]/10 backdrop-blur-md">
1661+
${formattedDate ? `<div class="text-2xs text-[var(--fg-subtle)] mb-2">${formattedDate}</div>` : ''}
16381662
<div class="${hasMultipleItems ? 'flex flex-col gap-2' : ''}">
16391663
${rows}
16401664
</div>

0 commit comments

Comments
 (0)