@tonycodes-ui/mortar 0.29.0 → 0.31.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/CHANGELOG.md +6 -18
- package/catalog.json +859 -361
- package/cli/audit.mjs +1 -1
- package/cli/data.mjs +5 -0
- package/cli/packet.mjs +2 -0
- package/dist/agent-data.json +1 -1
- package/dist/application.d.ts +2 -0
- package/dist/application.js +2 -1
- package/dist/chart-core.d.ts +32 -0
- package/dist/chart-core.js +127 -0
- package/dist/charts.d.ts +5 -0
- package/dist/charts.js +4 -0
- package/dist/sparkline.d.ts +28 -0
- package/dist/sparkline.js +31 -0
- package/dist/xy-chart.d.ts +86 -0
- package/dist/xy-chart.js +391 -0
- package/docs/charts.md +111 -0
- package/package.json +5 -1
- package/src/application.css +6 -1
- package/src/application.tsx +10 -0
- package/src/chart-core.ts +142 -0
- package/src/charts.css +239 -0
- package/src/charts.ts +16 -0
- package/src/sparkline.tsx +124 -0
- package/src/tokens.css +26 -0
- package/src/xy-chart.tsx +886 -0
- package/styles.css +1 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Scales and path builders shared by Mortar's charts. Internal: the charts
|
|
2
|
+
// entry and MetricGroup use it; nothing here is exported from a package entry.
|
|
3
|
+
|
|
4
|
+
export type Extent = readonly [number, number];
|
|
5
|
+
|
|
6
|
+
/** Smallest and largest finite value; [0, 0] when there are none. */
|
|
7
|
+
export function extent(values: readonly number[]): Extent {
|
|
8
|
+
let min = Infinity;
|
|
9
|
+
let max = -Infinity;
|
|
10
|
+
for (const v of values)
|
|
11
|
+
if (Number.isFinite(v)) {
|
|
12
|
+
if (v < min) min = v;
|
|
13
|
+
if (v > max) max = v;
|
|
14
|
+
}
|
|
15
|
+
return min === Infinity ? [0, 0] : [min, max];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Maps a domain onto a range linearly. A flat domain maps to the range's
|
|
20
|
+
* middle. A missing value (NaN) stays missing, so lines keep their gaps.
|
|
21
|
+
*/
|
|
22
|
+
export function linearScale(domain: Extent, range: Extent) {
|
|
23
|
+
const [d0, d1] = domain;
|
|
24
|
+
const [r0, r1] = range;
|
|
25
|
+
const span = d1 - d0;
|
|
26
|
+
return (value: number) =>
|
|
27
|
+
!Number.isFinite(value)
|
|
28
|
+
? Number.NaN
|
|
29
|
+
: span === 0
|
|
30
|
+
? (r0 + r1) / 2
|
|
31
|
+
: r0 + ((value - d0) / span) * (r1 - r0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* "Nice" tick values from the step at or below the minimum to the first step
|
|
36
|
+
* at or above the maximum, so the top gridline is never below the data.
|
|
37
|
+
* Inverted, non-finite and degenerate domains still return a short list.
|
|
38
|
+
*/
|
|
39
|
+
export function niceTicks(domain: Extent, count = 5): number[] {
|
|
40
|
+
let [min, max] = domain;
|
|
41
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
42
|
+
const known = [min, max].find(Number.isFinite);
|
|
43
|
+
return [known ?? 0];
|
|
44
|
+
}
|
|
45
|
+
if (min > max) [min, max] = [max, min];
|
|
46
|
+
if (min === max) return [min];
|
|
47
|
+
const raw = (max - min) / Math.max(1, count);
|
|
48
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
49
|
+
const step =
|
|
50
|
+
[1, 2, 5, 10].map((m) => m * power).find((s) => s >= raw) ?? 10 * power;
|
|
51
|
+
// Round to the step's precision so 0.1 + 0.2 style drift never shows.
|
|
52
|
+
const digits = Math.min(20, Math.max(0, -Math.floor(Math.log10(step))));
|
|
53
|
+
const ticks: number[] = [];
|
|
54
|
+
for (let v = Math.floor(min / step) * step; ticks.length < 50; v += step) {
|
|
55
|
+
ticks.push(Number(v.toFixed(digits)));
|
|
56
|
+
// Stop at the maximum, or when the step is too small to change v at
|
|
57
|
+
// this magnitude (huge values with a tiny span).
|
|
58
|
+
if (v >= max - step * 1e-9 || v + step === v) break;
|
|
59
|
+
}
|
|
60
|
+
return ticks;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface Point {
|
|
64
|
+
x: number;
|
|
65
|
+
y: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** SVG path data for a polyline through the points; breaks at non-finite y. */
|
|
69
|
+
export function linePath(points: readonly Point[]): string {
|
|
70
|
+
let d = "";
|
|
71
|
+
let pen = false;
|
|
72
|
+
for (const { x, y } of points) {
|
|
73
|
+
if (!Number.isFinite(y)) {
|
|
74
|
+
pen = false;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
d += `${pen ? "L" : "M"}${round(x)} ${round(y)}`;
|
|
78
|
+
pen = true;
|
|
79
|
+
}
|
|
80
|
+
return d;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* SVG path data for the area between a line and a baseline y: one closed
|
|
85
|
+
* shape per run of finite values, so a missing value leaves a gap.
|
|
86
|
+
*/
|
|
87
|
+
export function areaPath(points: readonly Point[], baseline: number): string {
|
|
88
|
+
let d = "";
|
|
89
|
+
let run: Point[] = [];
|
|
90
|
+
const close = () => {
|
|
91
|
+
if (run.length >= 2) {
|
|
92
|
+
const first = run[0];
|
|
93
|
+
const last = run[run.length - 1];
|
|
94
|
+
d += `${linePath(run)}L${round(last.x)} ${round(baseline)}L${round(first.x)} ${round(baseline)}Z`;
|
|
95
|
+
}
|
|
96
|
+
run = [];
|
|
97
|
+
};
|
|
98
|
+
for (const p of points) {
|
|
99
|
+
if (Number.isFinite(p.y)) run.push(p);
|
|
100
|
+
else close();
|
|
101
|
+
}
|
|
102
|
+
close();
|
|
103
|
+
return d;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** SVG path data for the band between an upper and a lower edge (a stacked layer). */
|
|
107
|
+
export function bandPath(
|
|
108
|
+
upper: readonly Point[],
|
|
109
|
+
lower: readonly Point[],
|
|
110
|
+
): string {
|
|
111
|
+
// One closed shape per run where both edges are known, so a missing value
|
|
112
|
+
// leaves a gap instead of a fill bridging it.
|
|
113
|
+
let d = "";
|
|
114
|
+
let run: (readonly [Point, Point])[] = [];
|
|
115
|
+
const close = () => {
|
|
116
|
+
if (run.length >= 2) {
|
|
117
|
+
const back = run
|
|
118
|
+
.map(([, l]) => l)
|
|
119
|
+
.reverse()
|
|
120
|
+
.map((p) => `L${round(p.x)} ${round(p.y)}`)
|
|
121
|
+
.join("");
|
|
122
|
+
d += `${linePath(run.map(([u]) => u))}${back}Z`;
|
|
123
|
+
}
|
|
124
|
+
run = [];
|
|
125
|
+
};
|
|
126
|
+
upper.forEach((u, i) => {
|
|
127
|
+
const l = lower[i];
|
|
128
|
+
if (Number.isFinite(u.y) && l && Number.isFinite(l.y)) run.push([u, l]);
|
|
129
|
+
else close();
|
|
130
|
+
});
|
|
131
|
+
close();
|
|
132
|
+
return d;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const round = (n: number) => Math.round(n * 100) / 100;
|
|
136
|
+
|
|
137
|
+
/** A categorical series slot. There are eight and they are never reused: a
|
|
138
|
+
ninth series belongs in "Other" or in its own chart. */
|
|
139
|
+
export type ChartSlot = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
|
|
140
|
+
|
|
141
|
+
/** The CSS colour of a series slot, from the --m-chart-* tokens. */
|
|
142
|
+
export const seriesColour = (slot: ChartSlot) => `var(--m-chart-${slot})`;
|
package/src/charts.css
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/* Charts: Sparkline. Series colours come from the --m-chart-* tokens; text
|
|
2
|
+
always uses the text tokens, never a series colour. Every rule sits in
|
|
3
|
+
@layer components, like the other component families. */
|
|
4
|
+
@layer components {
|
|
5
|
+
/* == Sparkline == */
|
|
6
|
+
.m-sparkline {
|
|
7
|
+
display: inline-block;
|
|
8
|
+
width: 100%;
|
|
9
|
+
min-width: 48px;
|
|
10
|
+
/* Room for the 8px dot and its 2px ring at the top, end and bottom. */
|
|
11
|
+
padding: 6px 6px 6px 0;
|
|
12
|
+
vertical-align: middle;
|
|
13
|
+
}
|
|
14
|
+
.m-sparkline-plot {
|
|
15
|
+
position: relative;
|
|
16
|
+
display: block;
|
|
17
|
+
height: 100%;
|
|
18
|
+
}
|
|
19
|
+
.m-sparkline svg {
|
|
20
|
+
display: block;
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 100%;
|
|
23
|
+
overflow: visible;
|
|
24
|
+
}
|
|
25
|
+
.m-sparkline-line {
|
|
26
|
+
fill: none;
|
|
27
|
+
stroke: var(--m-sparkline-colour);
|
|
28
|
+
stroke-width: 2px;
|
|
29
|
+
stroke-linecap: round;
|
|
30
|
+
stroke-linejoin: round;
|
|
31
|
+
}
|
|
32
|
+
.m-sparkline-area {
|
|
33
|
+
fill: var(--m-sparkline-colour);
|
|
34
|
+
fill-opacity: 0.12;
|
|
35
|
+
}
|
|
36
|
+
.m-sparkline-band {
|
|
37
|
+
fill: var(--m-sunken);
|
|
38
|
+
}
|
|
39
|
+
/* The dot sits in HTML so it stays round when the SVG stretches. A surface
|
|
40
|
+
ring separates it from the line. */
|
|
41
|
+
.m-sparkline-last {
|
|
42
|
+
position: absolute;
|
|
43
|
+
width: 8px;
|
|
44
|
+
height: 8px;
|
|
45
|
+
margin: -4px 0 0 -4px;
|
|
46
|
+
border-radius: var(--m-radius-full);
|
|
47
|
+
background: var(--m-sparkline-colour);
|
|
48
|
+
box-shadow: 0 0 0 2px var(--m-surface);
|
|
49
|
+
}
|
|
50
|
+
@media (forced-colors: active) {
|
|
51
|
+
.m-sparkline-line {
|
|
52
|
+
stroke: CanvasText;
|
|
53
|
+
}
|
|
54
|
+
.m-sparkline-last {
|
|
55
|
+
background: CanvasText;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* == Chart frame: LineChart, AreaChart and BarChart == */
|
|
60
|
+
.m-chart {
|
|
61
|
+
display: grid;
|
|
62
|
+
gap: 12px;
|
|
63
|
+
min-width: 0;
|
|
64
|
+
margin: 0;
|
|
65
|
+
}
|
|
66
|
+
.m-chart-head {
|
|
67
|
+
display: grid;
|
|
68
|
+
gap: 4px;
|
|
69
|
+
}
|
|
70
|
+
.m-chart-title {
|
|
71
|
+
color: var(--m-ink);
|
|
72
|
+
font-size: 15px;
|
|
73
|
+
font-weight: var(--m-weight-semibold);
|
|
74
|
+
}
|
|
75
|
+
.m-chart-description {
|
|
76
|
+
color: var(--m-muted);
|
|
77
|
+
font-size: 13px;
|
|
78
|
+
}
|
|
79
|
+
.m-chart-legend {
|
|
80
|
+
display: flex;
|
|
81
|
+
flex-wrap: wrap;
|
|
82
|
+
gap: 4px 16px;
|
|
83
|
+
margin: 0;
|
|
84
|
+
padding: 0;
|
|
85
|
+
list-style: none;
|
|
86
|
+
color: var(--m-secondary);
|
|
87
|
+
font-size: 12px;
|
|
88
|
+
}
|
|
89
|
+
.m-chart-legend li {
|
|
90
|
+
display: inline-flex;
|
|
91
|
+
align-items: center;
|
|
92
|
+
gap: 6px;
|
|
93
|
+
}
|
|
94
|
+
.m-chart-legend li::before,
|
|
95
|
+
.m-chart-tooltip i {
|
|
96
|
+
content: "";
|
|
97
|
+
width: 10px;
|
|
98
|
+
height: 10px;
|
|
99
|
+
border-radius: var(--m-radius-xs);
|
|
100
|
+
background: var(--m-series);
|
|
101
|
+
}
|
|
102
|
+
.m-chart-body {
|
|
103
|
+
position: relative;
|
|
104
|
+
min-width: 0;
|
|
105
|
+
}
|
|
106
|
+
.m-chart-plot {
|
|
107
|
+
position: relative;
|
|
108
|
+
height: 100%;
|
|
109
|
+
border-radius: var(--m-radius);
|
|
110
|
+
outline-offset: 2px;
|
|
111
|
+
}
|
|
112
|
+
.m-chart-plot:focus-visible {
|
|
113
|
+
outline: 2px solid var(--m-focus);
|
|
114
|
+
}
|
|
115
|
+
.m-chart-plot svg {
|
|
116
|
+
display: block;
|
|
117
|
+
overflow: visible;
|
|
118
|
+
}
|
|
119
|
+
.m-chart-axis line {
|
|
120
|
+
stroke: var(--m-line);
|
|
121
|
+
stroke-width: 1px;
|
|
122
|
+
}
|
|
123
|
+
.m-chart-axis text,
|
|
124
|
+
.m-chart-end-label {
|
|
125
|
+
fill: var(--m-muted);
|
|
126
|
+
font-size: 11px;
|
|
127
|
+
font-variant-numeric: tabular-nums;
|
|
128
|
+
}
|
|
129
|
+
.m-chart-end-label {
|
|
130
|
+
fill: var(--m-secondary);
|
|
131
|
+
}
|
|
132
|
+
.m-chart-zero {
|
|
133
|
+
stroke: var(--m-line-strong);
|
|
134
|
+
stroke-width: 1px;
|
|
135
|
+
}
|
|
136
|
+
.m-chart-line {
|
|
137
|
+
fill: none;
|
|
138
|
+
stroke: var(--m-series);
|
|
139
|
+
stroke-width: 2px;
|
|
140
|
+
stroke-linecap: round;
|
|
141
|
+
stroke-linejoin: round;
|
|
142
|
+
}
|
|
143
|
+
.m-chart-area {
|
|
144
|
+
fill: var(--m-series);
|
|
145
|
+
fill-opacity: 0.18;
|
|
146
|
+
}
|
|
147
|
+
/* A 2px surface ring keeps markers readable where lines cross. */
|
|
148
|
+
.m-chart-marker {
|
|
149
|
+
fill: var(--m-series);
|
|
150
|
+
stroke: var(--m-surface);
|
|
151
|
+
stroke-width: 2px;
|
|
152
|
+
}
|
|
153
|
+
/* A 2px surface gap between stacked segments. */
|
|
154
|
+
.m-chart-bar {
|
|
155
|
+
fill: var(--m-series);
|
|
156
|
+
stroke: var(--m-surface);
|
|
157
|
+
stroke-width: 1px;
|
|
158
|
+
}
|
|
159
|
+
.m-chart-crosshair {
|
|
160
|
+
stroke: var(--m-line-strong);
|
|
161
|
+
stroke-width: 1px;
|
|
162
|
+
}
|
|
163
|
+
.m-chart-band {
|
|
164
|
+
fill: var(--m-sunken);
|
|
165
|
+
}
|
|
166
|
+
.m-chart-tooltip {
|
|
167
|
+
position: absolute;
|
|
168
|
+
top: 0;
|
|
169
|
+
z-index: 2;
|
|
170
|
+
display: grid;
|
|
171
|
+
gap: 4px;
|
|
172
|
+
/* Fits narrow charts: never wider than the plot minus the offset. */
|
|
173
|
+
min-width: min(140px, 60%);
|
|
174
|
+
max-width: calc(100% - 24px);
|
|
175
|
+
padding: 8px 10px;
|
|
176
|
+
border: 1px solid var(--m-line);
|
|
177
|
+
border-radius: var(--m-radius);
|
|
178
|
+
background: var(--m-surface);
|
|
179
|
+
box-shadow: var(--m-float);
|
|
180
|
+
color: var(--m-secondary);
|
|
181
|
+
font-size: 12px;
|
|
182
|
+
pointer-events: none;
|
|
183
|
+
transform: translateX(12px);
|
|
184
|
+
}
|
|
185
|
+
.m-chart-tooltip[data-side="start"] {
|
|
186
|
+
transform: translateX(calc(-100% - 12px));
|
|
187
|
+
}
|
|
188
|
+
.m-chart-tooltip strong {
|
|
189
|
+
color: var(--m-ink);
|
|
190
|
+
}
|
|
191
|
+
.m-chart-tooltip span {
|
|
192
|
+
display: flex;
|
|
193
|
+
align-items: center;
|
|
194
|
+
gap: 6px;
|
|
195
|
+
}
|
|
196
|
+
.m-chart-tooltip b {
|
|
197
|
+
margin-inline-start: auto;
|
|
198
|
+
color: var(--m-ink);
|
|
199
|
+
font-weight: var(--m-weight-semibold);
|
|
200
|
+
font-variant-numeric: tabular-nums;
|
|
201
|
+
}
|
|
202
|
+
.m-chart-state {
|
|
203
|
+
margin: 0;
|
|
204
|
+
color: var(--m-muted);
|
|
205
|
+
font-size: 13px;
|
|
206
|
+
}
|
|
207
|
+
.m-chart-actions {
|
|
208
|
+
display: flex;
|
|
209
|
+
justify-content: flex-end;
|
|
210
|
+
}
|
|
211
|
+
.m-chart-axis-label {
|
|
212
|
+
fill: var(--m-secondary);
|
|
213
|
+
font-size: 12px;
|
|
214
|
+
font-weight: var(--m-weight-semibold);
|
|
215
|
+
}
|
|
216
|
+
/* High-contrast mode would paint every series the same system colour, so
|
|
217
|
+
series marks and their legend swatches keep their colours (identity is
|
|
218
|
+
data here); the axes, labels and crosshair follow the system colours. */
|
|
219
|
+
@media (forced-colors: active) {
|
|
220
|
+
.m-chart-line,
|
|
221
|
+
.m-chart-area,
|
|
222
|
+
.m-chart-bar,
|
|
223
|
+
.m-chart-marker,
|
|
224
|
+
.m-chart-legend li::before,
|
|
225
|
+
.m-chart-tooltip i {
|
|
226
|
+
forced-color-adjust: none;
|
|
227
|
+
}
|
|
228
|
+
.m-chart-axis line,
|
|
229
|
+
.m-chart-crosshair,
|
|
230
|
+
.m-chart-zero {
|
|
231
|
+
stroke: CanvasText;
|
|
232
|
+
}
|
|
233
|
+
.m-chart-axis text,
|
|
234
|
+
.m-chart-end-label,
|
|
235
|
+
.m-chart-axis-label {
|
|
236
|
+
fill: CanvasText;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
package/src/charts.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* @tonycodes-ui/mortar/charts: charts drawn as SVG with Mortar's tokens, with
|
|
2
|
+
no runtime dependencies. See docs/charts.md. */
|
|
3
|
+
|
|
4
|
+
export { Sparkline } from "./sparkline.js";
|
|
5
|
+
export { LineChart, AreaChart, BarChart } from "./xy-chart.js";
|
|
6
|
+
export type { SparklineProps } from "./sparkline.js";
|
|
7
|
+
export type {
|
|
8
|
+
ChartBaseProps,
|
|
9
|
+
ChartSeries,
|
|
10
|
+
ChartX,
|
|
11
|
+
ChartY,
|
|
12
|
+
LineChartProps,
|
|
13
|
+
AreaChartProps,
|
|
14
|
+
BarChartProps,
|
|
15
|
+
} from "./xy-chart.js";
|
|
16
|
+
export type { ChartSlot } from "./chart-core.js";
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
import { componentRoot, cx } from "./internal.js";
|
|
3
|
+
import {
|
|
4
|
+
areaPath,
|
|
5
|
+
extent,
|
|
6
|
+
linearScale,
|
|
7
|
+
linePath,
|
|
8
|
+
seriesColour,
|
|
9
|
+
type ChartSlot,
|
|
10
|
+
} from "./chart-core.js";
|
|
11
|
+
|
|
12
|
+
export interface SparklineProps {
|
|
13
|
+
/** The values in order, oldest first. Missing values (NaN) break the line. */
|
|
14
|
+
values: readonly number[];
|
|
15
|
+
/** What the values measure, e.g. "Revenue, last 12 months". Read as the start of its summary. */
|
|
16
|
+
label: string;
|
|
17
|
+
/** Formats values in the spoken summary. Defaults to the locale's number format. */
|
|
18
|
+
format?: (value: number) => string;
|
|
19
|
+
/** Series colour slot, 1–8. Default 1. */
|
|
20
|
+
slot?: ChartSlot;
|
|
21
|
+
/** Height in pixels; the width follows the container. Default 32. */
|
|
22
|
+
height?: number;
|
|
23
|
+
/** Mark the latest value with a dot. Default true. */
|
|
24
|
+
showLast?: boolean;
|
|
25
|
+
/** Fill under the line. */
|
|
26
|
+
area?: boolean;
|
|
27
|
+
/** Shade a reference range, such as a target or normal band. */
|
|
28
|
+
band?: { min: number; max: number };
|
|
29
|
+
id?: string;
|
|
30
|
+
className?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const number = new Intl.NumberFormat();
|
|
34
|
+
|
|
35
|
+
/** The spoken summary: label, count, range and latest value. */
|
|
36
|
+
export function sparklineSummary(
|
|
37
|
+
values: readonly number[],
|
|
38
|
+
label: string,
|
|
39
|
+
format: (value: number) => string = (v) => number.format(v),
|
|
40
|
+
) {
|
|
41
|
+
const finite = values.filter(Number.isFinite);
|
|
42
|
+
if (!finite.length) return `${label}: no data`;
|
|
43
|
+
const [min, max] = extent(finite);
|
|
44
|
+
const last = finite[finite.length - 1];
|
|
45
|
+
return finite.length === 1
|
|
46
|
+
? `${label}: ${format(last)}`
|
|
47
|
+
: `${label}: ${finite.length} values from ${format(min)} to ${format(max)}, latest ${format(last)}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A small, axis-free trend line for a stat tile, table cell or list row. */
|
|
51
|
+
export function Sparkline({
|
|
52
|
+
values,
|
|
53
|
+
label,
|
|
54
|
+
format,
|
|
55
|
+
slot = 1,
|
|
56
|
+
height = 32,
|
|
57
|
+
showLast = true,
|
|
58
|
+
area = false,
|
|
59
|
+
band,
|
|
60
|
+
id,
|
|
61
|
+
className,
|
|
62
|
+
}: SparklineProps) {
|
|
63
|
+
const [min, max] = extent(
|
|
64
|
+
band ? [...values, band.min, band.max] : [...values],
|
|
65
|
+
);
|
|
66
|
+
// A single value maps through a flat domain to the middle.
|
|
67
|
+
const x = linearScale([0, values.length - 1], [0, 100]);
|
|
68
|
+
const y = linearScale([min, max], [100, 0]);
|
|
69
|
+
const points = values.map((v, i) => ({ x: x(i), y: y(v) }));
|
|
70
|
+
let lastIndex = values.length - 1;
|
|
71
|
+
while (lastIndex >= 0 && !Number.isFinite(values[lastIndex])) lastIndex--;
|
|
72
|
+
const last = lastIndex >= 0 ? points[lastIndex] : undefined;
|
|
73
|
+
return (
|
|
74
|
+
<span
|
|
75
|
+
id={id}
|
|
76
|
+
{...componentRoot("Sparkline")}
|
|
77
|
+
className={cx("m-sparkline", className)}
|
|
78
|
+
role="img"
|
|
79
|
+
aria-label={sparklineSummary(values, label, format)}
|
|
80
|
+
style={
|
|
81
|
+
{
|
|
82
|
+
height,
|
|
83
|
+
"--m-sparkline-colour": seriesColour(slot),
|
|
84
|
+
} as CSSProperties
|
|
85
|
+
}
|
|
86
|
+
>
|
|
87
|
+
{/* The plot sits inside padding sized for the dot and its ring, so
|
|
88
|
+
nothing reaches past the sparkline's own box. */}
|
|
89
|
+
<span className="m-sparkline-plot">
|
|
90
|
+
<svg
|
|
91
|
+
viewBox="0 0 100 100"
|
|
92
|
+
preserveAspectRatio="none"
|
|
93
|
+
aria-hidden="true"
|
|
94
|
+
focusable="false"
|
|
95
|
+
>
|
|
96
|
+
{band && (
|
|
97
|
+
<rect
|
|
98
|
+
className="m-sparkline-band"
|
|
99
|
+
x="0"
|
|
100
|
+
width="100"
|
|
101
|
+
y={Math.min(y(band.min), y(band.max))}
|
|
102
|
+
height={Math.abs(y(band.min) - y(band.max))}
|
|
103
|
+
/>
|
|
104
|
+
)}
|
|
105
|
+
{area && (
|
|
106
|
+
<path className="m-sparkline-area" d={areaPath(points, 100)} />
|
|
107
|
+
)}
|
|
108
|
+
<path
|
|
109
|
+
className="m-sparkline-line"
|
|
110
|
+
d={linePath(points)}
|
|
111
|
+
vectorEffect="non-scaling-stroke"
|
|
112
|
+
/>
|
|
113
|
+
</svg>
|
|
114
|
+
{showLast && last && (
|
|
115
|
+
<span
|
|
116
|
+
className="m-sparkline-last"
|
|
117
|
+
style={{ left: `${last.x}%`, top: `${last.y}%` }}
|
|
118
|
+
aria-hidden="true"
|
|
119
|
+
/>
|
|
120
|
+
)}
|
|
121
|
+
</span>
|
|
122
|
+
</span>
|
|
123
|
+
);
|
|
124
|
+
}
|
package/src/tokens.css
CHANGED
|
@@ -90,6 +90,16 @@
|
|
|
90
90
|
--m-warning: #945700;
|
|
91
91
|
--m-danger: #c70014;
|
|
92
92
|
--m-info: #0060c7;
|
|
93
|
+
/* Chart series, in fixed order; validated for colour-blind separation on
|
|
94
|
+
the light and dark surfaces (docs/charts.md). Not for status. */
|
|
95
|
+
--m-chart-1: #2a78d6;
|
|
96
|
+
--m-chart-2: #eb6834;
|
|
97
|
+
--m-chart-3: #1baf7a;
|
|
98
|
+
--m-chart-4: #eda100;
|
|
99
|
+
--m-chart-5: #e87ba4;
|
|
100
|
+
--m-chart-6: #008300;
|
|
101
|
+
--m-chart-7: #6250d6;
|
|
102
|
+
--m-chart-8: #e34948;
|
|
93
103
|
--m-on-feedback: #ffffff;
|
|
94
104
|
--m-code-bg: #000000;
|
|
95
105
|
--m-code-line: rgb(255 255 255 / 0.12);
|
|
@@ -176,6 +186,14 @@
|
|
|
176
186
|
--m-warning: #ffd60a;
|
|
177
187
|
--m-danger: #ff6961;
|
|
178
188
|
--m-info: #5aa9ff;
|
|
189
|
+
--m-chart-1: #3987e5;
|
|
190
|
+
--m-chart-2: #d95926;
|
|
191
|
+
--m-chart-3: #199e70;
|
|
192
|
+
--m-chart-4: #c98500;
|
|
193
|
+
--m-chart-5: #d55181;
|
|
194
|
+
--m-chart-6: #0ca30c;
|
|
195
|
+
--m-chart-7: #9085e9;
|
|
196
|
+
--m-chart-8: #e66767;
|
|
179
197
|
--m-on-feedback: #000000;
|
|
180
198
|
--m-code-bg: #0e0e10;
|
|
181
199
|
--m-code-line: rgb(255 255 255 / 0.1);
|
|
@@ -261,6 +279,14 @@
|
|
|
261
279
|
--m-warning: #ffd60a;
|
|
262
280
|
--m-danger: #ff6961;
|
|
263
281
|
--m-info: #5aa9ff;
|
|
282
|
+
--m-chart-1: #3987e5;
|
|
283
|
+
--m-chart-2: #d95926;
|
|
284
|
+
--m-chart-3: #199e70;
|
|
285
|
+
--m-chart-4: #c98500;
|
|
286
|
+
--m-chart-5: #d55181;
|
|
287
|
+
--m-chart-6: #0ca30c;
|
|
288
|
+
--m-chart-7: #9085e9;
|
|
289
|
+
--m-chart-8: #e66767;
|
|
264
290
|
--m-on-feedback: #000000;
|
|
265
291
|
--m-code-bg: #0e0e10;
|
|
266
292
|
--m-code-line: rgb(255 255 255 / 0.1);
|