Spaces:
Running
Running
Download src/StepChart.tsx from dsh-marl/trajectory-explorer: direct link, hf CLI and curl.
- Browser
- Download file 4.2 kB
-
https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/StepChart.tsx
- Command line
-
hf download hf://spaces/dsh-marl/trajectory-explorer/src/StepChart.tsx
-
curl -L -o StepChart.tsx https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/StepChart.tsx
4.2 kB
| // A per-step chart: one line for the mean of each step, optionally a dot for every episode behind it. | |
| // Hover a step for its numbers; click a dot to open that episode. | |
| import { useEffect, useRef, useState } from "react"; | |
| export interface Dot { x: number; y: number; id: string; title: string } | |
| function niceTicks(max: number): number[] { | |
| if (max <= 1) return [0, 0.25, 0.5, 0.75, 1]; | |
| const step = max <= 5 ? 1 : max <= 10 ? 2 : max <= 25 ? 5 : 10; | |
| const out: number[] = []; | |
| for (let v = 0; v <= max + 1e-9; v += step) out.push(v); | |
| if (out[out.length - 1] < max) out.push(out[out.length - 1] + step); | |
| return out; | |
| } | |
| export default function StepChart({ xs, mean, dots, yMax, fmt, height = 260, onDot, meanName, dotName }: { | |
| xs: number[]; mean: Map<number, number>; dots?: Dot[]; yMax?: number; fmt: (v: number) => string; height?: number; | |
| onDot?: (id: string) => void; meanName: string; dotName?: string; | |
| }) { | |
| const box = useRef<HTMLDivElement>(null); | |
| const [w, setW] = useState(720); | |
| const [hx, setHx] = useState<number | null>(null); | |
| const [hd, setHd] = useState<string | null>(null); | |
| useEffect(() => { | |
| if (!box.current) return; | |
| const ro = new ResizeObserver(([e]) => setW(Math.max(280, Math.floor(e.contentRect.width)))); | |
| ro.observe(box.current); | |
| return () => ro.disconnect(); | |
| }, []); | |
| const m = { l: 44, r: 16, t: 12, b: 30 }; | |
| const iw = w - m.l - m.r, ih = height - m.t - m.b; | |
| const top = yMax ?? Math.max(...[...mean.values(), ...(dots ?? []).map((d) => d.y)], 1); | |
| const ticks = niceTicks(top); | |
| const ymax = ticks[ticks.length - 1]; | |
| const band = iw / xs.length; | |
| const X = (x: number) => m.l + (xs.indexOf(x) + 0.5) * band; | |
| const Y = (y: number) => m.t + ih - (y / ymax) * ih; | |
| const byX = new Map<number, Dot[]>(); | |
| for (const d of dots ?? []) byX.set(d.x, [...(byX.get(d.x) || []), d]); | |
| const jit = (d: Dot) => { const g = byX.get(d.x)!; const i = g.indexOf(d); return (i - (g.length - 1) / 2) * Math.min(2.5, band / (g.length * 3)); }; | |
| const line = xs.filter((x) => mean.has(x)).map((x, i) => `${i ? "L" : "M"}${X(x)},${Y(mean.get(x)!)}`).join(""); | |
| const hdot = dots?.find((d) => d.id === hd); | |
| const tipX = hx != null ? X(hx) : 0; | |
| return ( | |
| <div className="chart" ref={box}> | |
| <div className="legend"> | |
| {dots && <span><i className="lg-dot" />{dotName}</span>} | |
| <span><i className="lg-line" />{meanName}</span> | |
| </div> | |
| <svg width={w} height={height} onMouseLeave={() => { setHx(null); setHd(null); }}> | |
| {ticks.map((t) => ( | |
| <g key={t}> | |
| <line x1={m.l} x2={w - m.r} y1={Y(t)} y2={Y(t)} className="grid" /> | |
| <text x={m.l - 8} y={Y(t)} className="ytick">{fmt(t)}</text> | |
| </g> | |
| ))} | |
| {xs.map((x) => <text key={x} x={X(x)} y={height - 10} className="xtick">{x}</text>)} | |
| {hx != null && <line x1={tipX} x2={tipX} y1={m.t} y2={m.t + ih} className="cross" />} | |
| <path d={line} className="mline" /> | |
| {xs.filter((x) => mean.has(x)).map((x) => <circle key={x} cx={X(x)} cy={Y(mean.get(x)!)} r={hx === x ? 5 : 3.5} className="mpt" />)} | |
| {xs.map((x) => ( | |
| <rect key={x} x={m.l + xs.indexOf(x) * band} y={m.t} width={band} height={ih} fill="transparent" onMouseEnter={() => setHx(x)} /> | |
| ))} | |
| {dots?.map((d) => ( | |
| <circle key={d.id} cx={X(d.x) + jit(d)} cy={Y(d.y)} r={hd === d.id ? 6 : 4.5} className={"edot" + (hd === d.id ? " on" : "")} | |
| onMouseEnter={() => { setHd(d.id); setHx(d.x); }} onMouseLeave={() => setHd(null)} onClick={() => onDot?.(d.id)} /> | |
| ))} | |
| </svg> | |
| {hx != null && ( | |
| <div className="tip" style={{ left: Math.min(Math.max(tipX + 12, 0), w - 240), top: 28 }}> | |
| <div className="tip-h">第 {hx} 步</div> | |
| {hdot ? <div className="tip-r"><span>{hdot.title}</span><b>{fmt(hdot.y)}</b></div> | |
| : <> | |
| <div className="tip-r"><span>{meanName}</span><b>{mean.has(hx) ? fmt(mean.get(hx)!) : "–"}</b></div> | |
| {byX.get(hx) && <div className="tip-n">{byX.get(hx)!.length} 条{dotName ? `,点一个打开` : ""}</div>} | |
| </>} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |