diff options
| author | Kostya <mail@sartin.in> | 2026-10-15 19:35:48 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-15 19:36:15 +0300 |
| commit | c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e (patch) | |
| tree | d9b721c72a4ffa2d6256adc305716c8432cb190c /ui-web/src/Chart.tsx | |
| parent | c67d5ff663b876a15a3831f5a2b564c4002e99b6 (diff) | |
| download | guess-market-4.0.tar.gz guess-market-4.0.tar.xz guess-market-4.0.zip | |
A React + TypeScript single-page client served by Vite, working against
the same servlets as the desktop client: login, the events screen with
filters, details, order books and trading, and the account screen with
deposit, holdings, ledger rows and the user directory. No file upload,
as the exercise specifies.
It looks like the desktop client on purpose: the three skins of
theme.css as CSS variables, the four animations redone with SVG and
keyframes, and the desktop's Nerd Fonts, which npm run dev copies from
ui-fx instead of keeping a second copy in the repo. The shared chat sits
in a drawer above the status bar, as on the desktop, on its own poll
because chat lines don't move the market version.
Vite forwards /guess-market to Tomcat on 8080, so the page and the API
share an origin and the session cookie needs no CORS. The client polls
/api/sync once a second and reloads only when the version moved.
run-web.bat installs the packages and starts it on localhost:5173.
package.sh stages the sources under build/dist/ex4, without
node_modules.
Diffstat (limited to 'ui-web/src/Chart.tsx')
| -rw-r--r-- | ui-web/src/Chart.tsx | 94 |
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 @@ | |||
| 1 | import { EventDetail, Ledger, num } from './api'; | ||
| 2 | |||
| 3 | export type Point = [number, number]; | ||
| 4 | |||
| 5 | const seconds = (at: string) => { | ||
| 6 | const t = Date.parse(at.replace(' ', 'T')); | ||
| 7 | return Number.isNaN(t) ? 0 : t / 1000; | ||
| 8 | }; | ||
| 9 | const now = () => Date.now() / 1000; | ||
| 10 | const clock = (t: number) => new Date(t * 1000).toTimeString().slice(0, 8); | ||
| 11 | |||
| 12 | export 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 | |||
| 33 | export 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 | |||
| 50 | const TICKS = [0, 1, 2, 3, 4]; | ||
| 51 | |||
| 52 | export 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 | } | ||