|
| 1 | +import { useFormatter } from "@keybr/lesson-ui"; |
| 2 | +import { Range, Vector } from "@keybr/math"; |
| 3 | +import { computeSpeed, type Stats, type Step } from "@keybr/textinput"; |
| 4 | +import { |
| 5 | + Canvas, |
| 6 | + formatDuration, |
| 7 | + type Rect, |
| 8 | + type ShapeList, |
| 9 | + Shapes, |
| 10 | +} from "@keybr/widget"; |
| 11 | +import { type ReactNode } from "react"; |
| 12 | +import { Chart, chartArea, type SizeProps } from "./Chart.tsx"; |
| 13 | +import { withStyles } from "./decoration.ts"; |
| 14 | +import { paintScatterPlot, projection } from "./graph.ts"; |
| 15 | +import { type ChartStyles, useChartStyles } from "./use-chart-styles.ts"; |
| 16 | + |
| 17 | +export function RollingSpeedChart({ |
| 18 | + stats, |
| 19 | + steps, |
| 20 | + width, |
| 21 | + height, |
| 22 | +}: { |
| 23 | + readonly stats: Stats; |
| 24 | + readonly steps: readonly Step[]; |
| 25 | +} & SizeProps): ReactNode { |
| 26 | + const styles = useChartStyles(); |
| 27 | + const paint = usePaint(styles, stats, steps); |
| 28 | + return ( |
| 29 | + <Chart width={width} height={height}> |
| 30 | + <Canvas paint={chartArea(styles, paint)} /> |
| 31 | + </Chart> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function usePaint(styles: ChartStyles, stats: Stats, steps: readonly Step[]) { |
| 36 | + const { formatSpeed } = useFormatter(); |
| 37 | + const g = withStyles(styles); |
| 38 | + |
| 39 | + const vTime = new Vector(); |
| 40 | + const vSpeed = new Vector(); |
| 41 | + const vBumps = new Vector(); |
| 42 | + const head = steps[0]; |
| 43 | + for (let index = 1; index < steps.length; index++) { |
| 44 | + const delta = Math.min(10, index); |
| 45 | + const prev = steps[index - delta]; |
| 46 | + const curr = steps[index]; |
| 47 | + const speed = computeSpeed(delta, curr.timeStamp - prev.timeStamp); |
| 48 | + vTime.add(curr.timeStamp - head.timeStamp); |
| 49 | + vSpeed.add(speed); |
| 50 | + if (curr.typo) { |
| 51 | + vBumps.add(curr.timeStamp - head.timeStamp); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + const rTime = Range.from(vTime); |
| 56 | + const rSpeed = Range.from(vSpeed); |
| 57 | + |
| 58 | + return (box: Rect): ShapeList => { |
| 59 | + const proj = projection(box, rTime, rSpeed); |
| 60 | + return [ |
| 61 | + g.paintGrid(box, "horizontal", { lines: 5 }), |
| 62 | + g.paintGrid(box, "vertical", { lines: 5 }), |
| 63 | + g.paintAxis(box, "bottom"), |
| 64 | + g.paintAxis(box, "left"), |
| 65 | + paintScatterPlot(proj, vTime, vSpeed, { |
| 66 | + style: styles.speed, |
| 67 | + }), |
| 68 | + pointAverageSpeedLine(), |
| 69 | + paintBumps(), |
| 70 | + g.paintTicks(box, rTime, "bottom", { |
| 71 | + lines: 5, |
| 72 | + fmt: (value) => formatDuration(value, { showMillis: true }), |
| 73 | + }), |
| 74 | + g.paintTicks(box, rSpeed, "left", { fmt: formatSpeed }), |
| 75 | + ]; |
| 76 | + |
| 77 | + function pointAverageSpeedLine(): ShapeList { |
| 78 | + const y = Math.round(proj.y(stats.speed)); |
| 79 | + return [ |
| 80 | + Shapes.fill(styles.threshold, [ |
| 81 | + Shapes.rect({ |
| 82 | + x: box.x - 10, |
| 83 | + y: y, |
| 84 | + width: box.width + 20, |
| 85 | + height: 1, |
| 86 | + }), |
| 87 | + ]), |
| 88 | + Shapes.fillText({ |
| 89 | + x: box.x + box.width + 15, |
| 90 | + y: y, |
| 91 | + value: formatSpeed(stats.speed), |
| 92 | + style: { |
| 93 | + ...styles.thresholdLabel, |
| 94 | + textAlign: "left", |
| 95 | + textBaseline: "middle", |
| 96 | + }, |
| 97 | + }), |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + function paintBumps(): ShapeList { |
| 102 | + const r = Math.round( |
| 103 | + Math.min(Math.max(proj.box.width / vTime.length, 2), 6), |
| 104 | + ); |
| 105 | + return Shapes.fill( |
| 106 | + styles.accuracy, |
| 107 | + [...vBumps].map((time) => { |
| 108 | + const x = Math.round(proj.x(time)); |
| 109 | + return [ |
| 110 | + Shapes.rect({ |
| 111 | + x: x, |
| 112 | + y: box.y, |
| 113 | + width: 1, |
| 114 | + height: box.height, |
| 115 | + }), |
| 116 | + Shapes.circle({ |
| 117 | + cx: x, |
| 118 | + cy: box.y + box.height, |
| 119 | + r, |
| 120 | + }), |
| 121 | + ]; |
| 122 | + }), |
| 123 | + ); |
| 124 | + } |
| 125 | + }; |
| 126 | +} |
0 commit comments