|
| 1 | +import { useFormatter } from "@keybr/lesson-ui"; |
| 2 | +import { Range, Vector } from "@keybr/math"; |
| 3 | +import { timeToSpeed } from "@keybr/result"; |
| 4 | +import { Canvas, type Rect, type ShapeList, Shapes } from "@keybr/widget"; |
| 5 | +import { type ReactNode } from "react"; |
| 6 | +import { Chart, chartArea, type SizeProps } from "./Chart.tsx"; |
| 7 | +import { withStyles } from "./decoration.ts"; |
| 8 | +import { bucketize } from "./dist/util.ts"; |
| 9 | +import { type TimeToType } from "./types.ts"; |
| 10 | +import { type ChartStyles, useChartStyles } from "./use-chart-styles.ts"; |
| 11 | + |
| 12 | +export function TimeToTypeHistogram({ |
| 13 | + steps, |
| 14 | + width, |
| 15 | + height, |
| 16 | +}: { |
| 17 | + readonly steps: readonly TimeToType[]; |
| 18 | +} & SizeProps): ReactNode { |
| 19 | + const styles = useChartStyles(); |
| 20 | + const paint = usePaint(styles, steps); |
| 21 | + return ( |
| 22 | + <Chart width={width} height={height}> |
| 23 | + <Canvas paint={chartArea(styles, paint)} /> |
| 24 | + </Chart> |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function usePaint(styles: ChartStyles, steps: readonly TimeToType[]) { |
| 29 | + const { formatSpeed } = useFormatter(); |
| 30 | + const g = withStyles(styles); |
| 31 | + |
| 32 | + const histogram = buildHistogram(steps); |
| 33 | + |
| 34 | + const vIndex = new Vector(); |
| 35 | + const vValue = new Vector(); |
| 36 | + for (let index = 0; index < histogram.length; index++) { |
| 37 | + vIndex.add(index); |
| 38 | + vValue.add(histogram[index]); |
| 39 | + } |
| 40 | + const rIndex = Range.from(vIndex); |
| 41 | + const rValue = Range.from(vValue); |
| 42 | + |
| 43 | + return (box: Rect): ShapeList => { |
| 44 | + return [ |
| 45 | + g.paintGrid(box, "vertical", { lines: 5 }), |
| 46 | + g.paintGrid(box, "horizontal", { lines: 5 }), |
| 47 | + paintHistogram(), |
| 48 | + g.paintAxis(box, "bottom"), |
| 49 | + g.paintAxis(box, "left"), |
| 50 | + g.paintTicks(box, rIndex, "bottom", { |
| 51 | + lines: 5, |
| 52 | + fmt: formatSpeed, |
| 53 | + style: styles.valueLabel, |
| 54 | + }), |
| 55 | + ]; |
| 56 | + |
| 57 | + function paintHistogram(): ShapeList { |
| 58 | + return Shapes.fill( |
| 59 | + styles.speed, |
| 60 | + [...rIndex.steps()].map((index) => { |
| 61 | + const w = Math.ceil(box.width / rIndex.span); |
| 62 | + const x = Math.round(rIndex.normalize(index, 1) * box.width); |
| 63 | + const y = Math.round(rValue.normalize(vValue.at(index)) * box.height); |
| 64 | + return Shapes.rect({ |
| 65 | + x: box.x + x, |
| 66 | + y: box.y + box.height - y, |
| 67 | + width: w, |
| 68 | + height: y, |
| 69 | + }); |
| 70 | + }), |
| 71 | + ); |
| 72 | + } |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +function buildHistogram(steps: readonly TimeToType[]) { |
| 77 | + const histogram = new Array(1501).fill(0); |
| 78 | + for (const { timeToType } of steps) { |
| 79 | + if (timeToType > 0) { |
| 80 | + const index = Math.round(timeToSpeed(timeToType)); |
| 81 | + if (index >= 0 && index < histogram.length) { |
| 82 | + histogram[index] += 1; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return bucketize(histogram, 30); |
| 87 | +} |
0 commit comments