@zsherman/charts 0.0.0-stage → 0.1.1

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.
Files changed (180) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +373 -0
  3. package/README.md +13 -2
  4. package/THIRD_PARTY_NOTICES.md +33 -0
  5. package/dist/dom/bar-list/bar-list.js +386 -0
  6. package/dist/dom/bar-list/bar-list.styles.js +171 -0
  7. package/dist/dom/bar-list/index.js +1 -0
  8. package/dist/dom/chart/chart.js +716 -0
  9. package/dist/dom/chart/chart.styles.js +165 -0
  10. package/dist/dom/chart/index.js +1 -0
  11. package/dist/dom/chart-grid/chart-grid.js +67 -0
  12. package/dist/dom/chart-grid/index.js +1 -0
  13. package/dist/dom/donut/donut.js +565 -0
  14. package/dist/dom/donut/donut.styles.js +111 -0
  15. package/dist/dom/donut/index.js +1 -0
  16. package/dist/dom/heat-graph/calendar.js +118 -0
  17. package/dist/dom/heat-graph/heat-graph.js +423 -0
  18. package/dist/dom/heat-graph/heat-graph.styles.js +168 -0
  19. package/dist/dom/heat-graph/index.js +1 -0
  20. package/dist/dom/heat-matrix/heat-matrix.js +346 -0
  21. package/dist/dom/heat-matrix/heat-matrix.styles.js +155 -0
  22. package/dist/dom/heat-matrix/index.js +1 -0
  23. package/dist/dom/heat-matrix/model.js +41 -0
  24. package/dist/dom/index.js +13 -0
  25. package/dist/dom/internal/colors.js +44 -0
  26. package/dist/dom/internal/floating-tip.js +83 -0
  27. package/dist/dom/internal/format.js +43 -0
  28. package/dist/dom/internal/grid-context.js +5 -0
  29. package/dist/dom/internal/hidden.styles.js +16 -0
  30. package/dist/dom/internal/legend.js +112 -0
  31. package/dist/dom/internal/legend.styles.js +108 -0
  32. package/dist/dom/internal/markers.js +101 -0
  33. package/dist/dom/internal/options.js +300 -0
  34. package/dist/dom/internal/place.js +27 -0
  35. package/dist/dom/internal/scheme-watch.js +73 -0
  36. package/dist/dom/internal/share.js +28 -0
  37. package/dist/dom/internal/types.js +0 -0
  38. package/dist/dom/row-chart/index.js +1 -0
  39. package/dist/dom/row-chart/model.js +59 -0
  40. package/dist/dom/row-chart/row-chart.js +691 -0
  41. package/dist/dom/row-chart/row-chart.styles.js +242 -0
  42. package/dist/dom/segmented-bar/index.js +1 -0
  43. package/dist/dom/segmented-bar/segmented-bar.js +280 -0
  44. package/dist/dom/segmented-bar/segmented-bar.styles.js +88 -0
  45. package/dist/dom/sparkline/index.js +1 -0
  46. package/dist/dom/sparkline/sparkline.js +145 -0
  47. package/dist/dom/stacked-histogram/histogram.js +137 -0
  48. package/dist/dom/stacked-histogram/index.js +1 -0
  49. package/dist/dom/stacked-histogram/stacked-histogram.js +761 -0
  50. package/dist/dom/stacked-histogram/stacked-histogram.styles.js +211 -0
  51. package/dist/dom/state-timeline/index.js +1 -0
  52. package/dist/dom/state-timeline/model.js +166 -0
  53. package/dist/dom/state-timeline/state-timeline.js +729 -0
  54. package/dist/dom/state-timeline/state-timeline.styles.js +171 -0
  55. package/dist/dom/status-history/index.js +1 -0
  56. package/dist/dom/status-history/status-history.js +542 -0
  57. package/dist/dom/status-history/status-history.styles.js +171 -0
  58. package/dist/solid/bar-list/bar-list.jsx +139 -0
  59. package/dist/solid/bar-list/bar-list.styles.jsx +171 -0
  60. package/dist/solid/bar-list/index.jsx +1 -0
  61. package/dist/solid/chart/chart.jsx +454 -0
  62. package/dist/solid/chart/chart.styles.jsx +165 -0
  63. package/dist/solid/chart/index.jsx +1 -0
  64. package/dist/solid/chart-grid/chart-grid.jsx +46 -0
  65. package/dist/solid/chart-grid/index.jsx +1 -0
  66. package/dist/solid/donut/donut.jsx +291 -0
  67. package/dist/solid/donut/donut.styles.jsx +111 -0
  68. package/dist/solid/donut/index.jsx +1 -0
  69. package/dist/solid/heat-graph/calendar.jsx +118 -0
  70. package/dist/solid/heat-graph/heat-graph.jsx +228 -0
  71. package/dist/solid/heat-graph/heat-graph.styles.jsx +168 -0
  72. package/dist/solid/heat-graph/index.jsx +1 -0
  73. package/dist/solid/heat-matrix/heat-matrix.jsx +165 -0
  74. package/dist/solid/heat-matrix/heat-matrix.styles.jsx +155 -0
  75. package/dist/solid/heat-matrix/index.jsx +1 -0
  76. package/dist/solid/heat-matrix/model.jsx +41 -0
  77. package/dist/solid/index.jsx +13 -0
  78. package/dist/solid/internal/colors.jsx +44 -0
  79. package/dist/solid/internal/floating-tip.jsx +68 -0
  80. package/dist/solid/internal/format.jsx +43 -0
  81. package/dist/solid/internal/grid-context.jsx +5 -0
  82. package/dist/solid/internal/hidden.styles.jsx +16 -0
  83. package/dist/solid/internal/legend.jsx +35 -0
  84. package/dist/solid/internal/legend.styles.jsx +108 -0
  85. package/dist/solid/internal/markers.jsx +101 -0
  86. package/dist/solid/internal/options.jsx +300 -0
  87. package/dist/solid/internal/place.jsx +27 -0
  88. package/dist/solid/internal/scheme-watch.jsx +73 -0
  89. package/dist/solid/internal/share.jsx +28 -0
  90. package/dist/solid/internal/types.jsx +0 -0
  91. package/dist/solid/row-chart/index.jsx +1 -0
  92. package/dist/solid/row-chart/model.jsx +59 -0
  93. package/dist/solid/row-chart/row-chart.jsx +312 -0
  94. package/dist/solid/row-chart/row-chart.styles.jsx +242 -0
  95. package/dist/solid/segmented-bar/index.jsx +1 -0
  96. package/dist/solid/segmented-bar/segmented-bar.jsx +125 -0
  97. package/dist/solid/segmented-bar/segmented-bar.styles.jsx +88 -0
  98. package/dist/solid/sparkline/index.jsx +1 -0
  99. package/dist/solid/sparkline/sparkline.jsx +88 -0
  100. package/dist/solid/stacked-histogram/histogram.jsx +137 -0
  101. package/dist/solid/stacked-histogram/index.jsx +1 -0
  102. package/dist/solid/stacked-histogram/stacked-histogram.jsx +381 -0
  103. package/dist/solid/stacked-histogram/stacked-histogram.styles.jsx +211 -0
  104. package/dist/solid/state-timeline/index.jsx +1 -0
  105. package/dist/solid/state-timeline/model.jsx +166 -0
  106. package/dist/solid/state-timeline/state-timeline.jsx +387 -0
  107. package/dist/solid/state-timeline/state-timeline.styles.jsx +171 -0
  108. package/dist/solid/status-history/index.jsx +1 -0
  109. package/dist/solid/status-history/status-history.jsx +279 -0
  110. package/dist/solid/status-history/status-history.styles.jsx +171 -0
  111. package/dist/styles.css +1 -0
  112. package/dist/types/bar-list/bar-list.d.ts +91 -0
  113. package/dist/types/bar-list/bar-list.styles.d.ts +148 -0
  114. package/dist/types/bar-list/index.d.ts +3 -0
  115. package/dist/types/chart/chart.d.ts +98 -0
  116. package/dist/types/chart/chart.styles.d.ts +145 -0
  117. package/dist/types/chart/index.d.ts +3 -0
  118. package/dist/types/chart-grid/chart-grid.d.ts +35 -0
  119. package/dist/types/chart-grid/index.d.ts +2 -0
  120. package/dist/types/donut/donut.d.ts +124 -0
  121. package/dist/types/donut/donut.styles.d.ts +96 -0
  122. package/dist/types/donut/index.d.ts +3 -0
  123. package/dist/types/heat-graph/calendar.d.ts +61 -0
  124. package/dist/types/heat-graph/heat-graph.d.ts +82 -0
  125. package/dist/types/heat-graph/heat-graph.styles.d.ts +141 -0
  126. package/dist/types/heat-graph/index.d.ts +4 -0
  127. package/dist/types/heat-matrix/heat-matrix.d.ts +65 -0
  128. package/dist/types/heat-matrix/heat-matrix.styles.d.ts +135 -0
  129. package/dist/types/heat-matrix/index.d.ts +3 -0
  130. package/dist/types/heat-matrix/model.d.ts +15 -0
  131. package/dist/types/index.d.ts +12 -0
  132. package/dist/types/internal/colors.d.ts +22 -0
  133. package/dist/types/internal/floating-tip.d.ts +15 -0
  134. package/dist/types/internal/format.d.ts +7 -0
  135. package/dist/types/internal/grid-context.d.ts +9 -0
  136. package/dist/types/internal/hidden.styles.d.ts +15 -0
  137. package/dist/types/internal/legend.d.ts +18 -0
  138. package/dist/types/internal/legend.styles.d.ts +96 -0
  139. package/dist/types/internal/markers.d.ts +16 -0
  140. package/dist/types/internal/options.d.ts +59 -0
  141. package/dist/types/internal/place.d.ts +20 -0
  142. package/dist/types/internal/scheme-watch.d.ts +12 -0
  143. package/dist/types/internal/share.d.ts +19 -0
  144. package/dist/types/internal/types.d.ts +163 -0
  145. package/dist/types/row-chart/index.d.ts +4 -0
  146. package/dist/types/row-chart/model.d.ts +50 -0
  147. package/dist/types/row-chart/row-chart.d.ts +148 -0
  148. package/dist/types/row-chart/row-chart.styles.d.ts +216 -0
  149. package/dist/types/segmented-bar/index.d.ts +3 -0
  150. package/dist/types/segmented-bar/segmented-bar.d.ts +65 -0
  151. package/dist/types/segmented-bar/segmented-bar.styles.d.ts +75 -0
  152. package/dist/types/sparkline/index.d.ts +3 -0
  153. package/dist/types/sparkline/sparkline.d.ts +38 -0
  154. package/dist/types/stacked-histogram/histogram.d.ts +48 -0
  155. package/dist/types/stacked-histogram/index.d.ts +4 -0
  156. package/dist/types/stacked-histogram/stacked-histogram.d.ts +73 -0
  157. package/dist/types/stacked-histogram/stacked-histogram.styles.d.ts +187 -0
  158. package/dist/types/state-timeline/index.d.ts +3 -0
  159. package/dist/types/state-timeline/model.d.ts +63 -0
  160. package/dist/types/state-timeline/state-timeline.d.ts +108 -0
  161. package/dist/types/state-timeline/state-timeline.styles.d.ts +151 -0
  162. package/dist/types/status-history/index.d.ts +3 -0
  163. package/dist/types/status-history/status-history.d.ts +72 -0
  164. package/dist/types/status-history/status-history.styles.d.ts +150 -0
  165. package/docs/api.json +1346 -0
  166. package/docs/bar-list.md +190 -0
  167. package/docs/chart-grid.md +77 -0
  168. package/docs/chart.md +355 -0
  169. package/docs/donut.md +182 -0
  170. package/docs/heat-graph.md +121 -0
  171. package/docs/heat-matrix.md +108 -0
  172. package/docs/llms.txt +68 -0
  173. package/docs/row-chart.md +193 -0
  174. package/docs/search-index.json +291 -0
  175. package/docs/segmented-bar.md +128 -0
  176. package/docs/sparkline.md +82 -0
  177. package/docs/stacked-histogram.md +123 -0
  178. package/docs/state-timeline.md +141 -0
  179. package/docs/status-history.md +121 -0
  180. package/package.json +109 -4
@@ -0,0 +1,300 @@
1
+ import { markerExtent } from "./markers.jsx";
2
+ import uPlot from "uplot";
3
+ // src/internal/options.ts
4
+ /**
5
+ * Props → uPlot options. Pure apart from uPlot's path builders: colors arrive resolved (canvas can't
6
+ * read tokens), so a theme change is a rebuild with new colors.
7
+ */
8
+ const BAR_MAX = 24;
9
+ /** How far the pointer may be from a line (in CSS pixels) to focus it. */
10
+ const FOCUS_PROX = 16;
11
+ /** The other series' opacity while one is focused. */
12
+ const FOCUS_ALPHA = .25;
13
+ const BAR_GAP = 2;
14
+ /** The smallest step between neighbouring x values (1 when there's only one). */
15
+ function xStep(xs) {
16
+ let step = Infinity;
17
+ for (let i = 1; i < xs.length; i++) {
18
+ const a = xs[i - 1];
19
+ const b = xs[i];
20
+ if (a != null && b != null && b - a > 0 && b - a < step) step = b - a;
21
+ }
22
+ return Number.isFinite(step) ? step : 1;
23
+ }
24
+ /**
25
+ * Stacked series: each visible series sits on the sum of the visible ones before it (hidden ones add
26
+ * nothing, so the stack closes up when one is toggled off). `null` counts as 0 in the stack.
27
+ */
28
+ function stackData(data, hidden) {
29
+ const [xs, ...ys] = data;
30
+ const len = xs?.length ?? 0;
31
+ const sums = Array.from({ length: len }, () => 0);
32
+ const out = ys.map((series, s) => {
33
+ const row = Array.from({ length: len }, () => null);
34
+ for (let i = 0; i < len; i++) {
35
+ if (!hidden.has(s)) sums[i] += series[i] ?? 0;
36
+ row[i] = sums[i];
37
+ }
38
+ return row;
39
+ });
40
+ return [xs ?? [], ...out];
41
+ }
42
+ /**
43
+ * Grouped bars: each bar series gets a slot inside its x value's group, at most 24px wide with 2px
44
+ * between neighbours, the group centred on the value. uPlot reads the offsets (`disp.x0`) and width
45
+ * (`disp.size`) in data units at draw time, so they follow zoom and resize.
46
+ */
47
+ /** uPlot's `BarsPathBuilderFacetUnit.PixelPercent` (a const enum, so not importable here). */
48
+ const PLOT_FRACTION = 2;
49
+ /**
50
+ * Grouped bars: each bar series gets a slot inside its x value's group, at most 24px wide with 2px
51
+ * between neighbours, the group centred on the value. uPlot reads the offsets (`disp.x0`) and width
52
+ * (`disp.size`) as fractions of the plot's width at draw time, so they follow zoom and resize on any x
53
+ * scale (its data-unit sizes assume a scale that starts near zero, which a time axis doesn't).
54
+ */
55
+ function groupedBars(slot, count) {
56
+ const geometry = (u) => {
57
+ const xs = u.data[0];
58
+ const step = xStep(xs);
59
+ const x0 = xs[0] ?? 0;
60
+ const plot = u.bbox.width / (devicePixelRatio || 1);
61
+ const pxPerStep = Math.abs(u.valToPos(x0 + step, "x") - u.valToPos(x0, "x")) || 1;
62
+ const bar = Math.max(1, Math.min(BAR_MAX, (pxPerStep * .7 - (count - 1) * BAR_GAP) / count));
63
+ return {
64
+ plot,
65
+ bar,
66
+ offset: -(count * bar + (count - 1) * BAR_GAP) / 2 + slot * (bar + BAR_GAP)
67
+ };
68
+ };
69
+ return {
70
+ x0: {
71
+ unit: PLOT_FRACTION,
72
+ values: (u) => {
73
+ const { plot, offset } = geometry(u);
74
+ return Array.from(u.data[0], (x) => (u.valToPos(x, "x") + offset) / plot);
75
+ }
76
+ },
77
+ size: {
78
+ unit: PLOT_FRACTION,
79
+ values: (u) => {
80
+ const { plot, bar } = geometry(u);
81
+ return [bar / plot];
82
+ }
83
+ }
84
+ };
85
+ }
86
+ function buildOptions(input) {
87
+ const { series, x, y, theme } = input;
88
+ const categories = x?.categories;
89
+ const time = x?.time ?? !categories;
90
+ const barCount = series.filter((s) => s.type === "bar").length;
91
+ const hasBars = barCount > 0;
92
+ const lastVisible = series.reduce((last, _s, i) => input.hidden.has(i) ? last : i, -1);
93
+ let barSlot = 0;
94
+ const uSeries = [{}];
95
+ series.forEach((s, i) => {
96
+ const type = s.type ?? "line";
97
+ const stroke = input.strokes[i];
98
+ const out = {
99
+ label: s.label,
100
+ stroke,
101
+ show: !input.hidden.has(i),
102
+ spanGaps: false,
103
+ points: {
104
+ show: s.points ?? false,
105
+ size: 8,
106
+ width: 2,
107
+ stroke: theme.surface,
108
+ fill: stroke
109
+ }
110
+ };
111
+ if (type === "bar") {
112
+ const grouped = !input.stacked && barCount > 1;
113
+ out.fill = input.fills[i] ?? stroke;
114
+ if (input.stacked) {
115
+ out.stroke = theme.surface;
116
+ out.width = BAR_GAP;
117
+ } else out.width = 0;
118
+ out.points = { show: false };
119
+ out.paths = uPlot.paths.bars({
120
+ size: [.6, BAR_MAX],
121
+ gap: 0,
122
+ radius: input.stacked && i !== lastVisible ? 0 : [.2, 0],
123
+ ...grouped ? { disp: groupedBars(barSlot++, barCount) } : {}
124
+ });
125
+ } else {
126
+ out.width = s.width ?? 2;
127
+ if (s.dash) out.dash = s.dash;
128
+ if (type === "area" || input.stacked) out.fill = input.fills[i];
129
+ }
130
+ uSeries.push(out);
131
+ });
132
+ const bands = input.stacked ? series.slice(1).map((_s, i) => ({ series: [i + 2, i + 1] })) : [];
133
+ const axis = (a, gridDefault) => ({
134
+ show: a?.show ?? true,
135
+ stroke: theme.text,
136
+ font: theme.font,
137
+ labelFont: theme.labelFont,
138
+ grid: {
139
+ show: a?.grid ?? gridDefault,
140
+ stroke: theme.grid,
141
+ width: 1
142
+ },
143
+ ticks: { show: false },
144
+ ...a?.label ? {
145
+ label: a.label,
146
+ labelSize: 20,
147
+ labelGap: 4
148
+ } : {},
149
+ ...a?.size ? { size: a.size } : {},
150
+ ...a?.steps ? { incrs: a.steps } : {}
151
+ });
152
+ const xAxis = axis(x, false);
153
+ xAxis.border = {
154
+ show: true,
155
+ stroke: theme.baseline,
156
+ width: 1
157
+ };
158
+ xAxis.filter = (u, splits) => {
159
+ const right = u.bbox.width / devicePixelRatio;
160
+ return splits.map((v) => v != null && right - u.valToPos(v, "x") < 24 ? null : v);
161
+ };
162
+ if (!x?.size) xAxis.size = (_u, values) => xAxisHeight(values, x?.label ? 20 : 0);
163
+ if (categories) {
164
+ xAxis.values = (_u, ticks) => ticks.map((t) => Number.isInteger(t) ? x?.format?.(t) ?? categories[t] ?? "" : "");
165
+ xAxis.incrs = x?.steps ?? [1];
166
+ } else if (x?.format) {
167
+ const format = x.format;
168
+ xAxis.values = (_u, ticks) => ticks.map((t) => t == null ? "" : format(t));
169
+ } else if (hasBars && !time) xAxis.incrs = x?.steps ?? [1];
170
+ if (hasBars && time && !categories && !x?.steps) {
171
+ xAxis.incrs = (u) => [
172
+ 1,
173
+ 2,
174
+ 7,
175
+ 14,
176
+ 30,
177
+ 60,
178
+ 90,
179
+ 180,
180
+ 365
181
+ ].map((k) => k * xStep(u.data[0]));
182
+ xAxis.splits = (u, _axis, min, max, incr) => {
183
+ const xs = u.data[0];
184
+ const every = Math.max(1, Math.round(incr / xStep(xs)));
185
+ return Array.from(xs).filter((v, i) => i % every === 0 && v >= min && v <= max);
186
+ };
187
+ }
188
+ const yAxis = axis(y, true);
189
+ yAxis.size = y?.size ?? ((u, values) => yAxisWidth(u, values, theme.font));
190
+ const yFormat = y?.format;
191
+ yAxis.values = (_u, ticks) => ticks.map((t) => t == null ? "" : yFormat ? yFormat(t) : input.yTicks(t, ticks));
192
+ let xRange = x?.range;
193
+ if (!xRange && (hasBars || categories)) xRange = (u, min, max) => {
194
+ const half = categories ? .5 : xStep(u.data[0]) / 2;
195
+ return hasBars || categories ? [min - half, max + half] : [min, max];
196
+ };
197
+ return {
198
+ width: input.width,
199
+ height: input.height,
200
+ series: uSeries,
201
+ bands,
202
+ axes: [xAxis, yAxis],
203
+ scales: {
204
+ x: {
205
+ time,
206
+ auto: true,
207
+ ...xRange ? { range: xRange } : {}
208
+ },
209
+ y: {
210
+ auto: !y?.range,
211
+ ...y?.range ? { range: y.range } : { range: yRange(hasBars || input.stacked, markerExtent(input.markers)) }
212
+ }
213
+ },
214
+ legend: { show: false },
215
+ focus: { alpha: FOCUS_ALPHA },
216
+ cursor: {
217
+ show: input.cursor,
218
+ x: true,
219
+ y: false,
220
+ points: {
221
+ size: (_u, i) => series[i - 1]?.type === "bar" ? 0 : 8,
222
+ width: 2,
223
+ stroke: theme.surface,
224
+ fill: (_u, i) => input.strokes[i - 1] ?? theme.text
225
+ },
226
+ drag: {
227
+ x: input.brush !== false,
228
+ y: false,
229
+ setScale: input.brush === "zoom"
230
+ },
231
+ focus: input.focus ? {
232
+ prox: FOCUS_PROX,
233
+ dist: (_u, i, _idx, valPos, curPos) => series[i - 1]?.type === "bar" ? Infinity : valPos - curPos
234
+ } : { prox: -1 },
235
+ ...input.syncKey ? { sync: {
236
+ key: input.syncKey,
237
+ setSeries: false,
238
+ scales: ["x", null]
239
+ } } : {}
240
+ },
241
+ select: {
242
+ show: input.brush !== false,
243
+ left: 0,
244
+ top: 0,
245
+ width: 0,
246
+ height: 0
247
+ },
248
+ padding: input.padding,
249
+ hooks: input.hooks,
250
+ plugins: input.plugins
251
+ };
252
+ }
253
+ /**
254
+ * The y range when the axis has no fixed one: bars and stacks grow from zero to a round top; lines get
255
+ * uPlot's padded fit. Either way it takes in the y markers, so a target above the data stays in view.
256
+ */
257
+ function yRange(fromZero, extra) {
258
+ return (_u, dataMin, dataMax) => {
259
+ const min = Math.min(dataMin, ...extra);
260
+ const max = Math.max(dataMax, ...extra);
261
+ if (fromZero) return [Math.min(0, min), max === min ? max + 1 : niceMax(max)];
262
+ const [lo, hi] = uPlot.rangeNum(min, max, .1, true);
263
+ return [lo ?? min, hi ?? max];
264
+ };
265
+ }
266
+ /** A round top for a zero-based scale (bars, stacks): 1, 2 or 5 times a power of ten above the max. */
267
+ function niceMax(max) {
268
+ if (max <= 0) return max;
269
+ const pow = 10 ** Math.floor(Math.log10(max));
270
+ for (const m of [
271
+ 1,
272
+ 1.2,
273
+ 1.5,
274
+ 2,
275
+ 2.5,
276
+ 3,
277
+ 4,
278
+ 5,
279
+ 6,
280
+ 8,
281
+ 10
282
+ ]) if (m * pow >= max) return m * pow;
283
+ return max;
284
+ }
285
+ /** Tick labels may hold a second line (`"12am\n11/1"`): 16px a line, plus room for the tick gap. */
286
+ function xAxisHeight(values, label) {
287
+ return Math.max(1, ...(values ?? []).map((v) => String(v ?? "").split("\n").length)) * 16 + 10 + label;
288
+ }
289
+ let measure = null;
290
+ /** The y axis takes the width of its widest label (uPlot's default is a fixed 50px). */
291
+ function yAxisWidth(u, values, font) {
292
+ if (!values?.length) return 24;
293
+ measure ??= document.createElement("canvas").getContext("2d");
294
+ if (!measure) return 50;
295
+ measure.font = font;
296
+ const widest = Math.max(...values.map((v) => measure.measureText(v).width));
297
+ const label = u.axes[1]?.label ? 24 : 0;
298
+ return Math.ceil(widest) + 12 + label;
299
+ }
300
+ export { FOCUS_ALPHA, buildOptions, stackData, xStep };
@@ -0,0 +1,27 @@
1
+ // src/internal/place.ts
2
+ /** Space between the anchor and the tooltip. */
3
+ const TIP_GAP = 12;
4
+ /** The tooltip keeps this far from the viewport's edges. */
5
+ const TIP_MARGIN = 8;
6
+ /**
7
+ * Where a tooltip's top-left corner goes, in viewport pixels: beside its anchor (after it, or before it
8
+ * when `before`), centred on it vertically. A side that would leave the viewport flips to the other; when
9
+ * neither fits it's clamped inside. Vertically it's clamped inside the viewport, so a tooltip at the top
10
+ * or the bottom of the screen slides along its anchor instead of being cut off.
11
+ */
12
+ function placeTip(anchor, tip, viewport, before) {
13
+ const after = anchor.x + 12;
14
+ const ahead = anchor.x - 12 - tip.width;
15
+ const fitsAfter = after + tip.width <= viewport.width - 8;
16
+ const fitsBefore = ahead >= 8;
17
+ let x = before ? fitsBefore || !fitsAfter ? ahead : after : fitsAfter || !fitsBefore ? after : ahead;
18
+ x = clamp(x, 8, viewport.width - 8 - tip.width);
19
+ const y = clamp(anchor.y - tip.height / 2, 8, viewport.height - 8 - tip.height);
20
+ return {
21
+ x: Math.round(x),
22
+ y: Math.round(y)
23
+ };
24
+ }
25
+ /** `value` inside `[min, max]`; `min` wins when the range is empty (a box wider than the viewport). */
26
+ const clamp = (value, min, max) => Math.max(min, Math.min(value, max));
27
+ export { TIP_GAP, TIP_MARGIN, placeTip };
@@ -0,0 +1,73 @@
1
+ // src/internal/scheme-watch.ts
2
+ /** Tokens whose resolved values tell one scheme or theme from another. */
3
+ const SIGNALS = ["var(--sui-chart-1)", "var(--sui-text-muted)"];
4
+ const watchers = /* @__PURE__ */ new Set();
5
+ let pending = [];
6
+ let observer;
7
+ let media;
8
+ /** Each watcher's colors, resolved in one write pass and one read pass per token. */
9
+ function read(list) {
10
+ const out = list.map(() => "");
11
+ for (const css of SIGNALS) {
12
+ for (const w of list) {
13
+ w.probe.style.color = "";
14
+ w.probe.style.color = css;
15
+ }
16
+ list.forEach((w, i) => out[i] += `${getComputedStyle(w.probe).color} `);
17
+ }
18
+ return out;
19
+ }
20
+ function recheck(list) {
21
+ const live = list.filter((w) => watchers.has(w) && w.probe.isConnected);
22
+ const now = read(live);
23
+ live.forEach((w, i) => {
24
+ const seen = w.last;
25
+ w.last = now[i];
26
+ if (seen !== void 0 && seen !== now[i]) w.changed();
27
+ });
28
+ }
29
+ function bind() {
30
+ observer = new MutationObserver((records) => {
31
+ const targets = new Set(records.map((r) => r.target));
32
+ const hit = [...watchers].filter((w) => [...targets].some((t) => t.contains(w.probe)));
33
+ if (hit.length) recheck(hit);
34
+ });
35
+ observer.observe(document.documentElement, {
36
+ subtree: true,
37
+ attributes: true,
38
+ attributeFilter: ["data-color-scheme", "data-sui-theme"]
39
+ });
40
+ media = matchMedia("(prefers-color-scheme: dark)");
41
+ media.addEventListener("change", onMedia);
42
+ }
43
+ const onMedia = () => recheck([...watchers]);
44
+ function unbind() {
45
+ observer?.disconnect();
46
+ media?.removeEventListener("change", onMedia);
47
+ observer = media = void 0;
48
+ }
49
+ /**
50
+ * Calls `changed` when the colors `probe` resolves to change: its scheme or theme switched (an attribute on
51
+ * it or an ancestor, or the system preference). The first reading waits for a microtask, so charts mounting
52
+ * together are read together. Returns the cleanup.
53
+ */
54
+ function watchScheme(probe, changed) {
55
+ const w = {
56
+ probe,
57
+ last: void 0,
58
+ changed
59
+ };
60
+ if (!watchers.size) bind();
61
+ watchers.add(w);
62
+ pending.push(w);
63
+ if (pending.length === 1) queueMicrotask(() => {
64
+ const list = pending;
65
+ pending = [];
66
+ recheck(list);
67
+ });
68
+ return () => {
69
+ watchers.delete(w);
70
+ if (!watchers.size) unbind();
71
+ };
72
+ }
73
+ export { watchScheme };
@@ -0,0 +1,28 @@
1
+ // src/internal/share.ts
2
+ /**
3
+ * Part-to-whole helpers shared by Donut, Bar list and Segmented bar: what counts toward a total, a share
4
+ * written as a percent in the kit's locale, and the image's name written from the data. Plain functions, so
5
+ * each chart bundles only what it calls.
6
+ */
7
+ /** A value that counts toward a whole: a finite number above 0, else 0 (a missing or negative reading). */
8
+ const amount = (value) => value != null && Number.isFinite(value) && value > 0 ? value : 0;
9
+ /** A value's share of the total, 0 to 1 (0 when the total is 0). */
10
+ const shareOf = (value, total) => total > 0 ? amount(value) / total : 0;
11
+ /**
12
+ * A share (0 to 1) as a percent in the locale: one decimal under 10% ("4.2%"), whole above ("42%"), and
13
+ * "<0.1%" for a sliver that would otherwise read as nothing.
14
+ */
15
+ function shareFormat(locale) {
16
+ const whole = new Intl.NumberFormat(locale, {
17
+ style: "percent",
18
+ maximumFractionDigits: 0
19
+ });
20
+ const fine = new Intl.NumberFormat(locale, {
21
+ style: "percent",
22
+ maximumFractionDigits: 1
23
+ });
24
+ return (share) => share > 0 && share < .001 ? `<${fine.format(.001)}` : (share < .1 ? fine : whole).format(share);
25
+ }
26
+ /** The image's name, written from the data: "Revenue by region: EMEA 42%, Americas 35%, APAC 23%". */
27
+ const describe = (title, parts) => parts.length ? `${title}: ${parts.map((p) => `${p.label} ${p.share}`).join(", ")}` : title;
28
+ export { amount, describe, shareFormat, shareOf };
File without changes
@@ -0,0 +1 @@
1
+ export { RowChart } from "./row-chart.jsx";
@@ -0,0 +1,59 @@
1
+ import { amount, shareOf } from "../internal/share.jsx";
2
+ // src/row-chart/model.ts
3
+ /**
4
+ * RowChart's derivation: every row's bars as fractions of one shared scale, the text after each bar, the
5
+ * change from the first series, and each row's name in words. Plain functions of their input, no Solid.
6
+ */
7
+ function buildModel(input) {
8
+ const stacked = input.layout === "stacked";
9
+ const n = input.seriesCount;
10
+ const amounts = input.rows.map((r) => Array.from({ length: n }, (_v, i) => amount(r.values[i])));
11
+ let max = 0;
12
+ for (const a of amounts) max = Math.max(max, stacked ? sum(a) : Math.max(0, ...a));
13
+ if (input.max != null && Number.isFinite(input.max) && input.max > 0) max = input.max;
14
+ const scale = (v) => max > 0 ? Math.min(1, v / max) : 0;
15
+ return {
16
+ rows: input.rows.map((r, ri) => {
17
+ const values = amounts[ri];
18
+ const total = sum(values);
19
+ const first = r.values[0];
20
+ const bars = values.map((value, s) => {
21
+ const raw = r.values[s] ?? null;
22
+ const last = s === n - 1 && n > 1;
23
+ return {
24
+ series: s,
25
+ raw,
26
+ value,
27
+ fraction: scale(value),
28
+ text: raw == null ? "–" : input.format(value),
29
+ share: shareOf(value, total),
30
+ delta: !stacked && input.delta && last && raw != null && first != null && first > 0 ? (value - first) / first : void 0
31
+ };
32
+ });
33
+ const previous = input.rows[ri - 1];
34
+ return {
35
+ bars,
36
+ total,
37
+ fraction: scale(total),
38
+ groupStart: ri > 0 && (r.group ?? "") !== (previous?.group ?? "")
39
+ };
40
+ }),
41
+ max
42
+ };
43
+ }
44
+ const sum = (xs) => xs.reduce((a, b) => a + b, 0);
45
+ /** A change as a signed percent: "+54.9%", "−3.2%", "0%". */
46
+ function deltaFormat(locale) {
47
+ const fmt = new Intl.NumberFormat(locale, {
48
+ style: "percent",
49
+ maximumFractionDigits: 1,
50
+ signDisplay: "exceptZero"
51
+ });
52
+ return (d) => fmt.format(d);
53
+ }
54
+ /**
55
+ * A bar's length beside the text after it: the bar takes its fraction of the row's width less the room
56
+ * kept for the longest text, so the text always fits after the longest bar.
57
+ */
58
+ const barWidth = (fraction) => fraction > 0 ? `max(2px, calc((100% - var(--rc-text)) * ${fraction.toFixed(4)}))` : "0px";
59
+ export { barWidth, buildModel, deltaFormat };