Skip to content

Commit be55e95

Browse files
committed
add yearly binning
1 parent 8ef9584 commit be55e95

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

app/routes/stats/npm/index.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type TimeRange =
5959
| '1825-days'
6060
| 'all-time'
6161

62-
type BinningOption = 'monthly' | 'weekly' | 'daily'
62+
type BinningOption = 'monthly' | 'weekly' | 'daily' | 'yearly'
6363

6464
type NpmPackage = {
6565
name: string
@@ -451,6 +451,9 @@ function NpmStatsChart({
451451
let binnedDate = date
452452

453453
switch (binningOption) {
454+
case 'yearly':
455+
binnedDate = d3.timeYear.floor(date)
456+
break
454457
case 'monthly':
455458
binnedDate = d3.timeMonth.floor(date)
456459
break
@@ -624,6 +627,12 @@ function NpmStatsChart({
624627
return d3.timeFormat('%b %d')(d)
625628
case '7-days':
626629
return d3.timeFormat('%a')(d)
630+
case '730-days':
631+
case '1825-days':
632+
case 'all-time':
633+
return binningOption === 'yearly'
634+
? d3.timeFormat('%Y')(d)
635+
: d3.timeFormat('%b %Y')(d)
627636
default:
628637
return d3.timeFormat('%x')(d)
629638
}
@@ -704,7 +713,7 @@ export const Route = createFileRoute('/stats/npm/')({
704713
.default('7-days'),
705714
baseline: z.string().optional(),
706715
viewMode: z.enum(['absolute', 'relative']).optional(),
707-
binningOption: z.enum(['monthly', 'weekly', 'daily']).optional(),
716+
binningOption: z.enum(['yearly', 'monthly', 'weekly', 'daily']).optional(),
708717
alignStartDates: z.boolean().optional().default(false),
709718
}),
710719
loaderDeps: ({ search }) => ({
@@ -877,9 +886,9 @@ function RouteComponent() {
877886
case '730-days':
878887
return 'monthly'
879888
case '1825-days':
880-
return 'monthly'
889+
return 'yearly'
881890
case 'all-time':
882-
return 'monthly'
891+
return 'yearly'
883892
}
884893
})()
885894

@@ -1010,10 +1019,10 @@ function RouteComponent() {
10101019
setBinningOption('monthly')
10111020
break
10121021
case '1825-days':
1013-
setBinningOption('monthly')
1022+
setBinningOption('yearly')
10141023
break
10151024
case 'all-time':
1016-
setBinningOption('monthly')
1025+
setBinningOption('yearly')
10171026
break
10181027
}
10191028

@@ -1037,7 +1046,9 @@ function RouteComponent() {
10371046
})
10381047
}
10391048

1040-
const handleBinnedChange = (value: 'daily' | 'weekly' | 'monthly') => {
1049+
const handleBinnedChange = (
1050+
value: 'daily' | 'weekly' | 'monthly' | 'yearly'
1051+
) => {
10411052
navigate({
10421053
to: '.',
10431054
search: (prev) => ({
@@ -1152,10 +1163,22 @@ function RouteComponent() {
11521163
</Tooltip>
11531164
</div>
11541165
<div className="flex items-stretch bg-gray-500/10 rounded-md">
1166+
<Tooltip content="Group data by year">
1167+
<button
1168+
onClick={() => handleBinnedChange('yearly')}
1169+
className={`px-3 py-1.5 rounded-l ${
1170+
binningOption === 'yearly'
1171+
? 'bg-cyan-500 text-white'
1172+
: 'hover:bg-gray-500/20'
1173+
}`}
1174+
>
1175+
Yearly
1176+
</button>
1177+
</Tooltip>
11551178
<Tooltip content="Group data by month">
11561179
<button
11571180
onClick={() => handleBinnedChange('monthly')}
1158-
className={`px-3 py-1.5 rounded-l ${
1181+
className={`px-3 py-1.5 ${
11591182
binningOption === 'monthly'
11601183
? 'bg-cyan-500 text-white'
11611184
: 'hover:bg-gray-500/20'

0 commit comments

Comments
 (0)