@@ -78,10 +78,16 @@ type TypeCounts = {
7878 passenger : number
7979}
8080
81- // Projection row also carries trailing-365d total — same shape as TypeCounts
82- // but with one extra field. Separate type so `keyof TypeCounts` (used by
83- // `typesMap` for per-victim-type indexing) doesn't pick up `trailing_365`.
84- type ProjectionRow = TypeCounts & { trailing_365 : number }
81+ // Projection row carries trailing-365d totals split by victim type so the
82+ // header can filter to the active `t=` subset client-side. Separate type
83+ // so `keyof TypeCounts` (used by `typesMap` for per-victim-type indexing)
84+ // doesn't pick up the `trailing_365_*` extras.
85+ type ProjectionRow = TypeCounts & {
86+ trailing_365_driver : number
87+ trailing_365_pedestrian : number
88+ trailing_365_cyclist : number
89+ trailing_365_passenger : number
90+ }
8591
8692const curYear = new Date ( ) . getFullYear ( )
8793const prvYear = curYear - 1
@@ -102,10 +108,10 @@ const typeCountsQuery = (county: string | null, cc: number | null, mc: number |
102108 CAST(sum(pedestrian) as INT) as pedestrian,
103109 CAST(sum(cyclist) as INT) as cyclist,
104110 CAST(sum(passenger) as INT) as passenger,
105- CAST(sum(trailing_365_driver
106- + trailing_365_pedestrian
107- + trailing_365_cyclist
108- + trailing_365_passenger) as INT) as trailing_365
111+ CAST(sum(trailing_365_driver) as INT) as trailing_365_driver,
112+ CAST(sum(trailing_365_pedestrian) as INT) as trailing_365_pedestrian,
113+ CAST(sum(trailing_365_cyclist) as INT) as trailing_365_cyclist,
114+ CAST(sum( trailing_365_passenger) as INT) as trailing_365_passenger
109115 FROM read_csv_auto('projected')
110116 ${ where }
111117`
@@ -288,7 +294,7 @@ export function FatalitiesPerYearPlot({ id = "per-year", initialCounty = null, c
288294
289295 // Projected current-year totals (statewide / county / municipality).
290296 const projectionsQueryStr = useMemo ( ( ) => typeCountsQuery ( county , propCc ?? null , propMc ?? null ) , [ county , propCc , propMc ] )
291- const [ projections ] = useQuery < ProjectionRow > ( { db : projectionsDb , query : projectionsQueryStr , init : [ { driver : 0 , pedestrian : 0 , cyclist : 0 , passenger : 0 , trailing_365 : 0 } ] } )
297+ const [ projections ] = useQuery < ProjectionRow > ( { db : projectionsDb , query : projectionsQueryStr , init : [ { driver : 0 , pedestrian : 0 , cyclist : 0 , passenger : 0 , trailing_365_driver : 0 , trailing_365_pedestrian : 0 , trailing_365_cyclist : 0 , trailing_365_passenger : 0 } ] } )
292298 const monthlyQueryStr = useMemo ( ( ) => monthlyQueryFn ( county , propCc ?? null , propMc ?? null ) , [ county , propCc , propMc ] )
293299 const monthlyRowsAll = useQuery < MonthlyRow > ( { db : monthlyDb , query : monthlyQueryStr , init : [ ] } )
294300
@@ -853,13 +859,19 @@ export function FatalitiesPerYearPlot({ id = "per-year", initialCounty = null, c
853859 const total2021 = yearTotals [ 2021 ] ?. actual ?? 0
854860 const total2022 = yearTotals [ 2022 ] ?. actual ?? 0
855861 const countyLabel = regionLabel ?? ( county ? `${ county } County` : "NJ" )
862+ // Sum trailing-365d only over the active `t=` subset so the header
863+ // headline tracks the same filter the plot bars reflect.
864+ const trailing365 = selectedTypeKeys . reduce (
865+ ( sum , t ) => sum + ( projections [ `trailing_365_${ t } ` as keyof ProjectionRow ] as number ) ,
866+ 0 ,
867+ )
856868
857869 return (
858870 < div ref = { containerRef } >
859871 < h2 id = { id } >
860872 < a href = { `#${ id } ` } > Car Crash Deaths</ a >
861873 </ h2 >
862- < div className = { css . subtitle } > { activeType ? `${ TYPE_SINGULAR [ activeType ] } ` : '' } Fatalities{ effectivePerCapita ? ' per 100k population' : '' } , { yearRange ? `${ yearRange [ 0 ] } –${ yearRange [ 1 ] } ` : '2001–present' } { regionLabel ? ` · ${ regionLabel } ` : initialCounty ? ` · ${ initialCounty } County` : '' } { projections . trailing_365 > 0 ? ` · Last 365 days: ${ projections . trailing_365 . toLocaleString ( ) } deaths` : '' } </ div >
874+ < div className = { css . subtitle } > { activeType ? `${ TYPE_SINGULAR [ activeType ] } ` : '' } Fatalities{ effectivePerCapita ? ' per 100k population' : '' } , { yearRange ? `${ yearRange [ 0 ] } –${ yearRange [ 1 ] } ` : '2001–present' } { regionLabel ? ` · ${ regionLabel } ` : initialCounty ? ` · ${ initialCounty } County` : '' } { trailing365 > 0 ? ` · Last 365 days: ${ trailing365 . toLocaleString ( ) } deaths` : '' } </ div >
863875 < PlotWrapper
864876 key = { `${ timeGranularity } -${ activeType ?? 'all' } -${ highlightProjected } -${ showPop ? 'pop' : 'nopop' } ` }
865877 id = { id }
0 commit comments