TrendChart.tsx
2.29 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
export function TrendChart() {
const data = [30, 42, 38, 55, 60, 48, 72, 80, 68, 90, 85, 98];
const labels = [
'1月',
'2月',
'3月',
'4月',
'5月',
'6月',
'7月',
'8月',
'9月',
'10月',
'11月',
'12月',
];
const w = 720;
const h = 240;
const padL = 16;
const padR = 16;
const padT = 18;
const padB = 28;
const max = 100;
const step = (w - padL - padR) / (data.length - 1);
const px = (i: number) => padL + i * step;
const py = (v: number) => padT + (1 - v / max) * (h - padT - padB);
const line = data
.map((v, i) => `${i ? 'L' : 'M'}${px(i).toFixed(1)} ${py(v).toFixed(1)}`)
.join(' ');
const area = `${line} L${px(data.length - 1).toFixed(1)} ${(h - padB).toFixed(1)} L${px(0).toFixed(1)} ${(h - padB).toFixed(1)} Z`;
const grid = [0, 25, 50, 75, 100];
return (
<svg viewBox={`0 0 ${w} ${h}`} width="100%" style={{ display: 'block' }}>
<defs>
<linearGradient id="ujArea" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="#2B5CE6" stopOpacity="0.28" />
<stop offset="100%" stopColor="#2B5CE6" stopOpacity="0" />
</linearGradient>
</defs>
{grid.map((g) => (
<g key={g}>
<line x1={padL} x2={w - padR} y1={py(g)} y2={py(g)} stroke="#EEF1F6" strokeWidth="1" />
<text x={2} y={py(g) + 4} fontSize="10" fill="#AAB3C2">
{g}
</text>
</g>
))}
<path d={area} fill="url(#ujArea)" />
<path
d={line}
fill="none"
stroke="#2B5CE6"
strokeWidth="2.6"
strokeLinejoin="round"
strokeLinecap="round"
/>
{data.map((v, i) =>
i === data.length - 1 ? (
<g key={i}>
<circle cx={px(i)} cy={py(v)} r="5" fill="#fff" stroke="#2B5CE6" strokeWidth="2.6" />
<text
x={px(i)}
y={py(v) - 12}
fontSize="12"
fontWeight={700}
fill="#2B5CE6"
textAnchor="middle"
>
{v}
</text>
</g>
) : null,
)}
{labels.map((lb, i) => (
<text key={lb} x={px(i)} y={h - 8} fontSize="10" fill="#AAB3C2" textAnchor="middle">
{i % 2 === 0 ? lb : ''}
</text>
))}
</svg>
);
}