Skip to content

Commit 09fc137

Browse files
authored
x-axis labels include years and are at the starts of months (#79)
<img width="754" alt="screenshot of updated charts" src="https://github.com/user-attachments/assets/48870124-978b-499a-bdf6-047154b0d6a7" />
1 parent a7b22cd commit 09fc137

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/components/PackageCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ function maxDays(versionFilter: VersionFilter) {
3838
}
3939
}
4040

41-
function tickInterval(versionFilter: VersionFilter) {
41+
function tickInterval(versionFilter: VersionFilter): "month" | number {
4242
switch (versionFilter) {
4343
case "major":
44-
return 60;
44+
return "month";
4545
case "patch":
4646
return 7;
4747
case "prerelease":

src/components/VersionDownloadChart.tsx

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export type VersionDownloadChartProps = {
4141
maxDaysShown?: number;
4242

4343
/**
44-
* The number of days between ticks
44+
* The number of days between ticks. Or "month" for ticks at the start of each month.
4545
*/
46-
tickInterval?: number;
46+
tickInterval?: number | "month";
4747

4848
/**
4949
* The maximum number of "popular" versions show (see below).
@@ -102,7 +102,7 @@ const VersionDownloadChart: React.FC<VersionDownloadChartProps> = ({
102102
showTooltip,
103103
unit,
104104
versionLabeler,
105-
tickInterval,
105+
tickInterval = 7,
106106
theme,
107107
tooltipTheme,
108108
}) => {
@@ -164,14 +164,28 @@ const VersionDownloadChart: React.FC<VersionDownloadChartProps> = ({
164164
const firstDate = filteredHistory?.points[0]?.date;
165165
const lastDate = filteredHistory?.points[lastDateIndex]?.date;
166166

167-
const msInDay = 1000 * 60 * 60 * 24;
168167
const ticks: number[] = [];
169-
for (
170-
let date = firstDate ?? 0;
171-
date <= (lastDate ?? 0);
172-
date += msInDay * (tickInterval ?? 7)
173-
) {
174-
ticks.push(date);
168+
if (tickInterval === "month") {
169+
if (firstDate && lastDate) {
170+
const currentDate = new Date(firstDate);
171+
currentDate.setMonth(currentDate.getMonth() + 1);
172+
currentDate.setDate(1);
173+
currentDate.setHours(0, 0, 0, 0);
174+
175+
while (+currentDate <= lastDate) {
176+
ticks.push(currentDate.getTime());
177+
currentDate.setMonth(currentDate.getMonth() + 1);
178+
}
179+
}
180+
} else {
181+
const msInDay = 1000 * 60 * 60 * 24;
182+
for (
183+
let date = firstDate ?? 0;
184+
date <= (lastDate ?? 0);
185+
date += msInDay * tickInterval
186+
) {
187+
ticks.push(date);
188+
}
175189
}
176190

177191
return (
@@ -191,10 +205,18 @@ const VersionDownloadChart: React.FC<VersionDownloadChartProps> = ({
191205
scale="time"
192206
domain={["dataMin", "dataMax"]}
193207
tickFormatter={(unixTime) =>
194-
new Date(unixTime).toLocaleDateString("en-US", {
195-
month: "short",
196-
day: "numeric",
197-
})
208+
new Date(unixTime).toLocaleDateString(
209+
"en-US",
210+
tickInterval === "month"
211+
? {
212+
month: "short",
213+
year: "numeric",
214+
}
215+
: {
216+
month: "short",
217+
day: "numeric",
218+
}
219+
)
198220
}
199221
/>
200222
<YAxis

0 commit comments

Comments
 (0)