Skip to content

Commit 1e8a1b0

Browse files
authored
fix(frontend): fix data point aggregation logic for download chart (#1060)
This PR fixes the rendering of download chart when the recent 5 versions don't have enough x values in the chart. This PR fixes the download chart of `@ecopages/lit` package: BEFORE <img width="1228" alt="Screenshot 2025-04-22 at 11 49 45" src="https://github.com/user-attachments/assets/e9536df1-ca96-4c8e-ae70-ca8d7fc75f36" /> AFTER <img width="1242" alt="Screenshot 2025-04-22 at 11 49 31" src="https://github.com/user-attachments/assets/2c846670-9d46-4822-9472-07608b50d42f" />
1 parent 88351fc commit 1e8a1b0

File tree

3 files changed

+65
-65
lines changed

3 files changed

+65
-65
lines changed

frontend/deno.lock

Lines changed: 55 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/routes/package/(_islands)/DownloadChart.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
DownloadDataPoint,
66
PackageDownloadsRecentVersion,
77
} from "../../../utils/api_types.ts";
8+
import type ApexCharts from "apexcharts";
89

910
interface Props {
1011
downloads: PackageDownloadsRecentVersion[];
@@ -14,8 +15,7 @@ export type AggregationPeriod = "daily" | "weekly" | "monthly";
1415

1516
export function DownloadChart(props: Props) {
1617
const chartDivRef = useRef<HTMLDivElement>(null);
17-
// deno-lint-ignore no-explicit-any
18-
const chartRef = useRef<any>(null);
18+
const chartRef = useRef<ApexCharts>(null);
1919
const [graphRendered, setGraphRendered] = useState(false);
2020

2121
useEffect(() => {
@@ -63,7 +63,7 @@ export function DownloadChart(props: Props) {
6363
setGraphRendered(true);
6464
})();
6565
return () => {
66-
chartRef.current.destroy();
66+
chartRef.current?.destroy();
6767
chartRef.current = null;
6868
};
6969
}, []);
@@ -78,7 +78,7 @@ export function DownloadChart(props: Props) {
7878
<select
7979
id="aggregationPeriod"
8080
onChange={(e) =>
81-
chartRef.current.updateSeries(
81+
chartRef.current?.updateSeries(
8282
getSeries(
8383
props.downloads,
8484
e.currentTarget.value as AggregationPeriod,
@@ -158,7 +158,7 @@ export function normalize(
158158
dataPoints: DownloadDataPoint[],
159159
xValues: string[],
160160
aggregationPeriod: AggregationPeriod,
161-
): [Date, number][] {
161+
): [number, number][] {
162162
const normalized: { [key: string]: number } = {};
163163
for (const date of xValues) {
164164
normalized[date] = 0;
@@ -176,7 +176,7 @@ export function normalize(
176176

177177
return Object.entries(normalized).map((
178178
[key, value],
179-
) => [new Date(key), value]);
179+
) => [new Date(key).getTime(), value]);
180180
}
181181

182182
function getSeries(
@@ -193,7 +193,7 @@ function getSeries(
193193
).flat();
194194

195195
const xValues = collectX(
196-
dataPointsToDisplay.map((version) => version.downloads).flat(),
196+
dataPointsWithDownloads.map((version) => version.downloads).flat(),
197197
aggregationPeriod,
198198
);
199199

frontend/routes/package/(_islands)/DownloadWidget.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function DownloadWidget(props: Props) {
3434
}
3535

3636
const [hoveredDataPoint, setHoveredDataPoint] = useState<
37-
{ date: Date; data: number } | null
37+
{ date: number; data: number } | null
3838
>(null);
3939
const [graphRendered, setGraphRendered] = useState(false);
4040

@@ -141,11 +141,11 @@ export function DownloadWidget(props: Props) {
141141
<div>
142142
{hoveredDataPoint
143143
? `${
144-
hoveredDataPoint.date.toISOString()
144+
new Date(hoveredDataPoint.date).toISOString()
145145
.split("T")[0]
146146
} to ${
147147
new Date(
148-
hoveredDataPoint.date.getTime() + 6 * 24 * 60 * 60 * 1000,
148+
hoveredDataPoint.date + 6 * 24 * 60 * 60 * 1000,
149149
).toISOString()
150150
.split("T")[0]
151151
}`

0 commit comments

Comments
 (0)