@vitavision/charts 0.8.4 → 0.8.5
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/dist/index.d.ts +23 -2
- package/dist/index.js +145 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/Frame.tsx +8 -1
- package/src/Histogram.tsx +36 -3
- package/src/ThresholdRule.tsx +141 -2
- package/src/interaction.tsx +6 -1
package/dist/index.d.ts
CHANGED
|
@@ -182,6 +182,12 @@ interface FrameProps {
|
|
|
182
182
|
width: number;
|
|
183
183
|
height: number;
|
|
184
184
|
} | undefined;
|
|
185
|
+
/**
|
|
186
|
+
* The plot holds focusable controls (a draggable threshold). The SVG is then a
|
|
187
|
+
* `role="group"` named by `label` instead of an `img`, whose children assistive
|
|
188
|
+
* technology would otherwise hide.
|
|
189
|
+
*/
|
|
190
|
+
interactive?: boolean | undefined;
|
|
185
191
|
/** A ref to the outer `<figure>`, e.g. {@link useFluidSize}'s measuring ref. */
|
|
186
192
|
figureRef?: Ref<HTMLElement> | undefined;
|
|
187
193
|
}
|
|
@@ -192,7 +198,7 @@ interface FrameProps {
|
|
|
192
198
|
* Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
|
|
193
199
|
* as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
|
|
194
200
|
*/
|
|
195
|
-
export declare function Frame({ xScale, yScale, xLabel, yLabel, xTicks, yTicks, xFormat, children, label, footer, variant, className, size, figureRef }: FrameProps): import("react").JSX.Element;
|
|
201
|
+
export declare function Frame({ xScale, yScale, xLabel, yLabel, xTicks, yTicks, xFormat, children, label, footer, variant, className, size, figureRef, interactive }: FrameProps): import("react").JSX.Element;
|
|
196
202
|
/** One entry of a {@link Legend}. */
|
|
197
203
|
interface LegendItem {
|
|
198
204
|
/** The series name, rendered as text — it is what a screen reader announces. */
|
|
@@ -765,6 +771,21 @@ interface HistogramOptions extends InteractionProps {
|
|
|
765
771
|
* draws its own). Omitted or non-finite: none. For other x positions use `markers`.
|
|
766
772
|
*/
|
|
767
773
|
threshold?: number | undefined;
|
|
774
|
+
/**
|
|
775
|
+
* Makes `threshold` draggable: called with the new x, in data units and clamped to the
|
|
776
|
+
* domain, while the rule is dragged or stepped. The chart does not keep the value — move
|
|
777
|
+
* `threshold` to follow it. Needs a `threshold`. The rule becomes a focusable slider
|
|
778
|
+
* (Arrow keys step one bin, Page keys ten, Home and End go to the ends), and the SVG a
|
|
779
|
+
* `group` rather than an `img`.
|
|
780
|
+
*/
|
|
781
|
+
onThresholdChange?: ((x: number) => void) | undefined;
|
|
782
|
+
/** The threshold slider's accessible name. Default "Threshold". */
|
|
783
|
+
thresholdLabel?: string | undefined;
|
|
784
|
+
/**
|
|
785
|
+
* Freezes a draggable threshold. A string is the reason, shown as the rule's tooltip
|
|
786
|
+
* (`"Locked by the saved model"`).
|
|
787
|
+
*/
|
|
788
|
+
thresholdDisabled?: boolean | string | undefined;
|
|
768
789
|
/** Counts span decades in a long-tailed distribution; a log axis keeps the tail visible. */
|
|
769
790
|
logY?: boolean | undefined;
|
|
770
791
|
/** Where the chart is going (see {@link Variant}). Default `"panel"`. */
|
|
@@ -807,7 +828,7 @@ type HistogramProps = HistogramOptions & (HistogramValues | HistogramCounts);
|
|
|
807
828
|
* @remarks
|
|
808
829
|
* An SVG `role="img"` named by `label`. `threshold` draws a dashed decision rule, `markers`
|
|
809
830
|
* tick the x axis, `bands` shade x ranges, `onHover` adds a crosshair and the x under it,
|
|
810
|
-
* `onPick` reports the x of a click, and `cursor` draws the bin containing an x chosen
|
|
831
|
+
* `onPick` reports the x of a click, `onThresholdChange` makes the threshold a draggable slider, and `cursor` draws the bin containing an x chosen
|
|
811
832
|
* elsewhere in the selection colour (`--signal`) — no highlight when it lies outside the
|
|
812
833
|
* domain. Empty data draws an empty frame. For two classes sharing a threshold use
|
|
813
834
|
* {@link ScoreHistogram}.
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useId, useMemo, useState } from "react";
|
|
1
|
+
import { useCallback, useId, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { cn, toneColor } from "@vitavision/ui";
|
|
3
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
//#region src/Frame.tsx
|
|
@@ -93,7 +93,7 @@ const AXIS = "currentColor";
|
|
|
93
93
|
* Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
|
|
94
94
|
* as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
|
|
95
95
|
*/
|
|
96
|
-
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, xFormat, children, label, footer, variant = "panel", className, size, figureRef }) {
|
|
96
|
+
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, xFormat, children, label, footer, variant = "panel", className, size, figureRef, interactive = false }) {
|
|
97
97
|
const horizontal = xScale.ticks(xTicks);
|
|
98
98
|
const vertical = yScale.ticks(yTicks);
|
|
99
99
|
const plotArea = areaFor(variant, size);
|
|
@@ -105,7 +105,7 @@ function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, xFormat
|
|
|
105
105
|
ref: figureRef,
|
|
106
106
|
className: cn("flex flex-col gap-1 text-fg-muted", className),
|
|
107
107
|
children: [/* @__PURE__ */ jsxs("svg", {
|
|
108
|
-
role: "img",
|
|
108
|
+
role: interactive ? "group" : "img",
|
|
109
109
|
"aria-label": label,
|
|
110
110
|
viewBox: `0 0 ${PLOT.width} ${PLOT.height}`,
|
|
111
111
|
className: "w-full",
|
|
@@ -611,11 +611,15 @@ function Markers({ markers, xScale, area }) {
|
|
|
611
611
|
})
|
|
612
612
|
});
|
|
613
613
|
}
|
|
614
|
+
/** The default readout format: whole numbers from 100 up, three significant digits below. */
|
|
615
|
+
function defaultFormat$1(value) {
|
|
616
|
+
return Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3);
|
|
617
|
+
}
|
|
614
618
|
/**
|
|
615
619
|
* The pointer layer over the plot area: a crosshair and a value readout while hovering, the
|
|
616
620
|
* external `cursor` otherwise, and clicks as picks.
|
|
617
621
|
*/
|
|
618
|
-
function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format =
|
|
622
|
+
function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format = defaultFormat$1, xFormat }) {
|
|
619
623
|
const [hover, setHover] = useState(null);
|
|
620
624
|
const interactive = onHover !== void 0 || onPick !== void 0;
|
|
621
625
|
const xAt = (event) => {
|
|
@@ -1895,6 +1899,11 @@ function tallest(...series) {
|
|
|
1895
1899
|
}
|
|
1896
1900
|
//#endregion
|
|
1897
1901
|
//#region src/ThresholdRule.tsx
|
|
1902
|
+
/**
|
|
1903
|
+
* The decision threshold of a histogram: a dashed rule across the plot at one x, and — when
|
|
1904
|
+
* the caller can take a new value — a handle on it that a pointer drags and the arrow keys
|
|
1905
|
+
* step.
|
|
1906
|
+
*/
|
|
1898
1907
|
/** A dashed vertical rule at `value`; nothing when it is missing or not finite. */
|
|
1899
1908
|
function ThresholdRule({ value, xScale, area }) {
|
|
1900
1909
|
if (value === void 0 || !Number.isFinite(value)) return null;
|
|
@@ -1910,6 +1919,123 @@ function ThresholdRule({ value, xScale, area }) {
|
|
|
1910
1919
|
strokeDasharray: "3 2"
|
|
1911
1920
|
});
|
|
1912
1921
|
}
|
|
1922
|
+
/** How wide the grab area around the rule is, in viewBox units. */
|
|
1923
|
+
const GRAB = 12;
|
|
1924
|
+
/**
|
|
1925
|
+
* The threshold as a slider: the dashed rule plus a wider invisible grab area and a small
|
|
1926
|
+
* handle at the top. Pointer capture keeps a drag going outside the plot.
|
|
1927
|
+
*/
|
|
1928
|
+
function DraggableThreshold({ value, xScale, area, step, onChange, label, unit, disabled = false }) {
|
|
1929
|
+
const dragging = useRef(false);
|
|
1930
|
+
if (value === void 0 || !Number.isFinite(value)) return null;
|
|
1931
|
+
const [low, high] = [Math.min(...xScale.domain), Math.max(...xScale.domain)];
|
|
1932
|
+
const off = disabled !== false;
|
|
1933
|
+
const reason = typeof disabled === "string" ? disabled : void 0;
|
|
1934
|
+
const x = xScale.project(value);
|
|
1935
|
+
const emit = (next) => onChange(Math.min(high, Math.max(low, next)));
|
|
1936
|
+
const xAt = (event) => {
|
|
1937
|
+
const svg = event.currentTarget.ownerSVGElement;
|
|
1938
|
+
const ctm = svg?.getScreenCTM();
|
|
1939
|
+
if (!svg || !ctm) return value;
|
|
1940
|
+
const point = svg.createSVGPoint();
|
|
1941
|
+
point.x = event.clientX;
|
|
1942
|
+
point.y = event.clientY;
|
|
1943
|
+
return xScale.invert(point.matrixTransform(ctm.inverse()).x);
|
|
1944
|
+
};
|
|
1945
|
+
const keyStep = (key) => {
|
|
1946
|
+
switch (key) {
|
|
1947
|
+
case "ArrowRight":
|
|
1948
|
+
case "ArrowUp": return value + step;
|
|
1949
|
+
case "ArrowLeft":
|
|
1950
|
+
case "ArrowDown": return value - step;
|
|
1951
|
+
case "PageUp": return value + 10 * step;
|
|
1952
|
+
case "PageDown": return value - 10 * step;
|
|
1953
|
+
case "Home": return low;
|
|
1954
|
+
case "End": return high;
|
|
1955
|
+
default: return null;
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
1959
|
+
"data-threshold-handle": "",
|
|
1960
|
+
"data-disabled": off ? "" : void 0,
|
|
1961
|
+
role: "slider",
|
|
1962
|
+
tabIndex: off ? -1 : 0,
|
|
1963
|
+
"aria-label": label,
|
|
1964
|
+
"aria-orientation": "horizontal",
|
|
1965
|
+
"aria-valuemin": low,
|
|
1966
|
+
"aria-valuemax": high,
|
|
1967
|
+
"aria-valuenow": value,
|
|
1968
|
+
"aria-valuetext": `${defaultFormat$1(value)}${unit ? ` ${unit}` : ""}`,
|
|
1969
|
+
"aria-disabled": off || void 0,
|
|
1970
|
+
className: "group text-fg outline-none",
|
|
1971
|
+
style: {
|
|
1972
|
+
cursor: off ? "not-allowed" : "col-resize",
|
|
1973
|
+
touchAction: "none"
|
|
1974
|
+
},
|
|
1975
|
+
onKeyDown: (event) => {
|
|
1976
|
+
if (off) return;
|
|
1977
|
+
const next = keyStep(event.key);
|
|
1978
|
+
if (next === null) return;
|
|
1979
|
+
event.preventDefault();
|
|
1980
|
+
emit(next);
|
|
1981
|
+
},
|
|
1982
|
+
onPointerDown: (event) => {
|
|
1983
|
+
if (off) return;
|
|
1984
|
+
dragging.current = true;
|
|
1985
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
1986
|
+
event.currentTarget.focus();
|
|
1987
|
+
emit(xAt(event));
|
|
1988
|
+
},
|
|
1989
|
+
onPointerMove: (event) => {
|
|
1990
|
+
if (dragging.current) emit(xAt(event));
|
|
1991
|
+
},
|
|
1992
|
+
onPointerUp: (event) => {
|
|
1993
|
+
dragging.current = false;
|
|
1994
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
|
|
1995
|
+
},
|
|
1996
|
+
onPointerCancel: () => {
|
|
1997
|
+
dragging.current = false;
|
|
1998
|
+
},
|
|
1999
|
+
children: [
|
|
2000
|
+
reason !== void 0 && /* @__PURE__ */ jsx("title", { children: reason }),
|
|
2001
|
+
/* @__PURE__ */ jsx("rect", {
|
|
2002
|
+
x: x - GRAB / 2,
|
|
2003
|
+
y: area.y1,
|
|
2004
|
+
width: GRAB,
|
|
2005
|
+
height: area.y0 - area.y1,
|
|
2006
|
+
fill: "transparent"
|
|
2007
|
+
}),
|
|
2008
|
+
/* @__PURE__ */ jsx("line", {
|
|
2009
|
+
"data-threshold": "",
|
|
2010
|
+
x1: x,
|
|
2011
|
+
x2: x,
|
|
2012
|
+
y1: area.y0,
|
|
2013
|
+
y2: area.y1,
|
|
2014
|
+
stroke: "currentColor",
|
|
2015
|
+
strokeWidth: 1.25,
|
|
2016
|
+
strokeDasharray: "3 2",
|
|
2017
|
+
opacity: off ? .6 : 1
|
|
2018
|
+
}),
|
|
2019
|
+
/* @__PURE__ */ jsx("path", {
|
|
2020
|
+
"data-handle": "",
|
|
2021
|
+
d: `M${x - 4} ${area.y1 - 1} h8 l-4 6 z`,
|
|
2022
|
+
fill: "currentColor",
|
|
2023
|
+
opacity: off ? .6 : 1
|
|
2024
|
+
}),
|
|
2025
|
+
/* @__PURE__ */ jsx("rect", {
|
|
2026
|
+
x: x - GRAB / 2,
|
|
2027
|
+
y: area.y1 - 2,
|
|
2028
|
+
width: GRAB,
|
|
2029
|
+
height: area.y0 - area.y1 + 2,
|
|
2030
|
+
rx: 2,
|
|
2031
|
+
fill: "none",
|
|
2032
|
+
stroke: "var(--signal)",
|
|
2033
|
+
strokeWidth: 2,
|
|
2034
|
+
className: "hidden group-focus-visible:block"
|
|
2035
|
+
})
|
|
2036
|
+
]
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
1913
2039
|
//#endregion
|
|
1914
2040
|
//#region src/Histogram.tsx
|
|
1915
2041
|
/**
|
|
@@ -1918,13 +2044,13 @@ function ThresholdRule({ value, xScale, area }) {
|
|
|
1918
2044
|
* @remarks
|
|
1919
2045
|
* An SVG `role="img"` named by `label`. `threshold` draws a dashed decision rule, `markers`
|
|
1920
2046
|
* tick the x axis, `bands` shade x ranges, `onHover` adds a crosshair and the x under it,
|
|
1921
|
-
* `onPick` reports the x of a click, and `cursor` draws the bin containing an x chosen
|
|
2047
|
+
* `onPick` reports the x of a click, `onThresholdChange` makes the threshold a draggable slider, and `cursor` draws the bin containing an x chosen
|
|
1922
2048
|
* elsewhere in the selection colour (`--signal`) — no highlight when it lies outside the
|
|
1923
2049
|
* domain. Empty data draws an empty frame. For two classes sharing a threshold use
|
|
1924
2050
|
* {@link ScoreHistogram}.
|
|
1925
2051
|
*/
|
|
1926
2052
|
function Histogram(props) {
|
|
1927
|
-
const { label, xLabel, yLabel, unit, logY = false, variant = "panel", height = 200, footer, className, threshold, bands, markers, cursor, onHover, onPick } = props;
|
|
2053
|
+
const { label, xLabel, yLabel, unit, logY = false, variant = "panel", height = 200, footer, className, threshold, onThresholdChange, thresholdLabel = "Threshold", thresholdDisabled = false, bands, markers, cursor, onHover, onPick } = props;
|
|
1928
2054
|
const { counts, domain } = props.counts !== void 0 ? cleanCounts(props.counts, props.domain) : binValues(props.values, props.bins, props.domain);
|
|
1929
2055
|
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
1930
2056
|
const size = variant === "fluid" ? fluidSize : void 0;
|
|
@@ -1934,6 +2060,7 @@ function Histogram(props) {
|
|
|
1934
2060
|
const yScale = logY ? logScale([.5, top], plotArea.y0, plotArea.y1) : linearScale([0, top], plotArea.y0, plotArea.y1);
|
|
1935
2061
|
const highlighted = binAt(counts.length, domain, cursor);
|
|
1936
2062
|
const hasHighlight = highlighted !== null && (counts[highlighted] ?? 0) > 0;
|
|
2063
|
+
const draggable = onThresholdChange !== void 0 && threshold !== void 0 && Number.isFinite(threshold);
|
|
1937
2064
|
const xTitle = [xLabel, unit ? `(${unit})` : void 0].filter(Boolean).join(" ") || void 0;
|
|
1938
2065
|
return /* @__PURE__ */ jsxs(Frame, {
|
|
1939
2066
|
xScale,
|
|
@@ -1946,6 +2073,7 @@ function Histogram(props) {
|
|
|
1946
2073
|
size,
|
|
1947
2074
|
figureRef: variant === "fluid" ? fluidRef : void 0,
|
|
1948
2075
|
footer,
|
|
2076
|
+
interactive: draggable,
|
|
1949
2077
|
children: [
|
|
1950
2078
|
bands && bands.length > 0 && /* @__PURE__ */ jsx(Bands, {
|
|
1951
2079
|
bands,
|
|
@@ -1963,7 +2091,7 @@ function Histogram(props) {
|
|
|
1963
2091
|
d: barsPath(counts, yScale, plotArea, { only: highlighted }),
|
|
1964
2092
|
fill: "var(--signal)"
|
|
1965
2093
|
}),
|
|
1966
|
-
/* @__PURE__ */ jsx(ThresholdRule, {
|
|
2094
|
+
!draggable && /* @__PURE__ */ jsx(ThresholdRule, {
|
|
1967
2095
|
value: threshold,
|
|
1968
2096
|
xScale,
|
|
1969
2097
|
area: plotArea
|
|
@@ -1980,6 +2108,16 @@ function Histogram(props) {
|
|
|
1980
2108
|
series: [],
|
|
1981
2109
|
onHover,
|
|
1982
2110
|
onPick
|
|
2111
|
+
}),
|
|
2112
|
+
draggable && /* @__PURE__ */ jsx(DraggableThreshold, {
|
|
2113
|
+
value: threshold,
|
|
2114
|
+
xScale,
|
|
2115
|
+
area: plotArea,
|
|
2116
|
+
step: (domain[1] - domain[0]) / Math.max(1, counts.length),
|
|
2117
|
+
onChange: onThresholdChange,
|
|
2118
|
+
label: thresholdLabel,
|
|
2119
|
+
unit,
|
|
2120
|
+
disabled: thresholdDisabled
|
|
1983
2121
|
})
|
|
1984
2122
|
]
|
|
1985
2123
|
});
|