@milktop/react-ui 0.3.28 → 0.3.29
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/chart/AreaChart.js +9 -11
- package/dist/chart/chart.d.ts +6 -0
- package/dist/chart/chart.js +21 -0
- package/package.json +1 -1
package/dist/chart/AreaChart.js
CHANGED
|
@@ -12,7 +12,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
12
12
|
* - `zero`: the value axis starts at 0 (default); false fits it to the values, so a small change
|
|
13
13
|
* on a large value shows (a weight); stacked charts always start at 0
|
|
14
14
|
* - `time`: `x` holds dates (Date, or '2024-03-08'), rows in date order: each point sits by its
|
|
15
|
-
* date, so a year's gap looks like one, and the axis is marked in months
|
|
15
|
+
* date, so a year's gap looks like one, and the axis is marked in months or years (days when short)
|
|
16
16
|
* - `formatX(value)`: the axis' labels (a Date when `time`); `tooltipLabel(row)`: the tooltip's heading
|
|
17
17
|
* - `xTicks`: where `time` marks its axis (dates), in place of its months and years: a birthday a year
|
|
18
18
|
* - `height` (240 px), `format(value)` for ticks and tooltips, `grid` (lines; true)
|
|
@@ -23,7 +23,7 @@ import { useId, useState } from 'react';
|
|
|
23
23
|
import { scalePoint, scaleTime } from 'd3-scale';
|
|
24
24
|
import { area, line, curveMonotoneX, curveLinear } from 'd3-shape';
|
|
25
25
|
import { ChartFrame } from './ChartFrame.js';
|
|
26
|
-
import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate } from './chart.js';
|
|
26
|
+
import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate, timeTicks, timeLabel } from './chart.js';
|
|
27
27
|
import { partsOf } from '../utils.js';
|
|
28
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 }) {
|
|
29
29
|
const t = useMessages();
|
|
@@ -57,15 +57,13 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
|
|
|
57
57
|
const byDate = scaleTime().domain([dates[0] ?? new Date(), dates[dates.length - 1] ?? new Date()]).range([0, innerW]);
|
|
58
58
|
const evenly = scalePoint().domain(rows.map((_, i) => i)).range([0, innerW]);
|
|
59
59
|
const at = (i) => (time ? byDate(dates[i]) : evenly(i)) ?? 0;
|
|
60
|
-
|
|
61
|
-
? (value) => toDate(value).toLocaleDateString(locale, { month: 'short', year: 'numeric' })
|
|
62
|
-
: (value) => String(value ?? ''));
|
|
63
|
-
const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
|
|
64
|
-
// About one date every 80px, on whole months and years
|
|
60
|
+
// About one date every 80px
|
|
65
61
|
const [start, end] = byDate.domain();
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
const auto = timeTicks(start, end, Math.max(2, Math.floor(innerW / 80)), (count) => byDate.ticks(count));
|
|
63
|
+
const marks = !time || rows.length < 2 ? []
|
|
64
|
+
: xTicks ? xTicks.map(toDate).filter((tick) => tick >= start && tick <= end) : auto.ticks;
|
|
65
|
+
const tickLabel = formatX ?? (time ? timeLabel(auto.unit, locale) : (value) => String(value ?? ''));
|
|
66
|
+
const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
|
|
69
67
|
// A mark near an end is drawn inward from it, so its label stays inside the chart
|
|
70
68
|
const anchorAt = (pos) => (pos < 40 ? 'start' : pos > innerW - 40 ? 'end' : 'middle');
|
|
71
69
|
const shape = curve === 'linear' ? curveLinear : curveMonotoneX;
|
|
@@ -83,5 +81,5 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
|
|
|
83
81
|
nearest = i; });
|
|
84
82
|
setHover(nearest);
|
|
85
83
|
}
|
|
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 ?
|
|
84
|
+
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 ? marks.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))] })] })] }) }));
|
|
87
85
|
}
|
package/dist/chart/chart.d.ts
CHANGED
|
@@ -23,3 +23,9 @@ export declare const leftFor: (ticks: number[], fmt: (value: unknown) => string)
|
|
|
23
23
|
export declare const labelStep: (count: number, innerW: number) => number;
|
|
24
24
|
export declare const num: (value: unknown) => number;
|
|
25
25
|
export declare const toDate: (value: unknown) => Date;
|
|
26
|
+
export type TimeTicks = {
|
|
27
|
+
ticks: Date[];
|
|
28
|
+
unit: 'day' | 'month' | 'year';
|
|
29
|
+
};
|
|
30
|
+
export declare function timeTicks(start: Date, end: Date, max: number, days: (count: number) => Date[]): TimeTicks;
|
|
31
|
+
export declare const timeLabel: (unit: TimeTicks["unit"], locale?: string) => (value: unknown) => string;
|
package/dist/chart/chart.js
CHANGED
|
@@ -28,3 +28,24 @@ export const num = (value) => +value || 0;
|
|
|
28
28
|
export const toDate = (value) => value instanceof Date ? value
|
|
29
29
|
: typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value) ? new Date(`${value}T00:00`)
|
|
30
30
|
: new Date(value);
|
|
31
|
+
// At most `max` marks between start and end: the first of every 1, 2, 3, 6, 12… months when the dates
|
|
32
|
+
// span two months or more, else days, as d3 picks them.
|
|
33
|
+
export function timeTicks(start, end, max, days) {
|
|
34
|
+
const from = start.getFullYear() * 12 + start.getMonth();
|
|
35
|
+
const to = end.getFullYear() * 12 + end.getMonth();
|
|
36
|
+
if (to - from < 2) {
|
|
37
|
+
const ticks = days(max);
|
|
38
|
+
const every = Math.ceil(ticks.length / max);
|
|
39
|
+
return { ticks: ticks.filter((_, i) => i % every === 0), unit: 'day' };
|
|
40
|
+
}
|
|
41
|
+
const step = [1, 2, 3, 6, 12, 24, 60, 120, 240].find((k) => Math.floor((to - from) / k) + 1 <= max) ?? 600;
|
|
42
|
+
const ticks = [];
|
|
43
|
+
for (let month = from; month <= to; month++) {
|
|
44
|
+
const tick = new Date(Math.floor(month / 12), month % 12, 1);
|
|
45
|
+
if (month % step === 0 && tick >= start)
|
|
46
|
+
ticks.push(tick);
|
|
47
|
+
}
|
|
48
|
+
return { ticks, unit: step >= 12 ? 'year' : 'month' };
|
|
49
|
+
}
|
|
50
|
+
export const timeLabel = (unit, locale) => (value) => toDate(value).toLocaleDateString(locale, unit === 'day' ? { day: 'numeric', month: 'short' }
|
|
51
|
+
: unit === 'month' ? { month: 'short', year: 'numeric' } : { year: 'numeric' });
|