-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.tsx
More file actions
149 lines (139 loc) ยท 4.12 KB
/
Copy pathchart.tsx
File metadata and controls
149 lines (139 loc) ยท 4.12 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* LineChartPanel โ Bloomberg ์ฐจํธ ์ฐฝ ์ฌํ (SVG, ์์กด์ฑ ์์).
* ๊ฒ์ ํ๋กฏ + ์ ์ ๊ทธ๋ฆฌ๋ + ๋ฉํฐ์ปฌ๋ฌ ๋ผ์ธ + ํ๋จ ๊ฑฐ๋๋ ๋ฐ + ์ถ ๋ผ๋ฒจ.
* ๋ ํผ๋ฐ์ค: docs/design/bloomberg-reference-3.png (Graph #2 / #3)
*/
import { cn } from "@/lib/utils/cn";
type Tone = "accent" | "up" | "down" | "info" | "warn" | "mag" | "default";
const STROKE: Record<Tone, string> = {
accent: "var(--term-accent)",
up: "var(--term-up)",
down: "var(--term-down)",
info: "var(--term-info)",
warn: "var(--term-warn)",
mag: "var(--term-mag)",
default: "var(--term-white)",
};
type Series = { data: number[]; tone: Tone };
const VB_W = 300;
const VB_H = 120;
const PLOT_H = 92; // ๋ผ์ธ ์์ญ
const PAD_L = 2;
const PAD_R = 2;
function pointsFor(data: number[], min: number, max: number) {
const span = max - min || 1;
const innerW = VB_W - PAD_L - PAD_R;
const step = innerW / (data.length - 1 || 1);
return data
.map((v, i) => {
const x = PAD_L + i * step;
const y = PLOT_H - ((v - min) / span) * (PLOT_H - 4) - 2;
return `${x.toFixed(1)},${y.toFixed(1)}`;
})
.join(" ");
}
export function LineChartPanel({
title,
code,
series,
volume,
yLabels,
xLabels,
className,
}: {
title: string;
code?: string;
series: Series[];
volume?: number[];
yLabels?: string[];
xLabels?: string[];
className?: string;
}) {
const all = series.flatMap((s) => s.data);
const min = Math.min(...all);
const max = Math.max(...all);
const vMax = volume ? Math.max(...volume) || 1 : 1;
const gridYs = [0.0, 0.25, 0.5, 0.75, 1.0];
return (
<section
className={cn(
"bevel flex min-w-0 flex-col border border-term-border bg-term-panel",
className,
)}
>
<header className="flex items-center gap-2 bg-term-navy px-1.5 py-0.5">
<h2 className="truncate text-[11px] font-bold text-term-white">{title}</h2>
<span className="ml-auto text-[10px] text-term-info">
{code} ยท <span className="underline">Graph Options</span>
</span>
</header>
<div className="flex flex-1 gap-1 p-1">
<svg
viewBox={`0 0 ${VB_W} ${VB_H}`}
preserveAspectRatio="none"
className="h-32 w-full bg-term-bg"
>
{/* dotted horizontal gridlines */}
{gridYs.map((g, i) => (
<line
key={i}
x1={0}
x2={VB_W}
y1={g * PLOT_H}
y2={g * PLOT_H}
stroke="var(--term-grid)"
strokeWidth={0.5}
strokeDasharray="1 2"
/>
))}
{/* volume bars */}
{volume &&
volume.map((v, i) => {
const innerW = VB_W - PAD_L - PAD_R;
const bw = innerW / volume.length;
const h = (v / vMax) * (VB_H - PLOT_H - 4);
return (
<rect
key={i}
x={PAD_L + i * bw}
y={VB_H - h}
width={Math.max(0.6, bw - 0.6)}
height={h}
fill="var(--term-accent-dim)"
/>
);
})}
{/* series polylines */}
{series.map((s, i) => (
<polyline
key={i}
points={pointsFor(s.data, min, max)}
fill="none"
stroke={STROKE[s.tone]}
strokeWidth={0.9}
vectorEffect="non-scaling-stroke"
/>
))}
</svg>
{/* y axis labels */}
{yLabels && (
<div className="flex w-8 shrink-0 flex-col justify-between py-px text-right text-[9px] text-term-accent">
{yLabels.map((l, i) => (
<span key={i} className="tabular-nums">
{l}
</span>
))}
</div>
)}
</div>
{/* x axis labels */}
{xLabels && (
<div className="flex justify-between border-t border-term-grid px-1 pb-0.5 text-[9px] text-term-muted">
{xLabels.map((l, i) => (
<span key={i}>{l}</span>
))}
</div>
)}
</section>
);
}