Skip to content

Commit 32cc46c

Browse files
fix: show baseline names in npm chart tooltips (#1456)
fix: show baseline display names in npm chart tooltips
1 parent 4e8b293 commit 32cc46c

3 files changed

Lines changed: 212 additions & 85 deletions

File tree

‎src/components/npm-stats/NPMStatsChart.tsx‎

Lines changed: 9 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
ruleX,
1313
ruleY,
1414
type ChartLinearGradient,
15-
type ChartPoint,
16-
type ChartTooltipContentContext,
1715
} from '@tanstack/charts'
1816
import { renderChartSvgWithResources } from '@tanstack/charts/svg/resources'
1917
import { tooltip } from '@tanstack/charts/tooltip'
@@ -65,6 +63,13 @@ import {
6563
} from './shared'
6664
import { BASELINE_LINE_COLOR } from './BaselineSection'
6765

66+
import {
67+
getNpmChartTooltipContent,
68+
type NpmHistoryPoint,
69+
type NpmStackedHistoryPoint,
70+
type NpmBarPoint,
71+
} from './npmChartTooltip'
72+
6873
const BASELINE_LINE_SERIES = '__baseline__'
6974
const percentFormatter = new Intl.NumberFormat('en-US', {
7075
style: 'percent',
@@ -2158,34 +2163,6 @@ function TimelineRangeScrubber({
21582163
)
21592164
}
21602165

2161-
type NpmHistoryPoint = {
2162-
color: string
2163-
date: Date
2164-
id: string
2165-
label: string
2166-
seriesName: string
2167-
value: number
2168-
}
2169-
2170-
type NpmStackedHistoryPoint = NpmHistoryPoint & {
2171-
fill: string
2172-
value1: number
2173-
value2: number
2174-
}
2175-
2176-
type NpmBarPoint = {
2177-
category: string
2178-
color: string
2179-
fill: string
2180-
id: string
2181-
label: string
2182-
seriesName: string
2183-
value1: number
2184-
value2: number
2185-
}
2186-
2187-
type NpmChartDatum = NpmHistoryPoint | NpmStackedHistoryPoint | NpmBarPoint
2188-
21892166
type NpmStatsChartInput = {
21902167
barOrientation: BarOrientation
21912168
bars: Array<NpmBarPoint>
@@ -2204,61 +2181,6 @@ type NpmStatsChartInput = {
22042181
yLabel?: string
22052182
}
22062183

2207-
function getNpmChartTooltipContent(
2208-
points: ReadonlyArray<ChartPoint<NpmChartDatum>>,
2209-
context: ChartTooltipContentContext,
2210-
input: NpmStatsChartInput,
2211-
) {
2212-
const first = points[0]
2213-
if (!first) return { rows: [] }
2214-
2215-
const formatValue = (point: ChartPoint<NpmChartDatum>) => {
2216-
const datum = point.datum
2217-
const value = 'value1' in datum ? datum.value2 - datum.value1 : datum.value
2218-
2219-
return input.yFormat?.(value) ?? value.toLocaleString()
2220-
}
2221-
2222-
if (points.length > 1) {
2223-
const title =
2224-
input.viewMode === 'history'
2225-
? `Date: ${context.formatX(first.xValue)}`
2226-
: input.barOrientation === 'horizontal'
2227-
? context.formatY(first.yValue)
2228-
: context.formatX(first.xValue)
2229-
2230-
return {
2231-
title,
2232-
rows: points.map((point) => ({
2233-
active: point === context.primaryPoint,
2234-
color: point.datum.color,
2235-
label: point.groupLabel,
2236-
value: formatValue(point),
2237-
})),
2238-
}
2239-
}
2240-
2241-
return {
2242-
title: first.groupLabel,
2243-
color: first.datum.color,
2244-
rows:
2245-
input.viewMode === 'history'
2246-
? [
2247-
{ label: 'Date', value: context.formatX(first.xValue) },
2248-
{
2249-
label: input.yLabel ?? 'Downloads',
2250-
value: formatValue(first),
2251-
},
2252-
]
2253-
: [
2254-
{
2255-
label: input.xLabel ?? input.yLabel ?? 'Downloads',
2256-
value: formatValue(first),
2257-
},
2258-
],
2259-
}
2260-
}
2261-
22622184
function createNpmStatsChart(input: NpmStatsChartInput) {
22632185
const definition = defineChart(() => createNpmStatsChartSpec(input))
22642186

@@ -3505,6 +3427,7 @@ export function NPMStatsChart({
35053427
id: `${datum.seriesName}:${datum.date.getTime()}:complete`,
35063428
label: `${datum.name}: ${formatHistoryTooltipValue(value)}`,
35073429
seriesName: datum.seriesName,
3430+
seriesLabel: datum.name,
35083431
value,
35093432
}
35103433
})
@@ -3523,6 +3446,7 @@ export function NPMStatsChart({
35233446
id: `${datum.seriesName}:${datum.date.getTime()}:partial`,
35243447
label: `${datum.name}: ${formatHistoryTooltipValue(value)}`,
35253448
seriesName: datum.seriesName,
3449+
seriesLabel: datum.name,
35263450
value,
35273451
}
35283452
})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import type { ChartPoint, ChartTooltipContentContext } from '@tanstack/charts'
2+
import type { BarOrientation, ViewMode } from './shared'
3+
4+
export type NpmHistoryPoint = {
5+
color: string
6+
date: Date
7+
id: string
8+
label: string
9+
seriesName: string
10+
seriesLabel?: string
11+
value: number
12+
}
13+
14+
export type NpmStackedHistoryPoint = NpmHistoryPoint & {
15+
fill: string
16+
value1: number
17+
value2: number
18+
}
19+
20+
export type NpmBarPoint = {
21+
category: string
22+
color: string
23+
fill: string
24+
id: string
25+
label: string
26+
seriesName: string
27+
value1: number
28+
value2: number
29+
}
30+
31+
type NpmChartDatum = NpmHistoryPoint | NpmStackedHistoryPoint | NpmBarPoint
32+
33+
export function getNpmChartTooltipContent(
34+
points: ReadonlyArray<ChartPoint<NpmChartDatum>>,
35+
context: ChartTooltipContentContext,
36+
input: {
37+
barOrientation: BarOrientation
38+
viewMode: ViewMode
39+
xLabel?: string
40+
yLabel?: string
41+
yFormat?: (value: number) => string
42+
},
43+
) {
44+
const first = points[0]
45+
if (!first) return { rows: [] }
46+
47+
const getLabel = (point: ChartPoint<NpmChartDatum>) =>
48+
('seriesLabel' in point.datum ? point.datum.seriesLabel : undefined) ??
49+
point.groupLabel
50+
51+
const formatValue = (point: ChartPoint<NpmChartDatum>) => {
52+
const datum = point.datum
53+
const value = 'value1' in datum ? datum.value2 - datum.value1 : datum.value
54+
55+
return input.yFormat?.(value) ?? value.toLocaleString()
56+
}
57+
58+
if (points.length > 1) {
59+
const title =
60+
input.viewMode === 'history'
61+
? `Date: ${context.formatX(first.xValue)}`
62+
: input.barOrientation === 'horizontal'
63+
? context.formatY(first.yValue)
64+
: context.formatX(first.xValue)
65+
66+
return {
67+
title,
68+
rows: points.map((point) => ({
69+
active: point === context.primaryPoint,
70+
color: point.datum.color,
71+
label: getLabel(point),
72+
value: formatValue(point),
73+
})),
74+
}
75+
}
76+
77+
return {
78+
title: getLabel(first),
79+
color: first.datum.color,
80+
rows:
81+
input.viewMode === 'history'
82+
? [
83+
{ label: 'Date', value: context.formatX(first.xValue) },
84+
{
85+
label: input.yLabel ?? 'Downloads',
86+
value: formatValue(first),
87+
},
88+
]
89+
: [
90+
{
91+
label: input.xLabel ?? input.yLabel ?? 'Downloads',
92+
value: formatValue(first),
93+
},
94+
],
95+
}
96+
}

‎tests/npm-chart-tooltip.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import assert from 'node:assert/strict'
2+
import test from 'node:test'
3+
import type { ChartPoint } from '@tanstack/charts'
4+
import { getNpmChartTooltipContent } from '../src/components/npm-stats/npmChartTooltip'
5+
import { getBaselineDisplayName } from '../src/components/npm-stats/shared'
6+
7+
type TooltipInput = Parameters<typeof getNpmChartTooltipContent>[2]
8+
type TooltipDatum = Parameters<
9+
typeof getNpmChartTooltipContent
10+
>[0][number]['datum']
11+
12+
const date = new Date('2026-10-01T00:00:00Z')
13+
const input: TooltipInput = {
14+
barOrientation: 'vertical',
15+
viewMode: 'history',
16+
}
17+
const context = {
18+
pinned: false,
19+
xLabel: 'Date',
20+
yLabel: 'Downloads',
21+
formatX: () => 'Oct 1, 2026',
22+
formatY: String,
23+
}
24+
25+
function point(
26+
seriesName: string,
27+
value: number,
28+
seriesLabel?: string,
29+
): ChartPoint<TooltipDatum> {
30+
return {
31+
key: seriesName,
32+
markId: 'npm-history-complete',
33+
group: seriesName,
34+
groupLabel: seriesName,
35+
datum: {
36+
color: '#888',
37+
date,
38+
id: seriesName,
39+
label: `${seriesLabel ?? seriesName}: ${value}`,
40+
seriesName,
41+
seriesLabel,
42+
value,
43+
},
44+
datumIndex: 0,
45+
xValue: date,
46+
yValue: value,
47+
x: 0,
48+
y: 0,
49+
color: '#888',
50+
}
51+
}
52+
53+
for (const normalized of [false, true]) {
54+
for (const labels of [['React'], ['Custom baseline'], ['React', 'Vue']]) {
55+
const name = getBaselineDisplayName(
56+
labels.map((label) => ({
57+
packages: [{ name: label.toLowerCase() }],
58+
baseline: true,
59+
hidden: true,
60+
baselineLabel: label,
61+
})),
62+
)
63+
test(`${normalized ? 'percentage' : 'absolute'} tooltips display ${name} while preserving baseline identity`, () => {
64+
const baseline = point('__baseline__', normalized ? 1 : 1000000, name)
65+
const options = {
66+
...input,
67+
yLabel: normalized ? 'Downloads (% of baseline)' : 'Downloads',
68+
yFormat: normalized ? (value: number) => `${value * 100}%` : String,
69+
}
70+
const expectedValue = normalized ? '100%' : '1000000'
71+
const single = getNpmChartTooltipContent([baseline], context, options)
72+
assert.equal(single.title, name)
73+
assert.deepEqual(single.rows, [
74+
{ label: 'Date', value: 'Oct 1, 2026' },
75+
{ label: options.yLabel, value: expectedValue },
76+
])
77+
78+
const tracked = point('@tanstack/react-query', 0.5)
79+
const grouped = getNpmChartTooltipContent(
80+
[tracked, baseline],
81+
{
82+
...context,
83+
primaryPoint: baseline,
84+
},
85+
options,
86+
)
87+
assert.equal(grouped.title, 'Date: Oct 1, 2026')
88+
assert.deepEqual(
89+
grouped.rows.map((row) => row.label),
90+
['@tanstack/react-query', name],
91+
)
92+
assert.equal(grouped.rows[1]?.value, expectedValue)
93+
assert.equal(baseline.group, '__baseline__')
94+
assert.equal(baseline.datum.seriesName, '__baseline__')
95+
assert.ok(!JSON.stringify(grouped).includes('__baseline__'))
96+
})
97+
}
98+
}
99+
100+
test('ordinary single-series tooltips keep the chart group label', () => {
101+
const content = getNpmChartTooltipContent(
102+
[point('react', 100)],
103+
context,
104+
input,
105+
)
106+
assert.equal(content.title, 'react')
107+
})

0 commit comments

Comments
 (0)