| 1 | import { scaleBand, scaleLinear, scalePoint } from "d3-scale" |
| 2 | import { stack as |
| 3 | |
| 4 | export type StackType |
| 5 | |
| 6 | type Row |
| 7 | |
| 8 | const num = ( |
| 9 | typeof v === " |
| 10 | |
| 11 | /** |
| 12 | * Per-series [y0, y1] bands for every row. For `default` every series sits on |
| 13 | * the floor (y0 = 0); for `stacked`/`percent` they pile on top of each other |
| 14 | * via d3's stack layout. The shape `bands[key][i] = [y0, y1]` is what both the |
| 15 | * SVG area paths and the canvas overlay read from. |
| 16 | */ |
| 17 | export function computeBands( |
| 18 | data: Row |
| 19 | keys: string[] |
| 20 | stackType: StackType |
| 21 | ): { bands: |
| 22 | if (stackType === |
| 23 | const bands: |
| 24 | let max = |
| 25 | for (const key |
| 26 | bands[key] = |
| 27 | const v = |
| 28 | if (v > |
| 29 | return [0, v |
| 30 | }) |
| 31 | } |
| 32 | return { bands: |
| 33 | } |
| 34 | |
| 35 | const series = |
| 36 | .keys(keys) |
| 37 | .value((row, |
| 38 | .offset(stackType === |
| 39 | data |
| 40 | ) |
| 41 | |
| 42 | const bands: |
| 43 | let max = |
| 44 | series.forEach((layer |
| 45 | bands[layer.key |
| 46 | if (point[1] |
| 47 | return [point[0 |
| 48 | }) |
| 49 | }) |
| 50 | return { bands, |
| 51 | } |
| 52 | |
| 53 | /** x positions for each row index, evenly spread across the plot width. */ |
| 54 | export function buildXScale(length |
| 55 | return scalePoint<number |
| 56 | .domain(Array.from |
| 57 | .range([0, plotWidth |
| 58 | } |
| 59 | |
| 60 | /** Banded x for bar categories - each index owns a slot of `bandwidth` width. */ |
| 61 | export function buildBandScale(length |
| 62 | return scaleBand<number |
| 63 | .domain(Array.from |
| 64 | .range([0, plotWidth |
| 65 | .paddingInner(0.28) |
| 66 | .paddingOuter(0.18) |
| 67 | } |
| 68 | |
| 69 | /** Index of the category whose band a horizontal pixel offset falls in. */ |
| 70 | export function indexAtBand(px |
| 71 | if (length <= |
| 72 | const t = |
| 73 | return Math.min |
| 74 | } |
| 75 | |
| 76 | /** value → vertical pixel, with the floor at the bottom of the plot. */ |
| 77 | export function buildYScale(max |
| 78 | return scaleLinear().domain |
| 79 | } |
| 80 | |
| 81 | /** Index of the row nearest a horizontal pixel offset within the plot. */ |
| 82 | export function nearestIndex(px |
| 83 | if (length <= |
| 84 | const t = |
| 85 | return Math.round |
| 86 | } |