@bigpixelrocket/uiok 0.1.4 → 0.2.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 +88 -1
- package/README.md +1 -1
- package/dist/activity/ActivityBar.d.ts +21 -8
- package/dist/activity/ActivityBar.js +15 -5
- package/dist/activity/activity.css +53 -6
- package/dist/button/Button.d.ts +10 -6
- package/dist/button/Button.js +6 -1
- package/dist/button/button.css +201 -33
- package/dist/charts/Line.logic.d.ts +6 -0
- package/dist/charts/Line.logic.js +9 -0
- package/dist/charts/LineChart.js +11 -4
- package/dist/charts/Pie.logic.d.ts +89 -1
- package/dist/charts/Pie.logic.js +272 -2
- package/dist/charts/PieChart.d.ts +2 -1
- package/dist/charts/PieChart.js +12 -5
- package/dist/charts/PieFill.d.ts +15 -0
- package/dist/charts/PieFill.js +33 -0
- package/dist/charts/ResultChart.d.ts +2 -2
- package/dist/charts/ResultChart.js +173 -47
- package/dist/charts/ResultChart.logic.d.ts +60 -4
- package/dist/charts/ResultChart.logic.js +105 -6
- package/dist/charts/Scatter.logic.d.ts +18 -4
- package/dist/charts/Scatter.logic.js +98 -22
- package/dist/charts/ScatterChart.js +19 -5
- package/dist/charts/charts.css +233 -116
- package/dist/charts/seriesColour.d.ts +2 -0
- package/dist/charts/seriesColour.js +1 -1
- package/dist/datetime/DateTimePicker.d.ts +12 -2
- package/dist/datetime/DateTimePicker.js +61 -16
- package/dist/datetime/DateTimePicker.logic.d.ts +29 -0
- package/dist/datetime/DateTimePicker.logic.js +25 -0
- package/dist/datetime/datetime.css +89 -49
- package/dist/dialog/ConfirmDialog.d.ts +69 -20
- package/dist/dialog/ConfirmDialog.js +39 -12
- package/dist/dialog/DialogLayer.d.ts +25 -0
- package/dist/dialog/DialogLayer.js +24 -0
- package/dist/dialog/DialogLegend.d.ts +2 -1
- package/dist/dialog/DialogLegend.js +10 -5
- package/dist/dialog/DialogLegend.logic.d.ts +15 -3
- package/dist/dialog/DialogLegend.logic.js +8 -4
- package/dist/dialog/DialogLists.d.ts +157 -0
- package/dist/dialog/DialogLists.js +137 -0
- package/dist/dialog/ModalDialog.d.ts +20 -15
- package/dist/dialog/ModalDialog.js +22 -7
- package/dist/dialog/dialog.css +570 -127
- package/dist/dialog/index.d.ts +7 -1
- package/dist/dialog/index.js +7 -1
- package/dist/field/Check.d.ts +15 -0
- package/dist/field/Check.js +23 -0
- package/dist/field/field.css +812 -0
- package/dist/field/index.d.ts +10 -0
- package/dist/field/index.js +10 -0
- package/dist/field/labelHelp.d.ts +33 -0
- package/dist/field/labelHelp.js +22 -0
- package/dist/filter/FilterIcon.js +2 -1
- package/dist/filter/filter.css +72 -20
- package/dist/grid/DataGrid.d.ts +8 -1
- package/dist/grid/DataGrid.js +115 -40
- package/dist/grid/DataGrid.logic.d.ts +15 -3
- package/dist/grid/DataGrid.logic.js +18 -5
- package/dist/grid/grid.css +481 -56
- package/dist/icon/Spinner.d.ts +16 -0
- package/dist/icon/Spinner.js +13 -0
- package/dist/icon/index.d.ts +5 -0
- package/dist/icon/index.js +5 -0
- package/dist/icon/sizes.d.ts +19 -0
- package/dist/icon/sizes.js +19 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/keychord/KeyChord.d.ts +12 -1
- package/dist/keychord/KeyChord.js +2 -2
- package/dist/keychord/index.d.ts +1 -1
- package/dist/keychord/keychord.css +87 -9
- package/dist/labels/dialog.d.ts +12 -1
- package/dist/labels/dialog.js +3 -1
- package/dist/labels/selectAll.d.ts +4 -4
- package/dist/labels/selectAll.js +1 -1
- package/dist/legend/KeyLegendStrip.js +1 -1
- package/dist/legend/legend.css +82 -57
- package/dist/listing/Listing.d.ts +17 -10
- package/dist/listing/Listing.js +33 -26
- package/dist/listing/listing.css +1665 -237
- package/dist/menu/menu.css +227 -67
- package/dist/menu/useBellBeat.js +2 -2
- package/dist/namecut/NameCut.d.ts +3 -1
- package/dist/namecut/NameCut.js +152 -5
- package/dist/namecut/NameCut.logic.d.ts +5 -3
- package/dist/namecut/NameCut.logic.js +20 -5
- package/dist/namecut/namecut.css +27 -7
- package/dist/pager/Pager.d.ts +12 -4
- package/dist/pager/Pager.js +21 -7
- package/dist/pager/pager.css +41 -30
- package/dist/palette/Palette.js +2 -1
- package/dist/palette/palette.css +121 -60
- package/dist/pane/PaneActions.d.ts +56 -14
- package/dist/pane/PaneActions.js +64 -16
- package/dist/pane/PaneFrame.d.ts +37 -5
- package/dist/pane/PaneFrame.js +246 -11
- package/dist/pane/index.d.ts +4 -2
- package/dist/pane/index.js +4 -2
- package/dist/pane/toolWords.d.ts +14 -0
- package/dist/pane/toolWords.js +26 -0
- package/dist/pane/useToolbarFit.d.ts +25 -0
- package/dist/pane/useToolbarFit.js +95 -0
- package/dist/run/RunHeader.d.ts +5 -3
- package/dist/run/RunHeader.js +11 -8
- package/dist/run/run.css +64 -79
- package/dist/segmented/SegmentedChoice.d.ts +18 -2
- package/dist/segmented/SegmentedChoice.js +18 -7
- package/dist/segmented/segmented.css +62 -22
- package/dist/selectall/SelectAllBar.d.ts +4 -3
- package/dist/selectall/SelectAllBar.js +4 -3
- package/dist/selectall/selectall.css +17 -12
- package/dist/shell/shell.css +1073 -0
- package/dist/styles.css +620 -22
- package/dist/surface/surface.css +154 -0
- package/dist/tabs/PageTabs.d.ts +10 -5
- package/dist/tabs/PageTabs.js +126 -37
- package/dist/tabs/tabs.css +131 -51
- package/dist/terminal/Terminal.js +3 -1
- package/dist/terminal/Terminal.logic.d.ts +5 -4
- package/dist/terminal/Terminal.logic.js +9 -4
- package/dist/terminal/TerminalMenu.d.ts +4 -1
- package/dist/terminal/TerminalMenu.js +12 -5
- package/dist/terminal/ghostty/contrast.logic.d.ts +45 -0
- package/dist/terminal/ghostty/contrast.logic.js +130 -0
- package/dist/terminal/ghostty/core.d.ts +6 -0
- package/dist/terminal/ghostty/renderer.d.ts +9 -2
- package/dist/terminal/ghostty/renderer.js +91 -14
- package/dist/terminal/ghostty/surface.d.ts +8 -0
- package/dist/terminal/ghostty/surface.js +27 -1
- package/dist/terminal/terminal.css +87 -61
- package/dist/text/text.css +16 -7
- package/dist/themes/catalog.js +4 -3
- package/dist/themes/themes.generated.css +1307 -264
- package/dist/themes/tokens.d.ts +69 -14
- package/dist/themes/tokens.js +920 -97
- package/dist/tokens.css +239 -0
- package/dist/tooltip/Tooltip.d.ts +6 -0
- package/dist/tooltip/Tooltip.js +48 -6
- package/dist/tooltip/Tooltip.logic.d.ts +89 -12
- package/dist/tooltip/Tooltip.logic.js +142 -20
- package/dist/tooltip/index.d.ts +1 -1
- package/dist/tooltip/index.js +1 -1
- package/dist/tooltip/tooltip.css +39 -9
- package/package.json +3 -3
package/dist/charts/Pie.logic.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The geometry of the pie chart (`PieChart.tsx`) and of `ResultChart`'s pie:
|
|
3
|
-
* each slice's share and its arc as an SVG path
|
|
3
|
+
* each slice's share and its arc as an SVG path, and the fill each slice
|
|
4
|
+
* takes so neighbours and legend swatches never match.
|
|
4
5
|
*/
|
|
6
|
+
import { SERIES_COLOURS } from './seriesColour.js';
|
|
5
7
|
/** A point on the circle of radius `r` at `turn` turns from twelve o'clock, clockwise. */
|
|
6
8
|
const pointAt = (r, turn) => {
|
|
7
9
|
const angle = turn * 2 * Math.PI;
|
|
@@ -23,6 +25,274 @@ export function pieArcs(slices, r) {
|
|
|
23
25
|
const path = share >= 1
|
|
24
26
|
? `M 0 ${-r} A ${r} ${r} 0 1 1 0 ${r} A ${r} ${r} 0 1 1 0 ${-r} Z`
|
|
25
27
|
: `M 0 0 L ${pointAt(r, start)} A ${r} ${r} 0 ${share > 0.5 ? 1 : 0} 1 ${pointAt(r, turn)} Z`;
|
|
26
|
-
return { ...slice, share, path };
|
|
28
|
+
return { ...slice, share, path, middle: start + share / 2 };
|
|
27
29
|
});
|
|
28
30
|
}
|
|
31
|
+
/** How far out a tag stands from the rim, on its first ring and on its second. */
|
|
32
|
+
export const PIE_TAG_REACH = 8;
|
|
33
|
+
const SECOND_RING = 14;
|
|
34
|
+
/**
|
|
35
|
+
* The box a tag's number takes, for 11px tabular figures (about 6.6px a
|
|
36
|
+
* figure and 11px tall); `pieTags` keeps these boxes `PIE_TAG_CLEAR`
|
|
37
|
+
* pixels apart side by side and `PIE_TAG_CLEAR_ABOVE` one above the other.
|
|
38
|
+
*/
|
|
39
|
+
const FIGURE_WIDTH = 6.6;
|
|
40
|
+
const FIGURE_HEIGHT = 11;
|
|
41
|
+
/**
|
|
42
|
+
* The least room between two tags' numbers side by side: more than a
|
|
43
|
+
* figure's width, so "12" beside "1" never reads as "121".
|
|
44
|
+
*/
|
|
45
|
+
export const PIE_TAG_CLEAR = 8;
|
|
46
|
+
/** The least room between two tags' numbers one above the other, where lines keep them apart. */
|
|
47
|
+
export const PIE_TAG_CLEAR_ABOVE = 3;
|
|
48
|
+
/** The least room between a number and another tag's leader. */
|
|
49
|
+
const LEADER_CLEAR = 2;
|
|
50
|
+
/** The box `tag`'s number takes as drawn. */
|
|
51
|
+
export function pieTagBox(tag) {
|
|
52
|
+
const width = tag.text.length * FIGURE_WIDTH;
|
|
53
|
+
const left = tag.anchor === 'start'
|
|
54
|
+
? tag.textX
|
|
55
|
+
: tag.anchor === 'end'
|
|
56
|
+
? tag.textX - width
|
|
57
|
+
: tag.textX - width / 2;
|
|
58
|
+
return {
|
|
59
|
+
left,
|
|
60
|
+
right: left + width,
|
|
61
|
+
top: tag.textY - FIGURE_HEIGHT / 2,
|
|
62
|
+
bottom: tag.textY + FIGURE_HEIGHT / 2
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const boxesMeet = (a, b) => a.left < b.right + PIE_TAG_CLEAR &&
|
|
66
|
+
b.left < a.right + PIE_TAG_CLEAR &&
|
|
67
|
+
a.top < b.bottom + PIE_TAG_CLEAR_ABOVE &&
|
|
68
|
+
b.top < a.bottom + PIE_TAG_CLEAR_ABOVE;
|
|
69
|
+
/** Whether the segment from (x1, y1) to (x2, y2) passes through `box` grown by `clear`. */
|
|
70
|
+
const segmentMeetsBox = ([x1, y1, x2, y2], box, clear) => {
|
|
71
|
+
// Clip the segment's parameter range to the box, one axis at a time.
|
|
72
|
+
let low = 0;
|
|
73
|
+
let high = 1;
|
|
74
|
+
const clip = (from, delta, min, max) => {
|
|
75
|
+
if (delta === 0)
|
|
76
|
+
return from > min && from < max;
|
|
77
|
+
const a = (min - from) / delta;
|
|
78
|
+
const b = (max - from) / delta;
|
|
79
|
+
low = Math.max(low, Math.min(a, b));
|
|
80
|
+
high = Math.min(high, Math.max(a, b));
|
|
81
|
+
return low < high;
|
|
82
|
+
};
|
|
83
|
+
return (clip(x1, x2 - x1, box.left - clear, box.right + clear) &&
|
|
84
|
+
clip(y1, y2 - y1, box.top - clear, box.bottom + clear));
|
|
85
|
+
};
|
|
86
|
+
/** Whether two segments cross. */
|
|
87
|
+
const segmentsCross = (a, b) => {
|
|
88
|
+
const side = (s, x, y) => Math.sign((s[2] - s[0]) * (y - s[1]) - (s[3] - s[1]) * (x - s[0]));
|
|
89
|
+
return (side(a, b[0], b[1]) * side(a, b[2], b[3]) < 0 && side(b, a[0], a[1]) * side(b, a[2], a[3]) < 0);
|
|
90
|
+
};
|
|
91
|
+
/** How far one step of spreading moves a crowded tag, in turns (a quarter of a degree). */
|
|
92
|
+
const SPREAD_STEP = 1 / 1440;
|
|
93
|
+
const SPREAD_ROUNDS = 2000;
|
|
94
|
+
/**
|
|
95
|
+
* Where each slice's number goes when colours alone cannot tell slices apart
|
|
96
|
+
* (more slices than series colours): just outside the rim at the slice's
|
|
97
|
+
* middle, on a short leader. Each number's box keeps clear of every other
|
|
98
|
+
* number (`PIE_TAG_CLEAR` side by side) and leader, round the circle too, so the last tag and
|
|
99
|
+
* the first keep apart. Where tags crowd, they spread apart along their ring,
|
|
100
|
+
* keeping their order, so leaders never cross; where one ring cannot hold a
|
|
101
|
+
* run of thin slices, every other tag of the run steps out to a second ring.
|
|
102
|
+
* A leader is straight and never cuts into the pie, which bounds how far a
|
|
103
|
+
* tag may spread from its slice. A tag that still finds no clear place is
|
|
104
|
+
* `null` and its slice is left to the legend. `texts` holds each arc's
|
|
105
|
+
* number. A chart draws the pie `pieTagRoom` smaller to make the room.
|
|
106
|
+
*/
|
|
107
|
+
export function pieTags(arcs, r, texts = arcs.map((_arc, at) => String(at + 1))) {
|
|
108
|
+
const count = arcs.length;
|
|
109
|
+
if (count === 0)
|
|
110
|
+
return [];
|
|
111
|
+
const radii = [r + PIE_TAG_REACH, r + PIE_TAG_REACH + SECOND_RING];
|
|
112
|
+
// How far a tag may stand from its slice's middle on each ring: a
|
|
113
|
+
// straight leader from the rim leaves it outward while the end stays at
|
|
114
|
+
// least as far out along the slice's own direction as the rim.
|
|
115
|
+
const reaches = radii.map((radius) => Math.acos(r / radius) / (2 * Math.PI));
|
|
116
|
+
const tagAt = (at, ring, turn) => {
|
|
117
|
+
const radius = radii[ring] ?? r;
|
|
118
|
+
const angle = turn * 2 * Math.PI;
|
|
119
|
+
const middle = (arcs[at]?.middle ?? 0) * 2 * Math.PI;
|
|
120
|
+
const x = radius * Math.sin(angle);
|
|
121
|
+
const y = -radius * Math.cos(angle);
|
|
122
|
+
const anchor = Math.abs(Math.sin(angle)) < 0.3 ? 'middle' : x > 0 ? 'start' : 'end';
|
|
123
|
+
return {
|
|
124
|
+
fromX: r * Math.sin(middle),
|
|
125
|
+
fromY: -r * Math.cos(middle),
|
|
126
|
+
x,
|
|
127
|
+
y,
|
|
128
|
+
anchor,
|
|
129
|
+
// A tag above or below the pie stands clear of its leader's end; one
|
|
130
|
+
// to a side, a little way along from it.
|
|
131
|
+
textX: x + (anchor === 'start' ? 2 : anchor === 'end' ? -2 : 0),
|
|
132
|
+
textY: y + (anchor === 'middle' ? (y < 0 ? -6 : 6) : 0),
|
|
133
|
+
text: texts[at] ?? String(at + 1)
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
const leaderOf = (tag) => [tag.fromX, tag.fromY, tag.x, tag.y];
|
|
137
|
+
const meet = (a, b) => {
|
|
138
|
+
const boxA = pieTagBox(a);
|
|
139
|
+
const boxB = pieTagBox(b);
|
|
140
|
+
return (boxesMeet(boxA, boxB) ||
|
|
141
|
+
segmentMeetsBox(leaderOf(a), boxB, LEADER_CLEAR) ||
|
|
142
|
+
segmentMeetsBox(leaderOf(b), boxA, LEADER_CLEAR) ||
|
|
143
|
+
segmentsCross(leaderOf(a), leaderOf(b)));
|
|
144
|
+
};
|
|
145
|
+
// Sweep from just after the widest gap between slice middles, so a run
|
|
146
|
+
// of thin slices is never split between the sweep's end and its start.
|
|
147
|
+
let first = 0;
|
|
148
|
+
let widest = -1;
|
|
149
|
+
arcs.forEach((arc, at) => {
|
|
150
|
+
const before = arcs[(at + count - 1) % count]?.middle ?? 0;
|
|
151
|
+
const gap = (arc.middle - before + 1) % 1 || (count === 1 ? 1 : 0);
|
|
152
|
+
if (gap > widest) {
|
|
153
|
+
widest = gap;
|
|
154
|
+
first = at;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
const order = Array.from({ length: count }, (_, step) => (first + step) % count);
|
|
158
|
+
// Each tag's slice middle, unwrapped so it rises along the sweep.
|
|
159
|
+
const middles = order.map((at) => arcs[at]?.middle ?? 0);
|
|
160
|
+
middles.forEach((turn, step) => {
|
|
161
|
+
if (step > 0 && turn < (middles[step - 1] ?? 0))
|
|
162
|
+
middles[step] = turn + 1;
|
|
163
|
+
});
|
|
164
|
+
/**
|
|
165
|
+
* Spreads tags on the given rings (by sweep step) until none meets
|
|
166
|
+
* another: each round, every two tags that meet move apart by one step,
|
|
167
|
+
* the earlier back and the later on, so their order holds.
|
|
168
|
+
*/
|
|
169
|
+
const spread = (rings) => {
|
|
170
|
+
const turns = [...middles];
|
|
171
|
+
for (let round = 0; round < SPREAD_ROUNDS; round++) {
|
|
172
|
+
const tags = turns.map((turn, step) => tagAt(order[step] ?? 0, rings[step] ?? 0, turn));
|
|
173
|
+
const moves = turns.map(() => 0);
|
|
174
|
+
let crowded = false;
|
|
175
|
+
for (let a = 0; a < count; a++) {
|
|
176
|
+
for (let b = a + 1; b < count; b++) {
|
|
177
|
+
if (meet(tags[a], tags[b])) {
|
|
178
|
+
crowded = true;
|
|
179
|
+
// The first and the last meet round the circle's end, so the
|
|
180
|
+
// last moves back and the first on.
|
|
181
|
+
const wraps = b - a > count / 2;
|
|
182
|
+
moves[a] = (moves[a] ?? 0) + (wraps ? SPREAD_STEP : -SPREAD_STEP);
|
|
183
|
+
moves[b] = (moves[b] ?? 0) + (wraps ? -SPREAD_STEP : SPREAD_STEP);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!crowded)
|
|
188
|
+
break;
|
|
189
|
+
turns.forEach((turn, step) => {
|
|
190
|
+
turns[step] = turn + Math.sign(moves[step] ?? 0) * SPREAD_STEP;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return turns;
|
|
194
|
+
};
|
|
195
|
+
/** The tags on `rings` once spread, each kept only where it is clear and within reach. */
|
|
196
|
+
const place = (rings) => {
|
|
197
|
+
const turns = spread(rings);
|
|
198
|
+
const kept = [];
|
|
199
|
+
const placed = arcs.map(() => null);
|
|
200
|
+
order.forEach((at, step) => {
|
|
201
|
+
const ring = rings[step] ?? 0;
|
|
202
|
+
const turn = turns[step] ?? 0;
|
|
203
|
+
const tag = tagAt(at, ring, turn);
|
|
204
|
+
if (Math.abs(turn - (middles[step] ?? 0)) <= (reaches[ring] ?? 0) &&
|
|
205
|
+
!kept.some((other) => meet(tag, other))) {
|
|
206
|
+
kept.push(tag);
|
|
207
|
+
placed[at] = tag;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return placed;
|
|
211
|
+
};
|
|
212
|
+
const oneRing = place(order.map(() => 0));
|
|
213
|
+
if (!oneRing.includes(null))
|
|
214
|
+
return oneRing;
|
|
215
|
+
// One ring cannot hold them: in each run of tags that crowd at their
|
|
216
|
+
// slices' middles, every other tag steps out to the second ring.
|
|
217
|
+
const rings = [];
|
|
218
|
+
order.forEach((at, step) => {
|
|
219
|
+
const before = step > 0 ? tagAt(order[step - 1] ?? 0, 0, middles[step - 1] ?? 0) : null;
|
|
220
|
+
const crowds = before !== null && meet(before, tagAt(at, 0, middles[step] ?? 0));
|
|
221
|
+
rings.push(crowds && rings[step - 1] === 0 ? 1 : 0);
|
|
222
|
+
});
|
|
223
|
+
const twoRings = place(rings);
|
|
224
|
+
const placed = twoRings.filter((tag) => tag === null).length <= oneRing.filter((tag) => tag === null).length
|
|
225
|
+
? twoRings
|
|
226
|
+
: oneRing;
|
|
227
|
+
// A tag left out takes the nearest clear place on either ring, if any.
|
|
228
|
+
order.forEach((at, step) => {
|
|
229
|
+
if (placed[at] !== null)
|
|
230
|
+
return;
|
|
231
|
+
const kept = placed.filter((tag) => tag !== null);
|
|
232
|
+
const middle = middles[step] ?? 0;
|
|
233
|
+
const tries = [];
|
|
234
|
+
radii.forEach((radius, ring) => {
|
|
235
|
+
for (let nudge = 0; nudge <= (reaches[ring] ?? 0); nudge += SPREAD_STEP) {
|
|
236
|
+
for (const sign of nudge === 0 ? [0] : [1, -1]) {
|
|
237
|
+
tries.push({
|
|
238
|
+
ring,
|
|
239
|
+
turn: middle + sign * nudge,
|
|
240
|
+
cost: nudge * 2 * Math.PI * radius + ring * SECOND_RING
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
tries.sort((a, b) => a.cost - b.cost);
|
|
246
|
+
for (const { ring, turn } of tries) {
|
|
247
|
+
const tag = tagAt(at, ring, turn);
|
|
248
|
+
if (!kept.some((other) => meet(tag, other))) {
|
|
249
|
+
placed[at] = tag;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
return placed;
|
|
255
|
+
}
|
|
256
|
+
/** How much smaller a pie draws to make room for its tags outside the rim, both rings and the numbers on them. */
|
|
257
|
+
export const pieTagRoom = PIE_TAG_REACH + SECOND_RING + 16;
|
|
258
|
+
const HATCHES = ['solid', 'hatch', 'cross'];
|
|
259
|
+
/**
|
|
260
|
+
* How many slices have fills of their own: each of the eight series colours
|
|
261
|
+
* solid, then hatched, then crosshatched. A chart's legend lists no more.
|
|
262
|
+
*/
|
|
263
|
+
export const DISTINCT_SLICE_FILLS = SERIES_COLOURS * HATCHES.length;
|
|
264
|
+
/**
|
|
265
|
+
* The fill of the slice at `at` (from zero) in the order given: the series
|
|
266
|
+
* colours in turn, solid for the first eight, hatched for the next eight,
|
|
267
|
+
* crosshatched for the eight after, so the first 24 slices and their legend
|
|
268
|
+
* swatches never share a fill, and two neighbours never share a colour.
|
|
269
|
+
*/
|
|
270
|
+
export const sliceFill = (at) => ({
|
|
271
|
+
hue: at % SERIES_COLOURS,
|
|
272
|
+
hatch: HATCHES[Math.floor(at / SERIES_COLOURS) % HATCHES.length] ?? 'solid'
|
|
273
|
+
});
|
|
274
|
+
const sameFill = (a, b) => a !== undefined && a.hue === b.hue && a.hatch === b.hatch;
|
|
275
|
+
/**
|
|
276
|
+
* The fills of the slices drawn, in arc order, given the place in the data
|
|
277
|
+
* of each (`places`, rising; a slice of nothing is not drawn, so places may
|
|
278
|
+
* skip). Each slice takes `sliceFill` of its place, so a slice keeps the
|
|
279
|
+
* fill its legend entry shows. Past the 24 fills, where the cycle starts
|
|
280
|
+
* over, a slice that would match the slice before it, or (the last) the
|
|
281
|
+
* first slice it meets round the circle, steps to the next way of drawing
|
|
282
|
+
* its colour, so no two neighbouring slices ever share a fill.
|
|
283
|
+
*/
|
|
284
|
+
export function pieFills(places) {
|
|
285
|
+
const fills = [];
|
|
286
|
+
places.forEach((place, at) => {
|
|
287
|
+
let fill = sliceFill(place);
|
|
288
|
+
if (place >= DISTINCT_SLICE_FILLS) {
|
|
289
|
+
const before = fills[at - 1];
|
|
290
|
+
const after = at === places.length - 1 && at > 1 ? fills[0] : undefined;
|
|
291
|
+
for (let step = 1; (sameFill(before, fill) || sameFill(after, fill)) && step < 3; step++) {
|
|
292
|
+
fill = { hue: fill.hue, hatch: HATCHES[(HATCHES.indexOf(fill.hatch) + 1) % 3] ?? 'solid' };
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
fills.push(fill);
|
|
296
|
+
});
|
|
297
|
+
return fills;
|
|
298
|
+
}
|
|
@@ -15,6 +15,7 @@ export interface PieChartProps {
|
|
|
15
15
|
* A pie chart drawn in SVG from the chart tokens, in light and dark: a slice
|
|
16
16
|
* per value, clockwise from twelve o'clock, and a legend beside it naming
|
|
17
17
|
* each slice with its value and its share. Hovering a slice or its legend
|
|
18
|
-
* row lights both.
|
|
18
|
+
* row lights both, and the other slices step back. With no slice to draw it
|
|
19
|
+
* keeps the pie's place as a dashed ring and says there is no value.
|
|
19
20
|
*/
|
|
20
21
|
export declare function PieChart({ label, slices, format, size }: PieChartProps): ReactElement;
|
package/dist/charts/PieChart.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useState } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { useId, useState } from 'react';
|
|
3
|
+
import { useLabels } from '../labels/UiokProvider.js';
|
|
4
|
+
import { pieArcs, pieFills, sliceFill } from './Pie.logic.js';
|
|
5
|
+
import { SliceHatches, slicePaint, sliceSwatch } from './PieFill.js';
|
|
5
6
|
/**
|
|
6
7
|
* A pie chart drawn in SVG from the chart tokens, in light and dark: a slice
|
|
7
8
|
* per value, clockwise from twelve o'clock, and a legend beside it naming
|
|
8
9
|
* each slice with its value and its share. Hovering a slice or its legend
|
|
9
|
-
* row lights both.
|
|
10
|
+
* row lights both, and the other slices step back. With no slice to draw it
|
|
11
|
+
* keeps the pie's place as a dashed ring and says there is no value.
|
|
10
12
|
*/
|
|
11
13
|
export function PieChart({ label, slices, format, size = 160 }) {
|
|
14
|
+
const labels = useLabels().charts;
|
|
12
15
|
const [lit, setLit] = useState(null);
|
|
13
16
|
const r = size / 2 - 2;
|
|
14
17
|
const arcs = pieArcs(slices, r);
|
|
15
|
-
|
|
18
|
+
// Past the eight colours, slices are hatched, then crosshatched, so no
|
|
19
|
+
// two neighbours and no two legend swatches share a fill.
|
|
20
|
+
const fills = pieFills(arcs.map((_, index) => index));
|
|
21
|
+
const prefix = `pie-${useId().replace(/[^\w-]/g, '')}`;
|
|
22
|
+
return (_jsxs("figure", { "aria-label": label, className: "pie-chart", children: [_jsxs("svg", { "aria-hidden": true, height: size, viewBox: `${-size / 2} ${-size / 2} ${size} ${size}`, width: size, children: [_jsx(SliceHatches, { fills: fills, prefix: prefix }), arcs.length === 0 && _jsx("circle", { className: "pie-empty-ring", r: r }), arcs.map((arc, index) => (_jsx("path", { className: lit === index ? 'pie-slice lit' : 'pie-slice', d: arc.path, fill: slicePaint(prefix, fills[index] ?? sliceFill(index)), onPointerEnter: () => setLit(index), onPointerLeave: () => setLit(null) }, arc.label)))] }), arcs.length === 0 && _jsx("p", { className: "chart-empty-text-line", children: labels.noValue }), _jsx("ul", { className: "pie-legend", children: arcs.map((arc, index) => (_jsxs("li", { className: lit === index ? 'lit' : undefined, onPointerEnter: () => setLit(index), onPointerLeave: () => setLit(null), children: [_jsx("span", { "aria-hidden": true, className: "pie-swatch", style: sliceSwatch(fills[index] ?? sliceFill(index)) }), _jsx("span", { className: "pie-label", children: arc.label }), _jsxs("span", { className: "pie-value", children: [format(arc.value), " (", (arc.share * 100).toFixed(1), "%)"] })] }, arc.label))) })] }));
|
|
16
23
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CSSProperties, ReactElement } from 'react';
|
|
2
|
+
import type { SliceFill } from './Pie.logic.js';
|
|
3
|
+
/** What an SVG shape's `fill` is set to for `fill`: its series colour, or its pattern. */
|
|
4
|
+
export declare const slicePaint: (prefix: string, fill: SliceFill) => string;
|
|
5
|
+
/**
|
|
6
|
+
* The patterns the striped fills among `fills` paint with, named from
|
|
7
|
+
* `prefix` (unique to the chart), for the chart's SVG; nothing when every
|
|
8
|
+
* fill is solid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function SliceHatches({ prefix, fills }: {
|
|
11
|
+
readonly prefix: string;
|
|
12
|
+
readonly fills: readonly SliceFill[];
|
|
13
|
+
}): ReactElement | null;
|
|
14
|
+
/** The CSS background of an HTML swatch (a legend's, a read-out's) drawn in `fill`. */
|
|
15
|
+
export declare function sliceSwatch(fill: SliceFill): CSSProperties;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { seriesColour } from './seriesColour.js';
|
|
3
|
+
/** A stripe's period and width, in pixels; the hue keeps most of the face. */
|
|
4
|
+
const PERIOD = 4;
|
|
5
|
+
const STRIPE = 1.5;
|
|
6
|
+
const patternId = (prefix, fill) => `${prefix}-${fill.hatch}-${fill.hue}`;
|
|
7
|
+
/** What an SVG shape's `fill` is set to for `fill`: its series colour, or its pattern. */
|
|
8
|
+
export const slicePaint = (prefix, fill) => fill.hatch === 'solid' ? seriesColour(fill.hue) : `url(#${patternId(prefix, fill)})`;
|
|
9
|
+
/**
|
|
10
|
+
* The patterns the striped fills among `fills` paint with, named from
|
|
11
|
+
* `prefix` (unique to the chart), for the chart's SVG; nothing when every
|
|
12
|
+
* fill is solid.
|
|
13
|
+
*/
|
|
14
|
+
export function SliceHatches({ prefix, fills }) {
|
|
15
|
+
const striped = [
|
|
16
|
+
...new Map(fills.filter((fill) => fill.hatch !== 'solid').map((fill) => [patternId(prefix, fill), fill]))
|
|
17
|
+
];
|
|
18
|
+
if (striped.length === 0)
|
|
19
|
+
return null;
|
|
20
|
+
return (_jsx("defs", { children: striped.map(([id, fill]) => (_jsxs("pattern", { height: PERIOD, id: id, patternTransform: "rotate(45)", patternUnits: "userSpaceOnUse", width: PERIOD, children: [_jsx("rect", { fill: seriesColour(fill.hue), height: PERIOD, width: PERIOD }), _jsx("rect", { className: "pie-hatch-stripe", height: PERIOD, width: STRIPE }), fill.hatch === 'cross' && (_jsx("rect", { className: "pie-hatch-stripe", height: STRIPE, width: PERIOD }))] }, id))) }));
|
|
21
|
+
}
|
|
22
|
+
/** The CSS background of an HTML swatch (a legend's, a read-out's) drawn in `fill`. */
|
|
23
|
+
export function sliceSwatch(fill) {
|
|
24
|
+
const hue = seriesColour(fill.hue);
|
|
25
|
+
const stripes = (angle) => `repeating-linear-gradient(${angle}deg, var(--surface) 0 ${STRIPE}px, transparent ${STRIPE}px ${PERIOD}px)`;
|
|
26
|
+
return {
|
|
27
|
+
background: fill.hatch === 'solid'
|
|
28
|
+
? hue
|
|
29
|
+
: fill.hatch === 'hatch'
|
|
30
|
+
? `${stripes(-45)}, ${hue}`
|
|
31
|
+
: `${stripes(-45)}, ${stripes(45)}, ${hue}`
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -31,7 +31,7 @@ export interface ResultChartProps {
|
|
|
31
31
|
* (a monotone curve), and area (filled to zero, or stacked); pie of one
|
|
32
32
|
* series; timeline over a time axis; and scatter over a number axis. Title,
|
|
33
33
|
* legend, and axis labels are drawn inside the SVG, so its picture saves
|
|
34
|
-
* whole; hovering a value reads it out
|
|
34
|
+
* whole; hovering a value reads it out beside its mark. Each mark names
|
|
35
35
|
* its value in `data-series` and `data-at`, which one listener on the
|
|
36
36
|
* drawing reads, and the hover lives in a store the read-out follows, so
|
|
37
37
|
* the marks draw again only when the chart changes.
|
|
@@ -40,7 +40,7 @@ declare function ResultChartDrawing({ type, stacked, title, xLabel, yLabel, cate
|
|
|
40
40
|
/**
|
|
41
41
|
* A chart of a table of rows in one of eight types (`ResultChartType`),
|
|
42
42
|
* drawn in SVG from the chart tokens with its title, legend, and axes inside,
|
|
43
|
-
* and a read-out
|
|
43
|
+
* and a read-out beside the mark under the pointer. It draws again only when a
|
|
44
44
|
* prop changes, so keep `categories`, `xValues`, and `series` stable.
|
|
45
45
|
*/
|
|
46
46
|
export declare const ResultChart: import("react").MemoExoticComponent<typeof ResultChartDrawing>;
|