@seed-ship/mcp-ui-solid 6.23.0 → 6.24.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 +125 -0
- package/README.md +107 -2
- package/dist/components/ChartJSRenderer.cjs +192 -66
- package/dist/components/ChartJSRenderer.cjs.map +1 -1
- package/dist/components/ChartJSRenderer.d.ts.map +1 -1
- package/dist/components/ChartJSRenderer.js +192 -66
- package/dist/components/ChartJSRenderer.js.map +1 -1
- package/dist/components/DegradedFallback.cjs +34 -2
- package/dist/components/DegradedFallback.cjs.map +1 -1
- package/dist/components/DegradedFallback.d.ts +7 -0
- package/dist/components/DegradedFallback.d.ts.map +1 -1
- package/dist/components/DegradedFallback.js +34 -2
- package/dist/components/DegradedFallback.js.map +1 -1
- package/dist/components/MissingValue.cjs +43 -0
- package/dist/components/MissingValue.cjs.map +1 -0
- package/dist/components/MissingValue.d.ts +27 -0
- package/dist/components/MissingValue.d.ts.map +1 -0
- package/dist/components/MissingValue.js +43 -0
- package/dist/components/MissingValue.js.map +1 -0
- package/dist/components/UIResourceRenderer.cjs +237 -199
- package/dist/components/UIResourceRenderer.cjs.map +1 -1
- package/dist/components/UIResourceRenderer.d.ts.map +1 -1
- package/dist/components/UIResourceRenderer.js +237 -199
- package/dist/components/UIResourceRenderer.js.map +1 -1
- package/dist/components/chart-data-table.cjs +30 -5
- package/dist/components/chart-data-table.cjs.map +1 -1
- package/dist/components/chart-data-table.d.ts +18 -1
- package/dist/components/chart-data-table.d.ts.map +1 -1
- package/dist/components/chart-data-table.js +30 -5
- package/dist/components/chart-data-table.js.map +1 -1
- package/dist/components/chart-missing-plugin.cjs +194 -0
- package/dist/components/chart-missing-plugin.cjs.map +1 -0
- package/dist/components/chart-missing-plugin.d.ts +127 -0
- package/dist/components/chart-missing-plugin.d.ts.map +1 -0
- package/dist/components/chart-missing-plugin.js +194 -0
- package/dist/components/chart-missing-plugin.js.map +1 -0
- package/dist/context/MCPUIStringsContext.cjs +12 -1
- package/dist/context/MCPUIStringsContext.cjs.map +1 -1
- package/dist/context/MCPUIStringsContext.d.ts +62 -0
- package/dist/context/MCPUIStringsContext.d.ts.map +1 -1
- package/dist/context/MCPUIStringsContext.js +12 -1
- package/dist/context/MCPUIStringsContext.js.map +1 -1
- package/dist/services/component-registry.cjs +9 -2
- package/dist/services/component-registry.cjs.map +1 -1
- package/dist/services/component-registry.d.ts.map +1 -1
- package/dist/services/component-registry.js +9 -2
- package/dist/services/component-registry.js.map +1 -1
- package/dist/services/validation.cjs +31 -8
- package/dist/services/validation.cjs.map +1 -1
- package/dist/services/validation.d.ts.map +1 -1
- package/dist/services/validation.js +31 -8
- package/dist/services/validation.js.map +1 -1
- package/dist/types/index.d.ts +14 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/degraded-projections.cjs +19 -3
- package/dist/utils/degraded-projections.cjs.map +1 -1
- package/dist/utils/degraded-projections.d.ts +22 -2
- package/dist/utils/degraded-projections.d.ts.map +1 -1
- package/dist/utils/degraded-projections.js +19 -3
- package/dist/utils/degraded-projections.js.map +1 -1
- package/dist/utils/missing-value.cjs +60 -0
- package/dist/utils/missing-value.cjs.map +1 -0
- package/dist/utils/missing-value.d.ts +79 -0
- package/dist/utils/missing-value.d.ts.map +1 -0
- package/dist/utils/missing-value.js +60 -0
- package/dist/utils/missing-value.js.map +1 -0
- package/package.json +3 -3
- package/src/components/ChartJSRenderer.tsx +159 -6
- package/src/components/DegradedFallback.tsx +24 -2
- package/src/components/MissingValue.tsx +61 -0
- package/src/components/UIResourceRenderer.tsx +42 -9
- package/src/components/chart-data-table.ts +50 -6
- package/src/components/chart-missing-plugin.ts +334 -0
- package/src/context/MCPUIStringsContext.tsx +76 -0
- package/src/services/component-registry.ts +10 -2
- package/src/services/validation.ts +44 -9
- package/src/types/index.ts +13 -1
- package/src/utils/degraded-projections.ts +44 -4
- package/src/utils/missing-value.ts +127 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Missing values in a Chart.js chart (v6.24.0) — everything the renderer adds
|
|
3
|
+
* to its Chart.js config when a payload says a value is MISSING (`null`).
|
|
4
|
+
*
|
|
5
|
+
* The rule: absence is never zero. Chart.js already keeps the slot of a `null`
|
|
6
|
+
* (no label dropped) and draws nothing for it; what it does not do is make the
|
|
7
|
+
* absence READABLE. A null bar is a zero-height bar that is skipped when
|
|
8
|
+
* drawn, a null line point is a hole the line may bridge (`spanGaps`), and the
|
|
9
|
+
* legend, tooltip and axis know nothing of either. This module supplies:
|
|
10
|
+
*
|
|
11
|
+
* - {@link createMissingValuesPlugin} — a PER-CHART plugin (never
|
|
12
|
+
* `Chart.register`ed, so it can leak into no other chart) that outlines a
|
|
13
|
+
* dashed stub where a bar should have been (not on stacked or overlaid
|
|
14
|
+
* bars, whose baseline belongs to another series), and says "no data" on a
|
|
15
|
+
* chart with nothing to draw;
|
|
16
|
+
* - {@link createMissingLegendGenerator} — marks a fully-missing series in the
|
|
17
|
+
* legend;
|
|
18
|
+
* - {@link createTooltipLabel} — names a missing value, and appends the unit;
|
|
19
|
+
* - {@link withDatasetsNoGapSpan} — the shallow-cloned data with
|
|
20
|
+
* `spanGaps: false`;
|
|
21
|
+
* - {@link applyUnitAxisTitle} — the unit as the value axis title.
|
|
22
|
+
*
|
|
23
|
+
* Runtime-free on purpose (no `solid-js`, no `chart.js` import): the renderer
|
|
24
|
+
* hands in the Chart.js pieces it loaded and the chrome strings (lazily, as
|
|
25
|
+
* getters, so a late locale change is honoured at draw time). Every function
|
|
26
|
+
* is defensive — a drawing hook that throws would blank the whole chart.
|
|
27
|
+
*
|
|
28
|
+
* Chart.js 4.5.1 facts this relies on (verified in `dist/chart.js`):
|
|
29
|
+
* - a bar whose parsed value is `null` gets `base = head = vScale.getBasePixel()`
|
|
30
|
+
* and no `skip` flag (BarController.updateElements), and `BarController.draw`
|
|
31
|
+
* skips it (`getParsed(i)[vScale.axis] !== null`) — so nothing is painted;
|
|
32
|
+
* - the bar element still carries its true index-axis thickness (`width` for a
|
|
33
|
+
* vertical bar, `height` for a horizontal one).
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import type { MCPUIStrings } from '../context/MCPUIStringsContext';
|
|
37
|
+
import { formatMCPUIString } from '../utils/format-string';
|
|
38
|
+
import { isMissingChartValue } from '../utils/missing-value';
|
|
39
|
+
|
|
40
|
+
type Strings = Required<MCPUIStrings>;
|
|
41
|
+
|
|
42
|
+
/** Length, in px along the value axis, of the dashed stub of a missing bar. */
|
|
43
|
+
export const MISSING_BAR_STUB_LENGTH = 12;
|
|
44
|
+
/** Stroke of the stub when the dataset names no border colour. */
|
|
45
|
+
export const MISSING_BAR_FALLBACK_COLOR = '#9ca3af';
|
|
46
|
+
/** Colour of the centred "no data" text. */
|
|
47
|
+
export const NO_DATA_TEXT_COLOR = '#6b7280';
|
|
48
|
+
const STUB_DASH = [4, 3];
|
|
49
|
+
|
|
50
|
+
/** Chart types whose value axis is `y` and whose points are `{x, y}`. */
|
|
51
|
+
const POINT_CHART_TYPES = new Set(['scatter', 'bubble']);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The category of convention line a chart shows under its canvas:
|
|
55
|
+
* `'gaps'` (a line breaks), `'bars'` (a dashed outline stands in for the
|
|
56
|
+
* bar) or `'omitted'` (nothing is drawn: scatter, bubble, stacked bars).
|
|
57
|
+
*/
|
|
58
|
+
export type MissingNoteKind = 'gaps' | 'bars' | 'omitted';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether a bar chart's bars are stacked or overlaid (`stacked` on either
|
|
62
|
+
* axis). A missing bar there has no slot of its own at the baseline — the
|
|
63
|
+
* baseline belongs to the series below — so no dashed outline is drawn.
|
|
64
|
+
*/
|
|
65
|
+
export function isStackedChart(options: any): boolean {
|
|
66
|
+
return Boolean(options?.scales?.x?.stacked || options?.scales?.y?.stacked);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Which convention note a chart gets: how ITS missing values are drawn. */
|
|
70
|
+
export function missingNoteKind(chartType: string | undefined, options?: any): MissingNoteKind {
|
|
71
|
+
if (chartType === 'bar') return isStackedChart(options) ? 'omitted' : 'bars';
|
|
72
|
+
if (chartType === 'scatter' || chartType === 'bubble') return 'omitted';
|
|
73
|
+
return 'gaps';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ─── Data ──────────────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Chart.js data with every dataset shallow-cloned and `spanGaps: false`, so a
|
|
80
|
+
* line breaks at a missing value instead of joining its neighbours. Never
|
|
81
|
+
* mutates its input; `labels` and every other key are carried by reference.
|
|
82
|
+
*/
|
|
83
|
+
export function withDatasetsNoGapSpan<D extends { datasets?: unknown }>(data: D): D {
|
|
84
|
+
const datasets = Array.isArray(data?.datasets) ? data.datasets : null;
|
|
85
|
+
if (!datasets) return data;
|
|
86
|
+
return {
|
|
87
|
+
...data,
|
|
88
|
+
datasets: datasets.map((dataset: unknown) =>
|
|
89
|
+
dataset !== null && typeof dataset === 'object'
|
|
90
|
+
? { ...(dataset as object), spanGaps: false }
|
|
91
|
+
: dataset
|
|
92
|
+
),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ─── Legend ────────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
type LegendGenerator = (chart: any) => any[];
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Wraps a Chart.js legend label generator so a fully-missing dataset reads
|
|
102
|
+
* `"{series} (no data)"` (`strings.chartLegendNoData`). Every other item is
|
|
103
|
+
* passed through untouched.
|
|
104
|
+
*/
|
|
105
|
+
export function createMissingLegendGenerator(
|
|
106
|
+
base: LegendGenerator,
|
|
107
|
+
fullyMissing: readonly boolean[],
|
|
108
|
+
getStrings: () => Strings
|
|
109
|
+
): LegendGenerator {
|
|
110
|
+
return function generateLabels(this: unknown, chart: any) {
|
|
111
|
+
const items = base.call(this, chart);
|
|
112
|
+
if (!Array.isArray(items)) return items;
|
|
113
|
+
return items.map((item) => {
|
|
114
|
+
const index = item?.datasetIndex;
|
|
115
|
+
if (typeof index !== 'number' || !fullyMissing[index]) return item;
|
|
116
|
+
const text = typeof item.text === 'string' ? item.text : String(item.text ?? '');
|
|
117
|
+
return {
|
|
118
|
+
...item,
|
|
119
|
+
text: formatMCPUIString(getStrings().chartLegendNoData, { series: text }),
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ─── Tooltip ───────────────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
/** Whether a Chart.js tooltip item stands for a missing value. */
|
|
128
|
+
export function isMissingTooltipItem(item: any): boolean {
|
|
129
|
+
if (item === null || item === undefined) return false;
|
|
130
|
+
if (item.raw === null || item.raw === undefined || isMissingChartValue(item.raw)) return true;
|
|
131
|
+
const parsed = item.parsed;
|
|
132
|
+
if (parsed !== null && typeof parsed === 'object') {
|
|
133
|
+
const chartType = item.chart?.config?.type;
|
|
134
|
+
const valueKey =
|
|
135
|
+
chartType === 'radar' ? 'r' : item.chart?.options?.indexAxis === 'y' ? 'x' : 'y';
|
|
136
|
+
if (valueKey in parsed && parsed[valueKey] === null) return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Tooltip `label` callback: names a missing value (`"{series}: Missing
|
|
143
|
+
* value"`) and, with a `unit`, appends it to a present one. Otherwise it is
|
|
144
|
+
* Chart.js's default composition (dataset label, `": "`, formatted value) —
|
|
145
|
+
* including its `mode: 'dataset'` branch, which prefixes the CATEGORY label
|
|
146
|
+
* instead, since every item of that tooltip belongs to the same dataset.
|
|
147
|
+
* Hence a regular function: Chart.js calls it with the tooltip as `this`.
|
|
148
|
+
*
|
|
149
|
+
* `unit` is left off scatter and bubble tooltips: their formatted value is the
|
|
150
|
+
* pair `(x, y)`, to which one unit does not belong.
|
|
151
|
+
*/
|
|
152
|
+
export function createTooltipLabel(
|
|
153
|
+
getStrings: () => Strings,
|
|
154
|
+
unit: string | undefined,
|
|
155
|
+
chartType: string | undefined
|
|
156
|
+
): (this: any, item: any) => string {
|
|
157
|
+
const applyUnit = unit !== undefined && !POINT_CHART_TYPES.has(chartType ?? '');
|
|
158
|
+
return function label(this: any, item: any) {
|
|
159
|
+
const strings = getStrings();
|
|
160
|
+
const prefixLabel = this?.options?.mode === 'dataset' ? item?.label : item?.dataset?.label;
|
|
161
|
+
const prefix = typeof prefixLabel === 'string' && prefixLabel !== '' ? `${prefixLabel}: ` : '';
|
|
162
|
+
|
|
163
|
+
if (isMissingTooltipItem(item)) return `${prefix}${strings.missingValue}`;
|
|
164
|
+
|
|
165
|
+
const formatted = item?.formattedValue;
|
|
166
|
+
if (formatted === null || formatted === undefined) return prefix;
|
|
167
|
+
const value = applyUnit
|
|
168
|
+
? formatMCPUIString(strings.chartValueWithUnit, { value: formatted, unit: unit as string })
|
|
169
|
+
: String(formatted);
|
|
170
|
+
return `${prefix}${value}`;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ─── Axis ──────────────────────────────────────────────────────────────────
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Sets the chart's `unit` as the title of its value axis, in place on the
|
|
178
|
+
* `options` object the renderer just built (never on the payload's).
|
|
179
|
+
*
|
|
180
|
+
* Only bar, line, scatter and bubble have a value axis to title. The value
|
|
181
|
+
* axis is `y`, or `x` when the index axis is `y` (a horizontal bar or line).
|
|
182
|
+
* An existing `scales[axis].title` is the payload's own word and is kept; the
|
|
183
|
+
* rest of the scale config (a `timeAxis` block sets `scales.x`) is kept too.
|
|
184
|
+
*/
|
|
185
|
+
export function applyUnitAxisTitle(
|
|
186
|
+
options: any,
|
|
187
|
+
chartType: string | undefined,
|
|
188
|
+
unit: string | undefined
|
|
189
|
+
): void {
|
|
190
|
+
if (unit === undefined) return;
|
|
191
|
+
if (chartType !== 'bar' && chartType !== 'line' && !POINT_CHART_TYPES.has(chartType ?? '')) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const horizontal = (chartType === 'bar' || chartType === 'line') && options?.indexAxis === 'y';
|
|
195
|
+
const axis = horizontal ? 'x' : 'y';
|
|
196
|
+
if (options.scales?.[axis]?.title !== undefined) return;
|
|
197
|
+
options.scales = {
|
|
198
|
+
...options.scales,
|
|
199
|
+
[axis]: { ...options.scales?.[axis], title: { display: true, text: unit } },
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ─── Plugin ────────────────────────────────────────────────────────────────
|
|
204
|
+
|
|
205
|
+
export interface MissingValuesPluginOptions {
|
|
206
|
+
/** At least one dataset and every one of them has no observed value. */
|
|
207
|
+
allMissing: boolean;
|
|
208
|
+
/** Lazy: read at draw time. */
|
|
209
|
+
getNoDataText: () => string;
|
|
210
|
+
/** Lazy: `Chart.defaults.font.family`, when the build exposes it. */
|
|
211
|
+
getFontFamily?: () => string | undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A dataset colour for entry `index`, as a string, or `undefined`. */
|
|
215
|
+
function colorAt(color: unknown, index: number): string | undefined {
|
|
216
|
+
if (typeof color === 'string' && color !== '') return color;
|
|
217
|
+
if (Array.isArray(color) && color.length > 0) {
|
|
218
|
+
const picked = color[index % color.length];
|
|
219
|
+
return typeof picked === 'string' && picked !== '' ? picked : undefined;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Draws a dashed outline stub for every missing bar of every visible,
|
|
226
|
+
* non-stacked bar dataset, so a missing value is a visible mark, not a bar
|
|
227
|
+
* that "is" zero.
|
|
228
|
+
*
|
|
229
|
+
* The stub sits on the value-axis base, as thick as the bar would have been,
|
|
230
|
+
* and runs {@link MISSING_BAR_STUB_LENGTH}px toward the middle of the chart
|
|
231
|
+
* area (up from a bottom baseline, down from a top one, right from a left
|
|
232
|
+
* one). Clipped to the chart area.
|
|
233
|
+
*/
|
|
234
|
+
export function drawMissingBarStubs(chart: any): void {
|
|
235
|
+
const ctx = chart?.ctx;
|
|
236
|
+
const area = chart?.chartArea;
|
|
237
|
+
const datasets = chart?.data?.datasets;
|
|
238
|
+
if (!ctx || !area || !Array.isArray(datasets)) return;
|
|
239
|
+
|
|
240
|
+
const left = area.left;
|
|
241
|
+
const top = area.top;
|
|
242
|
+
const right = area.right ?? left + (area.width ?? 0);
|
|
243
|
+
const bottom = area.bottom ?? top + (area.height ?? 0);
|
|
244
|
+
|
|
245
|
+
let saved = false;
|
|
246
|
+
datasets.forEach((dataset: any, datasetIndex: number) => {
|
|
247
|
+
const meta = chart.getDatasetMeta?.(datasetIndex);
|
|
248
|
+
if (!meta) return;
|
|
249
|
+
const isBar = meta.type === 'bar' || meta.controller?.constructor?.id === 'bar';
|
|
250
|
+
if (!isBar) return;
|
|
251
|
+
// Stacked or overlaid bars: the baseline under a missing segment is the
|
|
252
|
+
// series below, so an outline there would point at the wrong series.
|
|
253
|
+
if (meta.vScale?.options?.stacked || meta.iScale?.options?.stacked) return;
|
|
254
|
+
if (typeof chart.isDatasetVisible === 'function' && !chart.isDatasetVisible(datasetIndex)) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const values: unknown[] = Array.isArray(dataset?.data) ? dataset.data : [];
|
|
258
|
+
const elements: any[] = Array.isArray(meta.data) ? meta.data : [];
|
|
259
|
+
|
|
260
|
+
values.forEach((value, index) => {
|
|
261
|
+
if (!isMissingChartValue(value)) return;
|
|
262
|
+
const element = elements[index];
|
|
263
|
+
if (!element) return;
|
|
264
|
+
const props = element.getProps?.(['x', 'y', 'base', 'width', 'height'], true) ?? element;
|
|
265
|
+
const { x, y, base, width, height } = props;
|
|
266
|
+
if (![x, y, base].every((n) => Number.isFinite(n))) return;
|
|
267
|
+
const horizontal = element.horizontal === true;
|
|
268
|
+
const thickness = horizontal ? height : width;
|
|
269
|
+
if (!Number.isFinite(thickness) || thickness <= 0) return;
|
|
270
|
+
|
|
271
|
+
let rect: [number, number, number, number];
|
|
272
|
+
if (horizontal) {
|
|
273
|
+
const dir = base < (left + right) / 2 ? 1 : -1;
|
|
274
|
+
rect = [Math.min(base, base + dir * MISSING_BAR_STUB_LENGTH), y - thickness / 2, MISSING_BAR_STUB_LENGTH, thickness];
|
|
275
|
+
} else {
|
|
276
|
+
const dir = base > (top + bottom) / 2 ? -1 : 1;
|
|
277
|
+
rect = [x - thickness / 2, Math.min(base, base + dir * MISSING_BAR_STUB_LENGTH), thickness, MISSING_BAR_STUB_LENGTH];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (!saved) {
|
|
281
|
+
ctx.save();
|
|
282
|
+
ctx.beginPath();
|
|
283
|
+
ctx.rect(left, top, right - left, bottom - top);
|
|
284
|
+
ctx.clip();
|
|
285
|
+
saved = true;
|
|
286
|
+
}
|
|
287
|
+
ctx.strokeStyle = colorAt(dataset?.borderColor, index) ?? MISSING_BAR_FALLBACK_COLOR;
|
|
288
|
+
ctx.lineWidth = 1;
|
|
289
|
+
ctx.setLineDash(STUB_DASH);
|
|
290
|
+
ctx.strokeRect(...rect);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
if (saved) {
|
|
294
|
+
ctx.setLineDash?.([]);
|
|
295
|
+
ctx.restore();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Draws `text` centred in the chart area (a chart with nothing to plot). */
|
|
300
|
+
export function drawNoDataText(chart: any, text: string, fontFamily?: string): void {
|
|
301
|
+
const ctx = chart?.ctx;
|
|
302
|
+
const area = chart?.chartArea;
|
|
303
|
+
if (!ctx || !area || !text) return;
|
|
304
|
+
const cx = (area.left + (area.right ?? area.left + (area.width ?? 0))) / 2;
|
|
305
|
+
const cy = (area.top + (area.bottom ?? area.top + (area.height ?? 0))) / 2;
|
|
306
|
+
ctx.save();
|
|
307
|
+
ctx.font = `12px ${fontFamily || 'sans-serif'}`;
|
|
308
|
+
ctx.fillStyle = NO_DATA_TEXT_COLOR;
|
|
309
|
+
ctx.textAlign = 'center';
|
|
310
|
+
ctx.textBaseline = 'middle';
|
|
311
|
+
ctx.fillText(text, cx, cy);
|
|
312
|
+
ctx.restore();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The per-chart plugin object. Passed as `config.plugins = [plugin]`: Chart.js
|
|
317
|
+
* scopes it to that one chart. Both hooks swallow their own errors — a
|
|
318
|
+
* decoration must never take the chart down.
|
|
319
|
+
*/
|
|
320
|
+
export function createMissingValuesPlugin(options: MissingValuesPluginOptions) {
|
|
321
|
+
return {
|
|
322
|
+
id: 'mcpuiMissingValues',
|
|
323
|
+
afterDatasetsDraw(chart: any) {
|
|
324
|
+
try {
|
|
325
|
+
drawMissingBarStubs(chart);
|
|
326
|
+
if (options.allMissing) {
|
|
327
|
+
drawNoDataText(chart, options.getNoDataText(), options.getFontFamily?.());
|
|
328
|
+
}
|
|
329
|
+
} catch {
|
|
330
|
+
// Cosmetic layer: never break the chart.
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
@@ -823,6 +823,70 @@ export interface MCPUIStrings {
|
|
|
823
823
|
sizeKilobytes?: string
|
|
824
824
|
/** ArtifactRenderer file size in megabytes. Template — `{size}`. */
|
|
825
825
|
sizeMegabytes?: string
|
|
826
|
+
|
|
827
|
+
// ── Missing values (tables, chart data views, charts) ──────
|
|
828
|
+
/**
|
|
829
|
+
* Accessible name of a value that is explicitly missing: read by screen
|
|
830
|
+
* readers in place of the `-` a table cell shows, and the tooltip value of
|
|
831
|
+
* a bar whose value is `null`.
|
|
832
|
+
* @since 6.24.0
|
|
833
|
+
*/
|
|
834
|
+
missingValue?: string
|
|
835
|
+
/**
|
|
836
|
+
* Legend under a table or data view that contains at least one missing
|
|
837
|
+
* value. Template — `{marker}` = the glyph the cells show (`-`).
|
|
838
|
+
* @since 6.24.0
|
|
839
|
+
*/
|
|
840
|
+
missingValueLegend?: string
|
|
841
|
+
/**
|
|
842
|
+
* Note under a line or radar chart with a missing value: the line is broken
|
|
843
|
+
* there rather than drawn through, or down to zero.
|
|
844
|
+
* @since 6.24.0
|
|
845
|
+
*/
|
|
846
|
+
chartMissingGaps?: string
|
|
847
|
+
/**
|
|
848
|
+
* Note under a bar chart with a missing value, which the chart draws as a
|
|
849
|
+
* dashed outline at the baseline so it cannot be read as a zero bar.
|
|
850
|
+
* @since 6.24.0
|
|
851
|
+
*/
|
|
852
|
+
chartMissingBars?: string
|
|
853
|
+
/**
|
|
854
|
+
* Note under a chart that draws nothing for a missing value — a scatter or
|
|
855
|
+
* bubble chart, or a stacked bar chart (whose baseline belongs to the series
|
|
856
|
+
* below, so no outline can stand in for the bar).
|
|
857
|
+
* @since 6.24.0
|
|
858
|
+
*/
|
|
859
|
+
chartMissingPoints?: string
|
|
860
|
+
/**
|
|
861
|
+
* Note naming the series whose values are ALL missing.
|
|
862
|
+
* Template — `{series}` = the series names, joined as a list in the locale.
|
|
863
|
+
* @since 6.24.0
|
|
864
|
+
*/
|
|
865
|
+
chartSeriesNoData?: string
|
|
866
|
+
/**
|
|
867
|
+
* Legend entry of a series whose values are all missing.
|
|
868
|
+
* Template — `{series}` = the series name.
|
|
869
|
+
* @since 6.24.0
|
|
870
|
+
*/
|
|
871
|
+
chartLegendNoData?: string
|
|
872
|
+
/**
|
|
873
|
+
* A value followed by the chart's `unit`, in tooltips.
|
|
874
|
+
* Template — `{value}` (already formatted), `{unit}`.
|
|
875
|
+
* @since 6.24.0
|
|
876
|
+
*/
|
|
877
|
+
chartValueWithUnit?: string
|
|
878
|
+
/**
|
|
879
|
+
* A column header followed by the chart's `unit`, in the chart data view
|
|
880
|
+
* and the degraded tables. Template — `{label}`, `{unit}`.
|
|
881
|
+
* @since 6.24.0
|
|
882
|
+
*/
|
|
883
|
+
chartLabelWithUnit?: string
|
|
884
|
+
/**
|
|
885
|
+
* Shown instead of a pie, doughnut or polar-area chart that received a
|
|
886
|
+
* missing value: those types would draw it as a zero share.
|
|
887
|
+
* @since 6.24.0
|
|
888
|
+
*/
|
|
889
|
+
chartMissingUnsupported?: string
|
|
826
890
|
}
|
|
827
891
|
|
|
828
892
|
/**
|
|
@@ -1125,6 +1189,18 @@ export const DEFAULT_MCPUI_STRINGS: Required<MCPUIStrings> = {
|
|
|
1125
1189
|
zoomOut: 'Zoom out',
|
|
1126
1190
|
zoomReset: 'Reset zoom',
|
|
1127
1191
|
zoomLevel: 'Zoom {percent}%',
|
|
1192
|
+
|
|
1193
|
+
// ── 6.24.0 — missing values ────────────────────────────────
|
|
1194
|
+
missingValue: 'Missing value',
|
|
1195
|
+
missingValueLegend: '“{marker}” marks a missing value.',
|
|
1196
|
+
chartMissingGaps: 'Breaks in a line mark missing values.',
|
|
1197
|
+
chartMissingBars: 'Dashed outlines mark missing values.',
|
|
1198
|
+
chartMissingPoints: 'Missing values are not plotted.',
|
|
1199
|
+
chartSeriesNoData: 'No data for {series}.',
|
|
1200
|
+
chartLegendNoData: '{series} (no data)',
|
|
1201
|
+
chartValueWithUnit: '{value} {unit}',
|
|
1202
|
+
chartLabelWithUnit: '{label} ({unit})',
|
|
1203
|
+
chartMissingUnsupported: 'This chart type cannot show missing values.',
|
|
1128
1204
|
}
|
|
1129
1205
|
|
|
1130
1206
|
/**
|
|
@@ -50,14 +50,15 @@ export const QuickchartRegistry: ComponentRegistryEntry = {
|
|
|
50
50
|
data: {
|
|
51
51
|
// Empty arrays match both branches; mirror Zod's inclusive union.
|
|
52
52
|
anyOf: [
|
|
53
|
-
|
|
53
|
+
// `null` is an explicit MISSING value (v6.24.0): never a zero.
|
|
54
|
+
{ type: 'array', items: { type: ['number', 'null'] } },
|
|
54
55
|
{
|
|
55
56
|
type: 'array',
|
|
56
57
|
items: {
|
|
57
58
|
type: 'object',
|
|
58
59
|
properties: {
|
|
59
60
|
x: { oneOf: [{ type: 'string' }, { type: 'number' }] },
|
|
60
|
-
y: { type: 'number' },
|
|
61
|
+
y: { type: ['number', 'null'] },
|
|
61
62
|
r: { type: 'number', minimum: 0 },
|
|
62
63
|
},
|
|
63
64
|
required: ['x', 'y'],
|
|
@@ -110,6 +111,13 @@ export const QuickchartRegistry: ComponentRegistryEntry = {
|
|
|
110
111
|
max: { type: 'string' },
|
|
111
112
|
},
|
|
112
113
|
},
|
|
114
|
+
unit: {
|
|
115
|
+
type: 'string',
|
|
116
|
+
minLength: 1,
|
|
117
|
+
maxLength: 32,
|
|
118
|
+
description:
|
|
119
|
+
'Unit of the values (e.g. "°C", "%"): shown on the value axis, in tooltips and in the data view',
|
|
120
|
+
},
|
|
113
121
|
height: { type: 'string', description: 'Chart container height as a CSS value' },
|
|
114
122
|
className: { type: 'string' },
|
|
115
123
|
},
|
|
@@ -45,6 +45,8 @@ import type {
|
|
|
45
45
|
// Runtime-free template helper: `src/validation.ts` (the SSR-safe subpath)
|
|
46
46
|
// re-exports this module, so it must stay free of any solid-js import.
|
|
47
47
|
import { formatMCPUIString } from '../utils/format-string';
|
|
48
|
+
// Runtime-free as well (v6.24.0): the missing-value rules shared with the renderers.
|
|
49
|
+
import { PART_TO_WHOLE_CHART_TYPES } from '../utils/missing-value';
|
|
48
50
|
|
|
49
51
|
/**
|
|
50
52
|
* All known ComponentType values — used to distinguish known-but-unvalidated
|
|
@@ -477,38 +479,71 @@ export function validateChartComponent(
|
|
|
477
479
|
}
|
|
478
480
|
}
|
|
479
481
|
|
|
480
|
-
// Data type validation — numbers for categorical, {x,y} objects for point charts
|
|
482
|
+
// Data type validation — numbers for categorical, {x,y} objects for point charts.
|
|
483
|
+
//
|
|
484
|
+
// `null` is an explicit missing value (v6.24.0): a number slot, or a point's
|
|
485
|
+
// `y`, that exists but was not observed. Line, bar, radar, scatter and bubble
|
|
486
|
+
// charts keep its slot and mark it. Pie, doughnut and polar-area charts
|
|
487
|
+
// cannot: Chart.js parses a null share as `0`, so the other shares and the
|
|
488
|
+
// wedge itself would all be drawn wrong — those types refuse it.
|
|
489
|
+
const missingUnsupported = PART_TO_WHOLE_CHART_TYPES.has(chartType);
|
|
481
490
|
for (const [index, dataset] of params.data.datasets.entries()) {
|
|
482
491
|
if (!Array.isArray(dataset.data)) continue;
|
|
483
492
|
const datasetIsPointChart = chartType === 'scatter' || chartType === 'bubble' || usesPoints(dataset.data);
|
|
484
493
|
for (const [dataIndex, value] of dataset.data.entries()) {
|
|
494
|
+
const path = `params.data.datasets[${index}].data[${dataIndex}]`;
|
|
485
495
|
if (datasetIsPointChart) {
|
|
486
496
|
const vObj = value as any;
|
|
487
497
|
if (
|
|
488
498
|
typeof value !== 'object' ||
|
|
489
499
|
value === null ||
|
|
490
500
|
vObj.x == null ||
|
|
491
|
-
typeof vObj.y !== 'number' ||
|
|
501
|
+
(vObj.y !== null && typeof vObj.y !== 'number') ||
|
|
492
502
|
(vObj.r !== undefined && (typeof vObj.r !== 'number' || !Number.isFinite(vObj.r) || vObj.r < 0))
|
|
493
503
|
) {
|
|
494
504
|
errors.push({
|
|
495
|
-
path
|
|
496
|
-
message: `Invalid point data: expected {x, y} object`,
|
|
505
|
+
path,
|
|
506
|
+
message: `Invalid point data: expected {x, y} object (y may be null for a missing value)`,
|
|
497
507
|
code: 'INVALID_POINT_DATA',
|
|
498
508
|
});
|
|
509
|
+
} else if (missingUnsupported && vObj.y === null) {
|
|
510
|
+
errors.push({
|
|
511
|
+
path,
|
|
512
|
+
message: `Missing value (null) is not supported by '${chartType}' charts, which would draw it as a zero share — use a bar or line chart, or a table`,
|
|
513
|
+
code: 'MISSING_VALUE_UNSUPPORTED',
|
|
514
|
+
});
|
|
499
515
|
}
|
|
500
|
-
} else {
|
|
501
|
-
if (
|
|
516
|
+
} else if (value === null) {
|
|
517
|
+
if (missingUnsupported) {
|
|
502
518
|
errors.push({
|
|
503
|
-
path
|
|
504
|
-
message: `
|
|
505
|
-
code: '
|
|
519
|
+
path,
|
|
520
|
+
message: `Missing value (null) is not supported by '${chartType}' charts, which would draw it as a zero share — use a bar or line chart, or a table`,
|
|
521
|
+
code: 'MISSING_VALUE_UNSUPPORTED',
|
|
506
522
|
});
|
|
507
523
|
}
|
|
524
|
+
} else if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
525
|
+
errors.push({
|
|
526
|
+
path,
|
|
527
|
+
message: `Invalid data value: ${value} (must be a finite number, or null for a missing value)`,
|
|
528
|
+
code: 'INVALID_DATA_TYPE',
|
|
529
|
+
});
|
|
508
530
|
}
|
|
509
531
|
}
|
|
510
532
|
}
|
|
511
533
|
|
|
534
|
+
// `unit` (v6.24.0) ends up in the value-axis title, in every tooltip and in
|
|
535
|
+
// the data-view headers: a short string, or nothing. An empty string is
|
|
536
|
+
// tolerated — the renderer treats it as "no unit" — so a producer that
|
|
537
|
+
// emits `unit: ''` does not lose its whole chart over it.
|
|
538
|
+
const unit = (params as { unit?: unknown }).unit;
|
|
539
|
+
if (unit !== undefined && (typeof unit !== 'string' || unit.length > 32)) {
|
|
540
|
+
errors.push({
|
|
541
|
+
path: 'params.unit',
|
|
542
|
+
message: 'Invalid unit: expected a string of at most 32 characters',
|
|
543
|
+
code: 'INVALID_UNIT',
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
512
547
|
return {
|
|
513
548
|
valid: errors.length === 0,
|
|
514
549
|
errors: errors.length > 0 ? errors : undefined,
|
package/src/types/index.ts
CHANGED
|
@@ -81,7 +81,13 @@ export interface ChartComponentParams {
|
|
|
81
81
|
labels: string[]
|
|
82
82
|
datasets: Array<{
|
|
83
83
|
label: string
|
|
84
|
-
|
|
84
|
+
/**
|
|
85
|
+
* `null` is an explicit missing value (v6.24.0): its label keeps its
|
|
86
|
+
* slot, a line breaks there, a bar shows a dashed outline — never a
|
|
87
|
+
* zero. Pie, doughnut and polar-area charts reject it, since they
|
|
88
|
+
* would draw it as a zero share.
|
|
89
|
+
*/
|
|
90
|
+
data: Array<number | null> | Array<{ x: string | number; y: number | null; r?: number }>
|
|
85
91
|
backgroundColor?: string | string[]
|
|
86
92
|
borderColor?: string | string[]
|
|
87
93
|
borderWidth?: number
|
|
@@ -135,6 +141,12 @@ export interface ChartComponentParams {
|
|
|
135
141
|
* Custom CSS class (Sprint 7)
|
|
136
142
|
*/
|
|
137
143
|
className?: string
|
|
144
|
+
/**
|
|
145
|
+
* Unit of every value in the chart (`'mm'`, `'km/h'`), shown as the value
|
|
146
|
+
* axis title (unless `options.scales` already sets one), after each tooltip
|
|
147
|
+
* value, and in the data-view column headers (v6.24.0). One unit per chart.
|
|
148
|
+
*/
|
|
149
|
+
unit?: string
|
|
138
150
|
}
|
|
139
151
|
|
|
140
152
|
/**
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { formatMCPUIString } from './format-string';
|
|
15
|
+
import { isMissingChartValue } from './missing-value';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Column headers and series names of the degraded tables.
|
|
@@ -46,10 +47,17 @@ export interface DegradedProjectionLabels {
|
|
|
46
47
|
feature: string;
|
|
47
48
|
/** Chart table — fallback dataset name. Template — `{n}`. */
|
|
48
49
|
series: string;
|
|
50
|
+
/**
|
|
51
|
+
* Chart table — a dataset column header followed by the chart's `unit`.
|
|
52
|
+
* Template — `{label}`, `{unit}`. Only applied when the chart has a unit.
|
|
53
|
+
* Optional so that a complete labels table written for 6.20.0–6.23.0 still
|
|
54
|
+
* type-checks. @since v6.24.0
|
|
55
|
+
*/
|
|
56
|
+
withUnit?: string;
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
/** English defaults for {@link DegradedProjectionLabels}. */
|
|
52
|
-
export const DEGRADED_PROJECTION_LABELS: DegradedProjectionLabels = {
|
|
60
|
+
export const DEGRADED_PROJECTION_LABELS: Required<DegradedProjectionLabels> = {
|
|
53
61
|
source: 'Source',
|
|
54
62
|
target: 'Target',
|
|
55
63
|
label: 'Label',
|
|
@@ -61,6 +69,7 @@ export const DEGRADED_PROJECTION_LABELS: DegradedProjectionLabels = {
|
|
|
61
69
|
marker: 'marker',
|
|
62
70
|
feature: 'feature',
|
|
63
71
|
series: 'Series {n}',
|
|
72
|
+
withUnit: '{label} ({unit})',
|
|
64
73
|
};
|
|
65
74
|
|
|
66
75
|
const withLabels = (labels?: Partial<DegradedProjectionLabels>): DegradedProjectionLabels => ({
|
|
@@ -73,6 +82,12 @@ export interface DegradedTable {
|
|
|
73
82
|
rows: Array<Array<string | number>>;
|
|
74
83
|
}
|
|
75
84
|
|
|
85
|
+
/** A degraded chart table: its cells, plus which of them are missing values. */
|
|
86
|
+
export interface DegradedChartTable extends DegradedTable {
|
|
87
|
+
/** Same shape as `rows`; `true` for a dataset entry that is missing. @since v6.24.0 */
|
|
88
|
+
missing: boolean[][];
|
|
89
|
+
}
|
|
90
|
+
|
|
76
91
|
const MAX_PROJECTED_ROWS = 200;
|
|
77
92
|
|
|
78
93
|
/** Compact a value to a single table cell string. */
|
|
@@ -188,25 +203,50 @@ export function mapToDegradedTable(params: {
|
|
|
188
203
|
* Chart → a series table: one row per label, one column per dataset. So a
|
|
189
204
|
* chart that can't draw still shows its numbers. Point/object data (scatter,
|
|
190
205
|
* bubble, time series) is stringified per cell.
|
|
206
|
+
*
|
|
207
|
+
* `missing` flags the cells whose dataset entry is a missing value (`null`
|
|
208
|
+
* or non-finite) so the table can mark them instead of showing an empty
|
|
209
|
+
* cell. A slot past the end of a shorter or empty dataset has no entry and is
|
|
210
|
+
* not flagged; a point object keeps its JSON text (where a `"y":null` reads
|
|
211
|
+
* as such) and is not flagged either. With a `unit`, dataset column headers
|
|
212
|
+
* become `labels.withUnit`.
|
|
191
213
|
*/
|
|
192
214
|
export function chartToDegradedTable(params: {
|
|
215
|
+
unit?: string;
|
|
193
216
|
data?: {
|
|
194
217
|
labels?: Array<string | number>;
|
|
195
218
|
datasets?: Array<{ label?: string; data?: unknown[] }>;
|
|
196
219
|
};
|
|
197
|
-
}, labels?: Partial<DegradedProjectionLabels>):
|
|
220
|
+
}, labels?: Partial<DegradedProjectionLabels>): DegradedChartTable {
|
|
198
221
|
const l = withLabels(labels);
|
|
222
|
+
const unit = typeof params.unit === 'string' && params.unit !== '' ? params.unit : undefined;
|
|
199
223
|
const datasets = params.data?.datasets ?? [];
|
|
200
224
|
const dataLabels = params.data?.labels ?? [];
|
|
201
225
|
const rowCount = Math.max(dataLabels.length, ...datasets.map((d) => d.data?.length ?? 0), 0);
|
|
202
226
|
|
|
203
227
|
const columns = [
|
|
204
228
|
'',
|
|
205
|
-
...datasets.map((d, i) =>
|
|
229
|
+
...datasets.map((d, i) => {
|
|
230
|
+
const name = d.label ?? formatMCPUIString(l.series, { n: i + 1 });
|
|
231
|
+
return unit === undefined
|
|
232
|
+
? name
|
|
233
|
+
: formatMCPUIString(l.withUnit ?? DEGRADED_PROJECTION_LABELS.withUnit, { label: name, unit });
|
|
234
|
+
}),
|
|
206
235
|
];
|
|
207
236
|
const rows: Array<Array<string | number>> = [];
|
|
237
|
+
const missing: boolean[][] = [];
|
|
208
238
|
for (let r = 0; r < Math.min(rowCount, MAX_PROJECTED_ROWS); r++) {
|
|
209
239
|
rows.push([cell(dataLabels[r] ?? r + 1), ...datasets.map((d) => cell(d.data?.[r]))]);
|
|
240
|
+
missing.push([
|
|
241
|
+
false,
|
|
242
|
+
...datasets.map((d) => {
|
|
243
|
+
// Past the end of a shorter or empty dataset there is no entry, so
|
|
244
|
+
// nothing to flag (the cell stays empty, as before 6.24.0).
|
|
245
|
+
if (r >= (d.data?.length ?? 0)) return false;
|
|
246
|
+
const entry = d.data?.[r];
|
|
247
|
+
return (entry === null || typeof entry !== 'object') && isMissingChartValue(entry);
|
|
248
|
+
}),
|
|
249
|
+
]);
|
|
210
250
|
}
|
|
211
|
-
return { columns, rows };
|
|
251
|
+
return { columns, rows, missing };
|
|
212
252
|
}
|