aboutsummaryrefslogtreecommitdiffstats
path: root/ui-web/src/Chart.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'ui-web/src/Chart.tsx')
-rw-r--r--ui-web/src/Chart.tsx94
1 files changed, 94 insertions, 0 deletions
diff --git a/ui-web/src/Chart.tsx b/ui-web/src/Chart.tsx
new file mode 100644
index 0000000..4caf3a5
--- /dev/null
+++ b/ui-web/src/Chart.tsx
@@ -0,0 +1,94 @@
1import { EventDetail, Ledger, num } from './api';
2
3export type Point = [number, number];
4
5const seconds = (at: string) => {
6 const t = Date.parse(at.replace(' ', 'T'));
7 return Number.isNaN(t) ? 0 : t / 1000;
8};
9const now = () => Date.now() / 1000;
10const clock = (t: number) => new Date(t * 1000).toTimeString().slice(0, 8);
11
12export function priceSeries(detail: EventDetail): Point[] {
13 const first = detail.summary.optionNames[0];
14 const current = num(detail.state.markets[0]?.price) || 0.5;
15 const trades = [...detail.history].reverse();
16 if (trades.length === 0) {
17 return [
18 [now() - 60, current],
19 [now(), current],
20 ];
21 }
22 const start = seconds(trades[0].at) || now();
23 const points: Point[] = [[start - 60, 0.5]];
24 for (const t of trades) {
25 const quantity = num(t.quantity);
26 const unit = quantity > 0 ? num(t.pricePaid) / quantity : current;
27 const price = t.optionName === first ? unit : 1 - unit;
28 points.push([seconds(t.at) || start, Math.max(0, Math.min(1, price))]);
29 }
30 return points;
31}
32
33export function balanceSeries(ledger: Ledger): Point[] {
34 const rows = ledger.entries;
35 const balance = num(ledger.balance);
36 if (rows.length === 0) {
37 return [
38 [now() - 60, balance],
39 [now(), balance],
40 ];
41 }
42 const start = seconds(rows[0].at) || now();
43 const points: Point[] = [[start - 60, Math.max(0, num(rows[0].balanceAfter) - num(rows[0].amount))]];
44 for (const row of rows) {
45 points.push([seconds(row.at) || start, num(row.balanceAfter)]);
46 }
47 return points;
48}
49
50const TICKS = [0, 1, 2, 3, 4];
51
52export function LineChart(props: {
53 series: { points: Point[]; tone: 'accent' | 'yes' | 'no' }[];
54 range?: [number, number];
55 height?: number;
56}) {
57 const all = props.series.flatMap((s) => s.points);
58 const t0 = Math.min(...all.map((p) => p[0]));
59 const t1 = Math.max(...all.map((p) => p[0]), t0 + 60);
60 let [lo, hi] = props.range ?? [Math.min(...all.map((p) => p[1]), 0), Math.max(...all.map((p) => p[1]))];
61 if (hi <= lo) hi = lo + 1;
62 if (!props.range) hi = lo + Math.ceil(((hi - lo) * 1.1) / 4) * 4; // headroom, in four whole steps
63
64 const x = (t: number) => ((t - t0) / (t1 - t0)) * 100;
65 const y = (v: number) => 100 - ((v - lo) / (hi - lo)) * 100;
66 const label = (v: number) => (hi - lo <= 1 ? v.toFixed(2).replace(/\.?0+$/, '') || '0' : Math.round(v).toLocaleString());
67
68 return (
69 <div className="chart card2">
70 <div className="yaxis">
71 {TICKS.map((i) => (
72 <span key={i}>{label(hi - ((hi - lo) * i) / 4)}</span>
73 ))}
74 </div>
75 <svg viewBox="0 0 100 100" preserveAspectRatio="none" style={{ height: props.height ?? 150 }}>
76 {TICKS.map((i) => (
77 <line key={i} className="grid" x1="0" x2="100" y1={i * 25} y2={i * 25} />
78 ))}
79 {props.series.map((s, i) => (
80 <path
81 key={i}
82 className={'stroke-' + s.tone}
83 d={s.points.map((p, j) => `${j ? 'L' : 'M'}${x(p[0]).toFixed(2)} ${y(p[1]).toFixed(2)}`).join(' ')}
84 />
85 ))}
86 </svg>
87 <div className="xaxis">
88 {TICKS.map((i) => (
89 <span key={i}>{clock(t0 + ((t1 - t0) * i) / 4)}</span>
90 ))}
91 </div>
92 </div>
93 );
94}