Newer
Older
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
import {
LineChart,
Line,
XAxis,
YAxis,
Tooltip,
Legend,
ResponsiveContainer,
CartesianGrid
} from 'recharts';
type SeriesDatum = { year: number; [key: string]: number | string };
export default function TimeSeriesChart({
data,
regions,
colors = [] as string[]
}: {
data: SeriesDatum[]; // each item: { year, RegionA: value, RegionB: value }
regions: string[]; // region keys to plot
colors?: string[];
}) {
const palette = colors.length ? colors : ['#0B5FFF', '#00A78E', '#FFB020', '#6B21A8'];
return (
<ResponsiveContainer width="100%" height={320}>
<LineChart data={data} margin={{ top: 12, right: 24, left: 0, bottom: 12 }}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="year" />
<YAxis />
<Tooltip />
<Legend />
{regions.map((r, i) => (
<Line
key={r}
type="monotone"
dataKey={r}
stroke={palette[i % palette.length]}
strokeWidth={2}
dot={{ r: 2 }}
activeDot={{ r: 4 }}
connectNulls
/>
))}
</LineChart>
</ResponsiveContainer>
);
}