@milktop/react-ui 0.3.23 → 0.3.24
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.
|
@@ -18,6 +18,7 @@ export type AreaChartProps = ChartProps & {
|
|
|
18
18
|
zero?: boolean;
|
|
19
19
|
time?: boolean;
|
|
20
20
|
formatX?: ((value: unknown) => string) | null;
|
|
21
|
+
xTicks?: (Date | string)[] | null;
|
|
21
22
|
tooltipLabel?: ((row: ChartRow) => string) | null;
|
|
22
23
|
};
|
|
23
|
-
export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, zero, time, formatX, tooltipLabel, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
|
|
24
|
+
export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, zero, time, formatX, xTicks, tooltipLabel, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
|
package/dist/chart/AreaChart.js
CHANGED
|
@@ -14,6 +14,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
14
14
|
* - `time`: `x` holds dates (Date, or '2024-03-08'), rows in date order: each point sits by its
|
|
15
15
|
* date, so a year's gap looks like one, and the axis is marked in months and years
|
|
16
16
|
* - `formatX(value)`: the axis' labels (a Date when `time`); `tooltipLabel(row)`: the tooltip's heading
|
|
17
|
+
* - `xTicks`: where `time` marks its axis (dates), in place of its months and years: a birthday a year
|
|
17
18
|
* - `height` (240 px), `format(value)` for ticks and tooltips, `grid` (lines; true)
|
|
18
19
|
* - `label`: what it shows, for assistive tech (a hidden table of the data)
|
|
19
20
|
*/
|
|
@@ -24,7 +25,7 @@ import { area, line, curveMonotoneX, curveLinear } from 'd3-shape';
|
|
|
24
25
|
import { ChartFrame } from './ChartFrame.js';
|
|
25
26
|
import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate } from './chart.js';
|
|
26
27
|
import { partsOf } from '../utils.js';
|
|
27
|
-
export function AreaChart({ data = [], x = 'label', y = null, series = null, height = 240, format = null, label: labelProp, grid = true, curve = 'smooth', stacked = false, zero = true, time = false, formatX = null, tooltipLabel = null, classes, ...rest }) {
|
|
28
|
+
export function AreaChart({ data = [], x = 'label', y = null, series = null, height = 240, format = null, label: labelProp, grid = true, curve = 'smooth', stacked = false, zero = true, time = false, formatX = null, xTicks = null, tooltipLabel = null, classes, ...rest }) {
|
|
28
29
|
const t = useMessages();
|
|
29
30
|
const label = labelProp ?? t.chart.label;
|
|
30
31
|
const part = partsOf('chart', classes);
|
|
@@ -61,7 +62,12 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
|
|
|
61
62
|
: (value) => String(value ?? ''));
|
|
62
63
|
const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
|
|
63
64
|
// About one date every 80px, on whole months and years
|
|
64
|
-
const
|
|
65
|
+
const [start, end] = byDate.domain();
|
|
66
|
+
const timeTicks = !time || rows.length < 2 ? []
|
|
67
|
+
: xTicks ? xTicks.map(toDate).filter((tick) => tick >= start && tick <= end)
|
|
68
|
+
: byDate.ticks(Math.max(2, Math.floor(innerW / 80)));
|
|
69
|
+
// A mark near an end is drawn inward from it, so its label stays inside the chart
|
|
70
|
+
const anchorAt = (pos) => (pos < 40 ? 'start' : pos > innerW - 40 ? 'end' : 'middle');
|
|
65
71
|
const shape = curve === 'linear' ? curveLinear : curveMonotoneX;
|
|
66
72
|
// Fitted, the fill runs down to the axis' foot rather than to 0, which is off the chart
|
|
67
73
|
const fill = area().x((_, i) => at(i)).y0((d) => (fitted ? innerH : ys(d[0]))).y1((d) => ys(d[1])).curve(shape);
|
|
@@ -77,5 +83,5 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
|
|
|
77
83
|
nearest = i; });
|
|
78
84
|
setHover(nearest);
|
|
79
85
|
}
|
|
80
|
-
return (_jsx(ChartFrame, { ...rest, rows: rows, x: x, labelOf: labelOf, list: list, fmt: fmt, height: height, label: label, classes: classes, hover: hover, onHover: setHover, onMove: move, width: width, onWidth: setWidth, tipLeft: hover == null ? 0 : left + at(hover), flip: hover != null && at(hover) > innerW * 0.6, children: _jsxs("svg", { ...part('svg'), width: width, height: height, "aria-hidden": "true", children: [_jsx("defs", { children: list.map((s, i) => (_jsxs("linearGradient", { id: `${id}-${i}`, x1: "0", x2: "0", y1: "0", y2: "1", children: [_jsx("stop", { offset: "0%", stopColor: s.color, stopOpacity: "0.28" }), _jsx("stop", { offset: "100%", stopColor: s.color, stopOpacity: "0.02" })] }, s.key))) }), _jsxs("g", { transform: `translate(${left},${pad.top})`, children: [ticks.map((tick) => (_jsxs("g", { children: [grid && _jsx("line", { ...part('grid'), x1: "0", x2: innerW, y1: ys(tick), y2: ys(tick) }), _jsx("text", { ...part('tick'), x: "-8", y: ys(tick), dy: "0.32em", textAnchor: "end", children: fmt(tick) })] }, tick))), time ? timeTicks.map((tick) => (_jsx("text", { ...part('tick'), x: byDate(tick), y: innerH + 18, textAnchor:
|
|
86
|
+
return (_jsx(ChartFrame, { ...rest, rows: rows, x: x, labelOf: labelOf, list: list, fmt: fmt, height: height, label: label, classes: classes, hover: hover, onHover: setHover, onMove: move, width: width, onWidth: setWidth, tipLeft: hover == null ? 0 : left + at(hover), flip: hover != null && at(hover) > innerW * 0.6, children: _jsxs("svg", { ...part('svg'), width: width, height: height, "aria-hidden": "true", children: [_jsx("defs", { children: list.map((s, i) => (_jsxs("linearGradient", { id: `${id}-${i}`, x1: "0", x2: "0", y1: "0", y2: "1", children: [_jsx("stop", { offset: "0%", stopColor: s.color, stopOpacity: "0.28" }), _jsx("stop", { offset: "100%", stopColor: s.color, stopOpacity: "0.02" })] }, s.key))) }), _jsxs("g", { transform: `translate(${left},${pad.top})`, children: [ticks.map((tick) => (_jsxs("g", { children: [grid && _jsx("line", { ...part('grid'), x1: "0", x2: innerW, y1: ys(tick), y2: ys(tick) }), _jsx("text", { ...part('tick'), x: "-8", y: ys(tick), dy: "0.32em", textAnchor: "end", children: fmt(tick) })] }, tick))), time ? timeTicks.map((tick) => (_jsx("text", { ...part('tick'), x: byDate(tick), y: innerH + 18, textAnchor: anchorAt(byDate(tick)), children: tickLabel(tick) }, +tick))) : rows.map((row, i) => i % step === 0 && (_jsx("text", { ...part('tick'), x: at(i), y: innerH + 18, textAnchor: i === 0 ? 'start' : i === rows.length - 1 ? 'end' : 'middle', children: tickLabel(row[x]) }, i))), list.map((s, i) => (_jsxs("g", { children: [_jsx("path", { ...part('area'), d: fill(bands[i]) ?? '', fill: `url(#${id}-${i})` }), _jsx("path", { ...part('line'), d: stroke(bands[i]) ?? '', stroke: s.color })] }, s.key))), hover != null && _jsxs(_Fragment, { children: [_jsx("line", { ...part('cursor'), x1: at(hover), x2: at(hover), y1: "0", y2: innerH }), list.map((s, i) => _jsx("circle", { ...part('dot'), cx: at(hover), cy: ys(bands[i][hover][1]), r: "4", fill: s.color }, s.key))] })] })] }) }));
|
|
81
87
|
}
|