Repository navigation
Expand file tree
/
Copy pathSampledLineChart.tsx
More file actions
95 lines (87 loc) · 2.5 KB
/
Copy pathSampledLineChart.tsx
File metadata and controls
95 lines (87 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use client";
import { useMemo } from "react";
import {
CartesianGrid,
Line,
LineChart,
Tooltip,
XAxis,
YAxis,
} from "recharts";
import { sampleLine } from "./sample-line.js";
export interface LinePoint {
readonly x: number;
readonly y: number | null;
}
export interface SampledLineChartProps<T extends LinePoint> {
readonly data: readonly T[];
readonly maxPoints: number;
readonly width: number;
readonly height: number;
readonly title?: string;
readonly xLabel?: string;
readonly yLabel?: string;
}
const readX = <T extends LinePoint>(row: T): number => row.x;
const readY = <T extends LinePoint>(row: T): number | null => row.y;
export function SampledLineChart<T extends LinePoint>({
data,
maxPoints,
width,
height,
title,
xLabel,
yLabel,
}: SampledLineChartProps<T>) {
const sampled = useMemo(
() => sampleLine(data, { x: readX, y: readY, maxPoints }),
[data, maxPoints],
);
let minimumX = Infinity;
let maximumX = -Infinity;
let minimumY = Infinity;
let maximumY = -Infinity;
for (const row of data) {
if (row.x < minimumX) minimumX = row.x;
if (row.x > maximumX) maximumX = row.x;
if (row.y !== null) {
if (row.y < minimumY) minimumY = row.y;
if (row.y > maximumY) maximumY = row.y;
}
}
const xDomain: [number, number] = data.length === 0 ? [0, 1] : [minimumX, maximumX];
const yDomain: [number, number] = minimumY === Infinity ? [0, 1] : [minimumY, maximumY];
return (
<section aria-label={title ?? "Sampled line chart"}>
{title === undefined ? null : <h2>{title}</h2>}
<p>
{sampled.sourceCount.toLocaleString("en-US")} source points ·{" "}
{sampled.data.length.toLocaleString("en-US")} displayed points
</p>
<LineChart width={width} height={height} data={sampled.data as T[]}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis
type="number"
dataKey={readX}
domain={xDomain}
label={xLabel === undefined ? undefined : { value: xLabel, position: "insideBottom", offset: -5 }}
/>
<YAxis
type="number"
domain={yDomain}
label={yLabel === undefined ? undefined : { value: yLabel, angle: -90, position: "insideLeft" }}
/>
<Tooltip />
<Line
type="linear"
dataKey={readY}
connectNulls={false}
dot={false}
isAnimationActive={false}
stroke="#2563eb"
strokeWidth={2}
/>
</LineChart>
</section>
);
}