@vitavision/charts 0.6.4 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,6 +24,19 @@ is accepted.
24
24
  with a threshold rule), and `LineProfile` (one or more series against arc length in pixels
25
25
  along a scan line or caliper axis, with detected edges drawn as labelled vertical rules).
26
26
 
27
+ **Fitting a chart to its container**: `variant="fluid"` takes the container's width at a fixed
28
+ `height`, for a chart in a resizable inspector.
29
+
30
+ **Interaction** on `LineChart` and `LineProfile`:
31
+ - `onHover` draws a crosshair with a value readout, and `onPick` reports a click's x.
32
+ - `cursor` draws an x chosen elsewhere, such as the pointer on the canvas a profile came from.
33
+ - `bands` shade x ranges, and `markers` tick positions on the x axis.
34
+
35
+ **Sequential colour maps**: `colormap("viridis" | "cividis", t)` and
36
+ `colormapValue(map, value, domain)`, for a magnitude such as an error, a validity or a
37
+ confidence. `ColormapLegend` shows the scale with its units; a map is never shown without one
38
+ (visual-language §4).
39
+
27
40
  ## License
28
41
 
29
42
  Licensed under either of
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import { ReactNode, Ref } from "react";
1
2
  import { MeasureTone } from "@vitavision/ui";
2
- import { ReactNode } from "react";
3
3
  //#region src/scale.d.ts
4
4
  /**
5
5
  * The arithmetic behind every chart: domains, ticks, and the map from a value to a pixel.
@@ -31,6 +31,8 @@ interface Scale {
31
31
  domain: [number, number];
32
32
  /** Map one domain value onto the pixel range this scale was built for. */
33
33
  project: (value: number) => number;
34
+ /** The inverse of `project`: the domain value at a pixel. Not clamped. */
35
+ invert: (pixel: number) => number;
34
36
  /**
35
37
  * Ticks inside the domain, in ascending order.
36
38
  *
@@ -98,28 +100,36 @@ export declare function histogram(values: readonly number[], domain: [number, nu
98
100
  //#region src/Frame.d.ts
99
101
  /**
100
102
  * Where a chart is going: `panel` (a 480×280 viewBox) for one column of a two-column grid,
101
- * `wide` (960×320) for a chart that spans a panel on its own.
103
+ * `wide` (960×320) for a chart that spans a panel on its own, or `fluid` to take its
104
+ * container's width at a fixed height, with text at its true size at any width.
102
105
  *
103
106
  * @remarks
104
107
  * Pick it by placement, not by pixel width — it keeps the chart's text at the same rendered
105
- * size as its neighbours'.
108
+ * size as its neighbours'. `fluid` measures its container (a `ResizeObserver`), for a chart
109
+ * in a resizable inspector or under a canvas.
106
110
  */
107
- type Variant = "panel" | "wide";
111
+ type Variant = "panel" | "wide" | "fluid";
112
+ /** A `fluid` chart's default height, in CSS pixels. */
113
+ export declare const FLUID_DEFAULT_HEIGHT = 200;
108
114
  /**
109
115
  * The drawable rectangle for one variant, in that variant's viewBox units.
110
116
  *
111
117
  * @param variant - Which viewBox the chart is drawn in.
118
+ * @param size - For `fluid`: the measured size in CSS pixels (see {@link useFluidSize}).
112
119
  * @returns The plot area's edges — `x0`/`x1` left to right, `y0` the bottom and `y1` the top
113
120
  * (SVG y grows downwards) — and the whole viewBox's `width`/`height`. Build scales over
114
121
  * `[x0, x1]` and `[y0, y1]`.
115
122
  */
116
- export declare function areaFor(variant: Variant): {
123
+ export declare function areaFor(variant: Variant, size?: {
124
+ width: number;
125
+ height: number;
126
+ }): {
117
127
  x0: 56;
118
128
  x1: number;
119
129
  y0: number;
120
130
  y1: 12;
121
- width: 480 | 960;
122
- height: 280 | 320;
131
+ width: number;
132
+ height: number;
123
133
  };
124
134
  /** The default geometry, for callers that do not care which variant they are in. */
125
135
  export declare const plotArea: {
@@ -127,9 +137,20 @@ export declare const plotArea: {
127
137
  x1: number;
128
138
  y0: number;
129
139
  y1: 12;
130
- width: 480 | 960;
131
- height: 280 | 320;
140
+ width: number;
141
+ height: number;
132
142
  };
143
+ /**
144
+ * The size of a `fluid` chart: its container's width, measured, at `height`.
145
+ *
146
+ * @param height - The chart's height in CSS pixels. Defaults to {@link FLUID_DEFAULT_HEIGHT}.
147
+ * @returns `[ref, size]`. Attach `ref` to the element whose width the chart takes (the
148
+ * `Frame`'s `figureRef`). The size is a 480 px-wide default until the first measure.
149
+ */
150
+ export declare function useFluidSize(height?: number): [(element: HTMLElement | null) => void, {
151
+ width: number;
152
+ height: number;
153
+ }];
133
154
  /** Props for {@link Frame}. */
134
155
  interface FrameProps {
135
156
  /** The horizontal scale, built over this variant's `x0`…`x1` (see {@link areaFor}). */
@@ -154,6 +175,13 @@ interface FrameProps {
154
175
  variant?: Variant;
155
176
  /** Extra classes for the outer `<figure>`, merged with `cn`. */
156
177
  className?: string | undefined;
178
+ /** For `fluid`: the measured size (see {@link useFluidSize}). */
179
+ size?: {
180
+ width: number;
181
+ height: number;
182
+ } | undefined;
183
+ /** A ref to the outer `<figure>`, e.g. {@link useFluidSize}'s measuring ref. */
184
+ figureRef?: Ref<HTMLElement> | undefined;
157
185
  }
158
186
  /**
159
187
  * The plot shell every chart here composes: grid, ticks, axes, axis titles, a footer.
@@ -162,7 +190,7 @@ interface FrameProps {
162
190
  * Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
163
191
  * as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
164
192
  */
165
- export declare function Frame({ xScale, yScale, xLabel, yLabel, xTicks, yTicks, children, label, footer, variant, className }: FrameProps): import("react").JSX.Element;
193
+ export declare function Frame({ xScale, yScale, xLabel, yLabel, xTicks, yTicks, children, label, footer, variant, className, size, figureRef }: FrameProps): import("react").JSX.Element;
166
194
  /** One entry of a {@link Legend}. */
167
195
  interface LegendItem {
168
196
  /** The series name, rendered as text — it is what a screen reader announces. */
@@ -230,6 +258,102 @@ export declare const DEFECT_COLOUR = "var(--defect)";
230
258
  */
231
259
  export declare function seriesColour(index: number): string;
232
260
  //#endregion
261
+ //#region src/interaction.d.ts
262
+ /** An x range marked on the plot: a gap, a stretch out of tolerance, a region of interest. */
263
+ interface Band {
264
+ /** Start of the range, in x domain units. */
265
+ from: number;
266
+ /** End of the range. */
267
+ to: number;
268
+ /** Its colour. Default `"muted"`. */
269
+ tone?: MeasureTone | undefined;
270
+ /** Text at the top of the band. */
271
+ label?: string | undefined;
272
+ }
273
+ /** A position marked on the x axis with a short tick: a station, a sample, a frame. */
274
+ interface Marker {
275
+ /** Where, in x domain units. */
276
+ position: number;
277
+ /** Its colour. Default `"muted"`. */
278
+ tone?: MeasureTone | undefined;
279
+ /** Shown as the tick's tooltip (`<title>`). */
280
+ label?: string | undefined;
281
+ }
282
+ /** A series as the readout sees it. */
283
+ interface ReadoutSeries {
284
+ /** Its name. */
285
+ name: string;
286
+ /** Its points. */
287
+ points: readonly {
288
+ x: number;
289
+ y: number;
290
+ }[];
291
+ /** Its colour. */
292
+ colour: string;
293
+ }
294
+ /** The props `LineChart` and `LineProfile` share for interaction and marking. */
295
+ interface InteractionProps {
296
+ /** x ranges drawn behind the series. */
297
+ bands?: readonly Band[] | undefined;
298
+ /** Positions ticked on the x axis. */
299
+ markers?: readonly Marker[] | undefined;
300
+ /** An x position drawn as a vertical rule, from outside the chart (e.g. a canvas hover). */
301
+ cursor?: number | null | undefined;
302
+ /** Called with the x under the pointer (in domain units, clamped to the domain), or `null` on leaving. */
303
+ onHover?: ((x: number | null) => void) | undefined;
304
+ /** Called with the x under a click. Makes the plot area a pointer target. */
305
+ onPick?: ((x: number) => void) | undefined;
306
+ }
307
+ /** Plot-area edges in viewBox units, as `areaFor` returns them (`y0` is the bottom). */
308
+ interface PlotArea {
309
+ /** Left edge. */
310
+ x0: number;
311
+ /** Right edge. */
312
+ x1: number;
313
+ /** Bottom edge. */
314
+ y0: number;
315
+ /** Top edge. */
316
+ y1: number;
317
+ }
318
+ /** The ranges behind the series. */
319
+ export declare function Bands({ bands, xScale, area }: {
320
+ bands: readonly Band[];
321
+ xScale: Scale;
322
+ area: PlotArea;
323
+ }): import("react").JSX.Element;
324
+ /** The ticks on the x axis. */
325
+ export declare function Markers({ markers, xScale, area }: {
326
+ markers: readonly Marker[];
327
+ xScale: Scale;
328
+ area: PlotArea;
329
+ }): import("react").JSX.Element;
330
+ /**
331
+ * The pointer layer over the plot area: a crosshair and a value readout while hovering, the
332
+ * external `cursor` otherwise, and clicks as picks.
333
+ */
334
+ export declare function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format }: {
335
+ xScale: Scale;
336
+ yScale: Scale;
337
+ area: PlotArea;
338
+ series: readonly ReadoutSeries[];
339
+ cursor?: number | null | undefined;
340
+ onHover?: ((x: number | null) => void) | undefined;
341
+ onPick?: ((x: number) => void) | undefined;
342
+ format?: (value: number) => string;
343
+ }): import("react").JSX.Element;
344
+ /**
345
+ * A series' value at `x`, linearly interpolated between the points either side, or `null`
346
+ * outside its range or across a gap.
347
+ *
348
+ * @param points - The series, in ascending `x`.
349
+ * @param x - Where.
350
+ * @returns The interpolated value, or `null`.
351
+ */
352
+ export declare function seriesValueAt(points: readonly {
353
+ x: number;
354
+ y: number;
355
+ }[], x: number): number | null;
356
+ //#endregion
233
357
  //#region src/LineChart.d.ts
234
358
  /** One line of a {@link LineChart}. */
235
359
  interface Series {
@@ -247,7 +371,7 @@ interface Series {
247
371
  colour?: string;
248
372
  }
249
373
  /** Props for {@link LineChart}. */
250
- interface LineChartProps {
374
+ interface LineChartProps extends InteractionProps {
251
375
  /** The lines, drawn in order — a later series paints over an earlier one. */
252
376
  series: Series[];
253
377
  /** Accessible name of the chart (the SVG's `aria-label`). Required. */
@@ -272,15 +396,19 @@ interface LineChartProps {
272
396
  variant?: Variant;
273
397
  /** Extra classes for the outer `<figure>`, merged with `cn`. */
274
398
  className?: string | undefined;
399
+ /** For `variant="fluid"`: the chart's height in CSS pixels. Default 200. */
400
+ height?: number | undefined;
275
401
  }
276
402
  /**
277
403
  * A multi-series line chart over a shared x axis — training curves, ROC and PR curves.
278
404
  *
279
405
  * @remarks
280
406
  * An SVG `role="img"` named by `label`, with the legend as real text under it. Series are
281
- * told apart by the legend's names, never by colour alone.
407
+ * told apart by the legend's names, never by colour alone. `bands` and `markers` mark x
408
+ * ranges and positions; `onHover` adds a crosshair and a value readout, `onPick` makes a
409
+ * click report its x, and `cursor` draws an x chosen elsewhere.
282
410
  */
283
- export declare function LineChart({ series, label, xLabel, yLabel, logY, xDomain, yDomain, underlay, footer, showLegend, variant, className }: LineChartProps): import("react").JSX.Element;
411
+ export declare function LineChart({ series, label, xLabel, yLabel, logY, xDomain, yDomain, underlay, footer, showLegend, variant, className, height, bands, markers, cursor, onHover, onPick }: LineChartProps): import("react").JSX.Element;
284
412
  //#endregion
285
413
  //#region src/BarChart.d.ts
286
414
  /**
@@ -382,7 +510,7 @@ interface EdgeMark {
382
510
  tone?: MeasureTone;
383
511
  }
384
512
  /** Props for {@link LineProfile}. */
385
- interface LineProfileProps {
513
+ interface LineProfileProps extends InteractionProps {
386
514
  /** The profiles, drawn in order. */
387
515
  series: ProfileSeries[];
388
516
  /** Detected edge positions, drawn as vertical rules over the series. */
@@ -405,6 +533,8 @@ interface LineProfileProps {
405
533
  variant?: Variant;
406
534
  /** Extra classes for the outer `<figure>`, merged with `cn`. */
407
535
  className?: string | undefined;
536
+ /** For `variant="fluid"`: the chart's height in CSS pixels. Default 200. */
537
+ height?: number | undefined;
408
538
  }
409
539
  /**
410
540
  * One or more series against arc length along a scan line or caliper axis, with detected
@@ -412,9 +542,87 @@ interface LineProfileProps {
412
542
  *
413
543
  * @remarks
414
544
  * An SVG `role="img"` named by `label`. The legend appears only for two or more series — a
415
- * single profile is named by the chart's label.
545
+ * single profile is named by the chart's label. `bands`, `markers`, `cursor`, `onHover` and
546
+ * `onPick` work as on `LineChart`: a caliper's profile can follow the canvas, and a click
547
+ * can pick a position along it.
548
+ */
549
+ export declare function LineProfile({ series, edges, label, xLabel, yLabel, xDomain, yDomain, footer, showLegend, variant, className, height, bands, markers, cursor, onHover, onPick }: LineProfileProps): import("react").JSX.Element;
550
+ //#endregion
551
+ //#region src/colormap.d.ts
552
+ /**
553
+ * Sequential colour maps for a magnitude (visual-language §4): a residual's size, a per-cell
554
+ * error, a confidence, a validity fraction.
555
+ *
556
+ * Only perceptually uniform maps ship, so equal steps in the data look like equal steps.
557
+ * - `viridis` is the default on the chrome.
558
+ * - `cividis` is for when the map alone carries the finding and the reader's colour vision is
559
+ * unknown.
560
+ *
561
+ * A continuous quality is a magnitude even when low values are "good": stepping it onto verdict
562
+ * colours (green / amber / red) claims a threshold judgement nobody made.
563
+ *
564
+ * The anchors are matplotlib's maps sampled at nine evenly spaced points. Linear interpolation
565
+ * between them stays within a few units of the full 256-entry tables.
566
+ */
567
+ /** The shipped maps. */
568
+ type ColormapName = "viridis" | "cividis";
569
+ /** The names of the shipped maps. */
570
+ export declare const COLORMAPS: readonly ColormapName[];
571
+ /**
572
+ * The colour of `t` on a map.
573
+ *
574
+ * @param name - The map.
575
+ * @param t - Position in `[0, 1]`; clamped. A non-finite `t` gives the map's low end.
576
+ * @returns A CSS `rgb(r g b)` colour.
577
+ */
578
+ export declare function colormap(name: ColormapName, t: number): string;
579
+ /**
580
+ * The colour of `t` on a map, as integer channels.
581
+ *
582
+ * @param name - The map.
583
+ * @param t - Position in `[0, 1]`; clamped.
584
+ * @returns `[r, g, b]`, each 0–255.
585
+ */
586
+ export declare function colormapRgb(name: ColormapName, t: number): [number, number, number];
587
+ /**
588
+ * A value's colour on a map, over a domain.
589
+ *
590
+ * @param name - The map.
591
+ * @param value - The value.
592
+ * @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
593
+ * @returns A CSS colour.
594
+ */
595
+ export declare function colormapValue(name: ColormapName, value: number, domain: readonly [number, number]): string;
596
+ /**
597
+ * A CSS `linear-gradient` of a map, for a legend bar.
598
+ *
599
+ * @param name - The map.
600
+ * @param direction - The gradient's direction. Defaults to `to right`.
601
+ * @returns A CSS `background-image` value.
602
+ */
603
+ export declare function colormapGradient(name: ColormapName, direction?: string): string;
604
+ //#endregion
605
+ //#region src/ColormapLegend.d.ts
606
+ /** Props of `ColormapLegend`. */
607
+ interface ColormapLegendProps {
608
+ /** The map. */
609
+ map: ColormapName;
610
+ /** The values at the two ends, `[low, high]`. */
611
+ domain: readonly [number, number];
612
+ /** The values' unit, written after them (`px`, `%`). */
613
+ unit?: string | undefined;
614
+ /** What the colour encodes, written before the bar ("Validity", "Edge error"). */
615
+ label: string;
616
+ /** Formats the end values. Defaults to at most 3 significant figures, without trailing zeros. */
617
+ format?: ((value: number) => string) | undefined;
618
+ /** Merged onto the row with `cn`. */
619
+ className?: string | undefined;
620
+ }
621
+ /**
622
+ * A gradient bar with its end values and unit, read as one line: "Edge error 0 [bar] 2.5 px".
623
+ * The bar is decorative; the label and the end values carry the meaning as text.
416
624
  */
417
- export declare function LineProfile({ series, edges, label, xLabel, yLabel, xDomain, yDomain, footer, showLegend, variant, className }: LineProfileProps): import("react").JSX.Element;
625
+ export declare function ColormapLegend({ map, domain, unit, label, format, className }: ColormapLegendProps): import("react").JSX.Element;
418
626
  //#endregion
419
- export type { BarRow, EdgeMark, FrameProps, HistogramProps, LegendItem, LegendProps, LineChartProps, LineProfileProps, ProfileSeries, Scale, Series, StackedBarsProps, Tick, Variant };
627
+ export type { Band, BarRow, ColormapLegendProps, ColormapName, EdgeMark, FrameProps, HistogramProps, InteractionProps, LegendItem, LegendProps, LineChartProps, LineProfileProps, Marker, PlotArea, ProfileSeries, ReadoutSeries, Scale, Series, StackedBarsProps, Tick, Variant };
420
628
  //# sourceMappingURL=index.d.ts.map