@@ -12,8 +12,6 @@ import {
1212 ruleX ,
1313 ruleY ,
1414 type ChartLinearGradient ,
15- type ChartPoint ,
16- type ChartTooltipContentContext ,
1715} from '@tanstack/charts'
1816import { renderChartSvgWithResources } from '@tanstack/charts/svg/resources'
1917import { tooltip } from '@tanstack/charts/tooltip'
@@ -65,6 +63,13 @@ import {
6563} from './shared'
6664import { BASELINE_LINE_COLOR } from './BaselineSection'
6765
66+ import {
67+ getNpmChartTooltipContent ,
68+ type NpmHistoryPoint ,
69+ type NpmStackedHistoryPoint ,
70+ type NpmBarPoint ,
71+ } from './npmChartTooltip'
72+
6873const BASELINE_LINE_SERIES = '__baseline__'
6974const 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-
21892166type 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-
22622184function 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 } )
0 commit comments