@lostgradient/cinder 0.22.0 → 0.23.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 +31 -0
- package/components.json +1 -1
- package/dist/_internal/chart/chart-bar-model.d.ts +24 -0
- package/dist/_internal/chart/chart-cartesian-model.d.ts +16 -0
- package/dist/_internal/chart/chart-layout.d.ts +23 -0
- package/dist/_internal/chart/chart-model-utilities.d.ts +157 -0
- package/dist/_internal/chart/chart-scale.d.ts +37 -0
- package/dist/_internal/chart/chart-utilities.d.ts +7 -164
- package/dist/_internal/chart/heatmap-utilities.d.ts +5 -4
- package/dist/components/_internal/chart-tooltip.css +21 -0
- package/dist/components/_internal/chart-tooltip.svelte.d.ts +11 -0
- package/dist/components/approval-card/index.js +5 -5
- package/dist/components/area-chart/area-chart.css +27 -23
- package/dist/components/area-chart/area-chart.schema.js +38 -1
- package/dist/components/area-chart/area-chart.types.d.ts +6 -2
- package/dist/components/area-chart/index.js +1589 -768
- package/dist/components/badge/badge.css +2 -2
- package/dist/components/badge/badge.schema.js +2 -2
- package/dist/components/badge/badge.types.d.ts +3 -3
- package/dist/components/badge/index.js +4 -4
- package/dist/components/bar-chart/bar-chart.css +27 -21
- package/dist/components/bar-chart/bar-chart.schema.js +38 -1
- package/dist/components/bar-chart/bar-chart.types.d.ts +7 -3
- package/dist/components/bar-chart/index.js +1544 -740
- package/dist/components/chart.types.d.ts +98 -2
- package/dist/components/json-schema-editor/index.js +4 -4
- package/dist/components/line-chart/index.js +1587 -785
- package/dist/components/line-chart/line-chart.css +25 -24
- package/dist/components/line-chart/line-chart.schema.js +38 -1
- package/dist/components/line-chart/line-chart.types.d.ts +6 -2
- package/dist/components/matrix-chart/index.js +1038 -429
- package/dist/components/matrix-chart/matrix-chart.css +12 -6
- package/dist/components/matrix-chart/matrix-chart.schema.js +26 -1
- package/dist/components/matrix-chart/matrix-chart.types.d.ts +5 -1
- package/dist/components/payload-inspector/index.js +4 -4
- package/dist/components/run-step-timeline/index.js +6 -6
- package/dist/components/sparkbar/index.js +1291 -4
- package/dist/components/sparkbar/sparkbar.css +5 -4
- package/dist/components/sparkbar/sparkbar.schema.js +26 -1
- package/dist/components/sparkbar/sparkbar.types.d.ts +5 -0
- package/dist/components/spectrogram/index.js +1076 -437
- package/dist/components/spectrogram/spectrogram.css +7 -6
- package/dist/components/spectrogram/spectrogram.schema.js +26 -1
- package/dist/components/spectrogram/spectrogram.types.d.ts +5 -1
- package/dist/components/spectrum-chart/index.js +1037 -423
- package/dist/components/spectrum-chart/spectrum-chart.css +8 -7
- package/dist/components/spectrum-chart/spectrum-chart.schema.js +26 -1
- package/dist/components/spectrum-chart/spectrum-chart.types.d.ts +5 -1
- package/dist/components/statistic/index.js +1284 -2
- package/dist/components/statistic/statistic.css +6 -5
- package/dist/components/statistic/statistic.schema.js +26 -1
- package/dist/components/statistic/statistic.types.d.ts +5 -0
- package/dist/components/statistic-group/index.js +1284 -2
- package/dist/components/waveform/index.js +1004 -404
- package/dist/components/waveform/waveform.css +8 -7
- package/dist/components/waveform/waveform.schema.js +26 -1
- package/dist/components/waveform/waveform.types.d.ts +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17730 -16540
- package/dist/server/components/approval-card/index.js +3 -3
- package/dist/server/components/area-chart/index.js +3 -3
- package/dist/server/components/badge/index.js +2 -2
- package/dist/server/components/bar-chart/index.js +3 -3
- package/dist/server/components/json-schema-editor/index.js +2 -2
- package/dist/server/components/line-chart/index.js +3 -3
- package/dist/server/components/matrix-chart/index.js +3 -3
- package/dist/server/components/payload-inspector/index.js +3 -3
- package/dist/server/components/run-step-timeline/index.js +2 -2
- package/dist/server/components/sparkbar/index.js +3 -2
- package/dist/server/components/spectrogram/index.js +3 -3
- package/dist/server/components/spectrum-chart/index.js +2 -2
- package/dist/server/components/statistic/index.js +4 -3
- package/dist/server/components/statistic-group/index.js +4 -3
- package/dist/server/components/waveform/index.js +2 -2
- package/dist/server/index.js +21 -21
- package/dist/server/{index.server-3dkwzkd3.js → index.server-10r7yf13.js} +110 -27
- package/dist/server/{index.server-93943z3e.js → index.server-151dt1tx.js} +1 -1
- package/dist/server/{index.server-pp2n6def.js → index.server-2y0xz53g.js} +37 -14
- package/dist/server/{index.server-bd8rcgf0.js → index.server-4hvjet4z.js} +1 -1
- package/dist/server/{index.server-bsm39gpy.js → index.server-5d83fkyg.js} +75 -21
- package/dist/server/{index.server-4wweyb2e.js → index.server-8dh2rtnc.js} +1 -1
- package/dist/server/{index.server-abqbstkt.js → index.server-9gj583qs.js} +41 -14
- package/dist/server/{index.server-2nmwsacy.js → index.server-9scm7zf9.js} +4 -4
- package/dist/server/{index.server-5j6jv7rm.js → index.server-9vwxyy06.js} +108 -29
- package/dist/server/{index.server-abjsjsw8.js → index.server-afpjvfz1.js} +111 -35
- package/dist/server/{index.server-86sw5ea6.js → index.server-dw9bz17h.js} +4 -4
- package/dist/server/{index.server-d26jzn2m.js → index.server-en31rprd.js} +3 -3
- package/dist/server/{index.server-dx58qant.js → index.server-f3a0ecqg.js} +72 -9
- package/dist/server/{index.server-d63pwytk.js → index.server-kz8aeeq5.js} +18 -10
- package/dist/server/index.server-mmmrgvyd.js +1274 -0
- package/dist/server/{index.server-1221p7e4.js → index.server-scahngwv.js} +13 -1
- package/dist/server/{index.server-nj8kk5bv.js → index.server-t73q59c1.js} +1 -1
- package/dist/server/{index.server-5schb2vt.js → index.server-tdrncj5e.js} +16 -6
- package/dist/server/{index.server-wm8957be.js → index.server-veqke4sq.js} +10 -1
- package/dist/server/{index.server-fphvzcfb.js → index.server-wqajghn9.js} +1 -1
- package/dist/server/{index.server-yd9shwqq.js → index.server-zjcjzcv6.js} +1 -1
- package/package.json +1 -1
- package/src/_internal/chart/chart-bar-model.ts +330 -0
- package/src/_internal/chart/chart-cartesian-model.ts +450 -0
- package/src/_internal/chart/chart-interaction.svelte.ts +25 -6
- package/src/_internal/chart/chart-layout.ts +348 -0
- package/src/_internal/chart/chart-model-utilities.ts +357 -0
- package/src/_internal/chart/chart-scale.ts +487 -0
- package/src/_internal/chart/chart-utilities.ts +22 -1078
- package/src/_internal/chart/heatmap-utilities.ts +21 -17
- package/src/components/_internal/chart-tooltip.css +21 -0
- package/src/components/_internal/chart-tooltip.svelte +44 -0
- package/src/components/approval-card/approval-card.svelte +1 -1
- package/src/components/area-chart/area-chart.css +27 -23
- package/src/components/area-chart/area-chart.examples.json +1 -1
- package/src/components/area-chart/area-chart.schema.json +37 -0
- package/src/components/area-chart/area-chart.schema.ts +40 -0
- package/src/components/area-chart/area-chart.svelte +103 -35
- package/src/components/area-chart/area-chart.types.ts +7 -1
- package/src/components/badge/badge.css +2 -2
- package/src/components/badge/badge.schema.json +1 -1
- package/src/components/badge/badge.schema.ts +1 -1
- package/src/components/badge/badge.svelte +2 -2
- package/src/components/badge/badge.types.ts +3 -3
- package/src/components/bar-chart/bar-chart.css +27 -21
- package/src/components/bar-chart/bar-chart.schema.json +37 -0
- package/src/components/bar-chart/bar-chart.schema.ts +40 -0
- package/src/components/bar-chart/bar-chart.svelte +94 -33
- package/src/components/bar-chart/bar-chart.types.ts +9 -2
- package/src/components/chart.types.ts +112 -2
- package/src/components/line-chart/line-chart.css +25 -24
- package/src/components/line-chart/line-chart.examples.json +1 -1
- package/src/components/line-chart/line-chart.schema.json +37 -0
- package/src/components/line-chart/line-chart.schema.ts +40 -0
- package/src/components/line-chart/line-chart.svelte +95 -40
- package/src/components/line-chart/line-chart.types.ts +7 -1
- package/src/components/matrix-chart/matrix-chart.css +12 -6
- package/src/components/matrix-chart/matrix-chart.schema.json +25 -0
- package/src/components/matrix-chart/matrix-chart.schema.ts +25 -0
- package/src/components/matrix-chart/matrix-chart.svelte +30 -12
- package/src/components/matrix-chart/matrix-chart.types.ts +5 -1
- package/src/components/run-step-timeline/run-step-timeline.svelte +2 -2
- package/src/components/sparkbar/sparkbar.css +5 -4
- package/src/components/sparkbar/sparkbar.schema.json +25 -0
- package/src/components/sparkbar/sparkbar.schema.ts +26 -0
- package/src/components/sparkbar/sparkbar.svelte +8 -1
- package/src/components/sparkbar/sparkbar.types.ts +5 -0
- package/src/components/spectrogram/spectrogram.css +7 -6
- package/src/components/spectrogram/spectrogram.schema.json +25 -0
- package/src/components/spectrogram/spectrogram.schema.ts +25 -0
- package/src/components/spectrogram/spectrogram.svelte +73 -28
- package/src/components/spectrogram/spectrogram.types.ts +5 -1
- package/src/components/spectrum-chart/spectrum-chart.css +8 -7
- package/src/components/spectrum-chart/spectrum-chart.schema.json +25 -0
- package/src/components/spectrum-chart/spectrum-chart.schema.ts +26 -0
- package/src/components/spectrum-chart/spectrum-chart.svelte +38 -12
- package/src/components/spectrum-chart/spectrum-chart.types.ts +5 -1
- package/src/components/statistic/statistic.css +6 -5
- package/src/components/statistic/statistic.schema.json +25 -0
- package/src/components/statistic/statistic.schema.ts +26 -0
- package/src/components/statistic/statistic.svelte +6 -1
- package/src/components/statistic/statistic.types.ts +5 -0
- package/src/components/waveform/waveform.css +8 -7
- package/src/components/waveform/waveform.schema.json +25 -0
- package/src/components/waveform/waveform.schema.ts +26 -0
- package/src/components/waveform/waveform.svelte +16 -3
- package/src/components/waveform/waveform.types.ts +5 -1
- package/src/index.ts +8 -0
- package/dist/server/index.server-6vbpz6dd.js +0 -690
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChartAxisConfiguration,
|
|
3
|
+
ChartXAxisConfiguration,
|
|
4
|
+
} from '../../components/chart.types.ts';
|
|
5
|
+
import type { ChartGeometry } from './chart-model-utilities.ts';
|
|
6
|
+
const CHART_OUTER_PADDING = 16;
|
|
7
|
+
const CHART_GUIDE_GAP = 8;
|
|
8
|
+
const CHART_AXIS_TITLE_GAP = 12;
|
|
9
|
+
export const HORIZONTAL_CATEGORY_LABEL_GAP = 8;
|
|
10
|
+
const HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING = 8;
|
|
11
|
+
const MAXIMUM_HORIZONTAL_CATEGORY_LABEL_FRACTION = 0.4;
|
|
12
|
+
const MAXIMUM_CHART_TEXT_MEASUREMENTS = 1_024;
|
|
13
|
+
|
|
14
|
+
export type ChartGeometryOptions = {
|
|
15
|
+
xTickLabels?: string[];
|
|
16
|
+
yTickLabels?: string[];
|
|
17
|
+
xAxis?: ChartXAxisConfiguration | undefined;
|
|
18
|
+
yAxis?: ChartAxisConfiguration | undefined;
|
|
19
|
+
marginLeft?: number | undefined;
|
|
20
|
+
/** Enable browser text measurement after mount; defaults to deterministic fallback metrics. */
|
|
21
|
+
measureText?: boolean | undefined;
|
|
22
|
+
/** Mounted chart root whose inherited typography should be used for browser measurement. */
|
|
23
|
+
measurementElement?: Element | undefined;
|
|
24
|
+
/** Invalidates cached measurements after web-font loading changes. */
|
|
25
|
+
measurementVersion?: number | undefined;
|
|
26
|
+
xTickPosition?: 'top' | 'bottom';
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function createChartGeometry(
|
|
30
|
+
width: number,
|
|
31
|
+
height: number,
|
|
32
|
+
options: ChartGeometryOptions = {},
|
|
33
|
+
): ChartGeometry {
|
|
34
|
+
const xTickRotation = options.xAxis?.tickLabelRotation ?? 0;
|
|
35
|
+
if (!Number.isFinite(xTickRotation)) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`[cinder/chart] rule=invalid-tick-label-rotation tickLabelRotation="${xTickRotation}": expected a finite number.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const xTickLabels = options.xTickLabels ?? [];
|
|
41
|
+
const yTickLabels = options.yTickLabels ?? [];
|
|
42
|
+
const measurementContext = createChartTextMeasurementContext(
|
|
43
|
+
options.measureText ?? false,
|
|
44
|
+
options.measurementElement,
|
|
45
|
+
options.measurementVersion ?? 0,
|
|
46
|
+
);
|
|
47
|
+
const textMeasurements = measureChartTexts(
|
|
48
|
+
[
|
|
49
|
+
...xTickLabels.map((label) => ({ label, rotation: xTickRotation })),
|
|
50
|
+
...yTickLabels.map((label) => ({ label, rotation: 0 })),
|
|
51
|
+
...(options.xAxis?.label ? [{ label: options.xAxis.label, rotation: 0 }] : []),
|
|
52
|
+
...(options.yAxis?.label ? [{ label: options.yAxis.label, rotation: -90 }] : []),
|
|
53
|
+
],
|
|
54
|
+
measurementContext,
|
|
55
|
+
);
|
|
56
|
+
const measurementFor = (label: string, rotation: number): ChartTextMeasurement =>
|
|
57
|
+
textMeasurements.get(chartTextMeasurementKey(label, rotation, measurementContext.cacheKey)) ??
|
|
58
|
+
fallbackChartTextMeasurement(label, rotation);
|
|
59
|
+
const xTickHeight = maximumMeasurement(
|
|
60
|
+
xTickLabels,
|
|
61
|
+
(label) => measurementFor(label, xTickRotation).height,
|
|
62
|
+
);
|
|
63
|
+
const firstXTickWidth = xTickLabels[0] ? measurementFor(xTickLabels[0], xTickRotation).width : 0;
|
|
64
|
+
const lastXTickLabel = xTickLabels.at(-1);
|
|
65
|
+
const lastXTickWidth = lastXTickLabel ? measurementFor(lastXTickLabel, xTickRotation).width : 0;
|
|
66
|
+
const yTickWidth = maximumMeasurement(yTickLabels, (label) => measurementFor(label, 0).width);
|
|
67
|
+
const xAxisTitleHeight = options.xAxis?.label
|
|
68
|
+
? measurementFor(options.xAxis.label, 0).height + CHART_AXIS_TITLE_GAP
|
|
69
|
+
: 0;
|
|
70
|
+
const yAxisTitleWidth = options.yAxis?.label
|
|
71
|
+
? measurementFor(options.yAxis.label, -90).width + CHART_AXIS_TITLE_GAP
|
|
72
|
+
: 0;
|
|
73
|
+
const derivedMarginLeft = CHART_OUTER_PADDING + yTickWidth + CHART_GUIDE_GAP + yAxisTitleWidth;
|
|
74
|
+
const maximumMarginLeft = Math.max(CHART_OUTER_PADDING, Math.floor(width * 0.4));
|
|
75
|
+
const endpointLeftMargin = Math.max(
|
|
76
|
+
CHART_OUTER_PADDING,
|
|
77
|
+
Math.ceil(firstXTickWidth / 2) + CHART_GUIDE_GAP,
|
|
78
|
+
);
|
|
79
|
+
const endpointRightMargin = Math.max(
|
|
80
|
+
CHART_OUTER_PADDING,
|
|
81
|
+
Math.ceil(lastXTickWidth / 2) + CHART_GUIDE_GAP,
|
|
82
|
+
);
|
|
83
|
+
const marginLeft = Math.min(
|
|
84
|
+
maximumMarginLeft,
|
|
85
|
+
Math.max(derivedMarginLeft, options.marginLeft ?? 0, endpointLeftMargin),
|
|
86
|
+
);
|
|
87
|
+
const marginTop =
|
|
88
|
+
options.xTickPosition === 'top'
|
|
89
|
+
? Math.min(
|
|
90
|
+
Math.max(CHART_OUTER_PADDING, Math.floor(height * 0.45)),
|
|
91
|
+
CHART_OUTER_PADDING + xTickHeight + CHART_GUIDE_GAP + xAxisTitleHeight,
|
|
92
|
+
)
|
|
93
|
+
: CHART_OUTER_PADDING;
|
|
94
|
+
const marginRight = endpointRightMargin;
|
|
95
|
+
const marginBottom =
|
|
96
|
+
options.xTickPosition === 'top'
|
|
97
|
+
? CHART_OUTER_PADDING
|
|
98
|
+
: Math.min(
|
|
99
|
+
Math.max(CHART_OUTER_PADDING, Math.floor(height * 0.45)),
|
|
100
|
+
CHART_OUTER_PADDING + xTickHeight + CHART_GUIDE_GAP + xAxisTitleHeight,
|
|
101
|
+
);
|
|
102
|
+
return {
|
|
103
|
+
plotWidth: Math.max(1, width - marginLeft - marginRight),
|
|
104
|
+
plotHeight: Math.max(1, height - marginTop - marginBottom),
|
|
105
|
+
marginTop,
|
|
106
|
+
marginRight,
|
|
107
|
+
marginBottom,
|
|
108
|
+
marginLeft,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function createHorizontalCategoryLabelLayout(
|
|
113
|
+
labels: string[],
|
|
114
|
+
chartWidth: number,
|
|
115
|
+
options: Pick<
|
|
116
|
+
ChartGeometryOptions,
|
|
117
|
+
'measureText' | 'measurementElement' | 'measurementVersion'
|
|
118
|
+
> = {},
|
|
119
|
+
): { labels: string[]; marginLeft: number } {
|
|
120
|
+
const measurementContext = createChartTextMeasurementContext(
|
|
121
|
+
options.measureText ?? false,
|
|
122
|
+
options.measurementElement,
|
|
123
|
+
options.measurementVersion ?? 0,
|
|
124
|
+
);
|
|
125
|
+
const textMeasurements = measureChartTexts(
|
|
126
|
+
[...labels.map((label) => ({ label, rotation: 0 })), { label: '…', rotation: 0 }],
|
|
127
|
+
measurementContext,
|
|
128
|
+
);
|
|
129
|
+
const measurementFor = (label: string): ChartTextMeasurement =>
|
|
130
|
+
textMeasurements.get(chartTextMeasurementKey(label, 0, measurementContext.cacheKey)) ??
|
|
131
|
+
fallbackChartTextMeasurement(label, 0);
|
|
132
|
+
const maximumMarginLeft = Math.max(
|
|
133
|
+
CHART_OUTER_PADDING,
|
|
134
|
+
Math.floor(chartWidth * MAXIMUM_HORIZONTAL_CATEGORY_LABEL_FRACTION),
|
|
135
|
+
);
|
|
136
|
+
let longestLabelWidth = 0;
|
|
137
|
+
for (const label of labels) {
|
|
138
|
+
longestLabelWidth = Math.max(longestLabelWidth, measurementFor(label).width);
|
|
139
|
+
}
|
|
140
|
+
const requestedMarginLeft =
|
|
141
|
+
longestLabelWidth + HORIZONTAL_CATEGORY_LABEL_GAP + HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING;
|
|
142
|
+
const marginLeft = Math.min(
|
|
143
|
+
maximumMarginLeft,
|
|
144
|
+
Math.max(CHART_OUTER_PADDING, requestedMarginLeft),
|
|
145
|
+
);
|
|
146
|
+
const availableLabelWidth = Math.max(
|
|
147
|
+
0,
|
|
148
|
+
marginLeft - HORIZONTAL_CATEGORY_LABEL_GAP - HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
marginLeft,
|
|
153
|
+
labels: labels.map((label) =>
|
|
154
|
+
truncateHorizontalCategoryLabel(label, availableLabelWidth, measurementFor),
|
|
155
|
+
),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function observeChartFontLoading(onFontsChanged: () => void): () => void {
|
|
160
|
+
if (typeof document === 'undefined' || !document.fonts) return () => {};
|
|
161
|
+
let active = true;
|
|
162
|
+
const handleFontsChanged = (): void => {
|
|
163
|
+
if (active) onFontsChanged();
|
|
164
|
+
};
|
|
165
|
+
document.fonts.addEventListener('loadingdone', handleFontsChanged);
|
|
166
|
+
void document.fonts.ready.then(handleFontsChanged);
|
|
167
|
+
return () => {
|
|
168
|
+
active = false;
|
|
169
|
+
document.fonts.removeEventListener('loadingdone', handleFontsChanged);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function truncateHorizontalCategoryLabel(
|
|
174
|
+
label: string,
|
|
175
|
+
availableWidth: number,
|
|
176
|
+
measurementFor: (label: string) => ChartTextMeasurement,
|
|
177
|
+
): string {
|
|
178
|
+
const measuredWidth = measurementFor(label).width;
|
|
179
|
+
if (measuredWidth <= availableWidth) return label;
|
|
180
|
+
const fallbackWidth = fallbackChartTextMeasurement(label, 0).width;
|
|
181
|
+
const measurementScale = fallbackWidth > 0 ? measuredWidth / fallbackWidth : 1;
|
|
182
|
+
const estimatedWidth = (candidate: string): number =>
|
|
183
|
+
candidate === '…'
|
|
184
|
+
? measurementFor('…').width
|
|
185
|
+
: fallbackChartTextMeasurement(candidate, 0).width * measurementScale;
|
|
186
|
+
|
|
187
|
+
const characters = Array.from(label);
|
|
188
|
+
while (characters.length > 0) {
|
|
189
|
+
const truncated = `${characters.join('')}…`;
|
|
190
|
+
if (estimatedWidth(truncated) <= availableWidth) return truncated;
|
|
191
|
+
characters.pop();
|
|
192
|
+
}
|
|
193
|
+
return availableWidth >= estimatedWidth('…') ? '…' : '';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
type ChartTextMeasurement = { width: number; height: number };
|
|
197
|
+
type ChartTextMeasurementContext = {
|
|
198
|
+
cacheKey: string;
|
|
199
|
+
container?: Element;
|
|
200
|
+
textStyle?: string;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
function maximumMeasurement(labels: string[], select: (label: string) => number): number {
|
|
204
|
+
let maximum = 0;
|
|
205
|
+
for (const label of labels) maximum = Math.max(maximum, select(label));
|
|
206
|
+
return maximum;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const chartTextMeasurementCache = new Map<string, ChartTextMeasurement>();
|
|
210
|
+
|
|
211
|
+
function measureChartTexts(
|
|
212
|
+
entries: Array<{ label: string; rotation: number }>,
|
|
213
|
+
context: ChartTextMeasurementContext,
|
|
214
|
+
): Map<string, ChartTextMeasurement> {
|
|
215
|
+
if (!context.container || typeof document === 'undefined') {
|
|
216
|
+
return new Map(
|
|
217
|
+
entries.map(({ label, rotation }) => [
|
|
218
|
+
chartTextMeasurementKey(label, rotation, context.cacheKey),
|
|
219
|
+
fallbackChartTextMeasurement(label, rotation),
|
|
220
|
+
]),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
const uniqueEntries = [
|
|
224
|
+
...new Map(
|
|
225
|
+
entries.map((entry) => [
|
|
226
|
+
chartTextMeasurementKey(entry.label, entry.rotation, context.cacheKey),
|
|
227
|
+
entry,
|
|
228
|
+
]),
|
|
229
|
+
).values(),
|
|
230
|
+
];
|
|
231
|
+
const missing = uniqueEntries.filter(
|
|
232
|
+
({ label, rotation }) =>
|
|
233
|
+
!chartTextMeasurementCache.has(chartTextMeasurementKey(label, rotation, context.cacheKey)),
|
|
234
|
+
);
|
|
235
|
+
if (missing.length > 0) {
|
|
236
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
237
|
+
svg.setAttribute(
|
|
238
|
+
'style',
|
|
239
|
+
'position:absolute;visibility:hidden;pointer-events:none;inline-size:0;block-size:0',
|
|
240
|
+
);
|
|
241
|
+
const texts = missing.map((entry) => {
|
|
242
|
+
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
243
|
+
if (context.textStyle) text.setAttribute('style', context.textStyle);
|
|
244
|
+
text.textContent = entry.label;
|
|
245
|
+
svg.append(text);
|
|
246
|
+
return { ...entry, text };
|
|
247
|
+
});
|
|
248
|
+
context.container.append(svg);
|
|
249
|
+
for (const { label, rotation, text } of texts) {
|
|
250
|
+
let measurement: ChartTextMeasurement | undefined;
|
|
251
|
+
try {
|
|
252
|
+
const bounds = text.getBBox();
|
|
253
|
+
if (bounds.width > 0 && bounds.height > 0) {
|
|
254
|
+
measurement = { width: bounds.width, height: bounds.height };
|
|
255
|
+
}
|
|
256
|
+
} catch {
|
|
257
|
+
// Keep deterministic fallback metrics when layout is unavailable.
|
|
258
|
+
}
|
|
259
|
+
if (measurement) {
|
|
260
|
+
cacheChartTextMeasurement(
|
|
261
|
+
chartTextMeasurementKey(label, rotation, context.cacheKey),
|
|
262
|
+
rotateChartTextMeasurement(measurement, rotation),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
svg.remove();
|
|
267
|
+
}
|
|
268
|
+
return new Map(
|
|
269
|
+
entries.map(({ label, rotation }) => [
|
|
270
|
+
chartTextMeasurementKey(label, rotation, context.cacheKey),
|
|
271
|
+
chartTextMeasurementCache.get(chartTextMeasurementKey(label, rotation, context.cacheKey)) ??
|
|
272
|
+
fallbackChartTextMeasurement(label, rotation),
|
|
273
|
+
]),
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function cacheChartTextMeasurement(key: string, measurement: ChartTextMeasurement): void {
|
|
278
|
+
if (!chartTextMeasurementCache.has(key)) {
|
|
279
|
+
while (chartTextMeasurementCache.size >= MAXIMUM_CHART_TEXT_MEASUREMENTS) {
|
|
280
|
+
const oldestKey = chartTextMeasurementCache.keys().next().value;
|
|
281
|
+
if (oldestKey === undefined) break;
|
|
282
|
+
chartTextMeasurementCache.delete(oldestKey);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
chartTextMeasurementCache.set(key, measurement);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function chartTextMeasurementKey(
|
|
289
|
+
label: string,
|
|
290
|
+
rotation: number,
|
|
291
|
+
measurementContext: string,
|
|
292
|
+
): string {
|
|
293
|
+
return `${label}\u0000${rotation}\u0000${measurementContext}`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function createChartTextMeasurementContext(
|
|
297
|
+
allowBrowserMeasurement: boolean,
|
|
298
|
+
measurementElement: Element | undefined,
|
|
299
|
+
measurementVersion: number,
|
|
300
|
+
): ChartTextMeasurementContext {
|
|
301
|
+
if (
|
|
302
|
+
!allowBrowserMeasurement ||
|
|
303
|
+
!measurementElement ||
|
|
304
|
+
typeof window === 'undefined' ||
|
|
305
|
+
!measurementElement.isConnected
|
|
306
|
+
) {
|
|
307
|
+
return { cacheKey: 'fallback' };
|
|
308
|
+
}
|
|
309
|
+
const style = window.getComputedStyle(measurementElement);
|
|
310
|
+
const fontSize = style.getPropertyValue('--cinder-text-xs').trim() || style.fontSize;
|
|
311
|
+
const textStyle = [
|
|
312
|
+
`font-family:${style.fontFamily}`,
|
|
313
|
+
`font-size:${fontSize}`,
|
|
314
|
+
`font-style:${style.fontStyle}`,
|
|
315
|
+
`font-weight:${style.fontWeight}`,
|
|
316
|
+
`font-stretch:${style.fontStretch}`,
|
|
317
|
+
`font-variant:${style.fontVariant}`,
|
|
318
|
+
`font-feature-settings:${style.fontFeatureSettings}`,
|
|
319
|
+
`font-kerning:${style.fontKerning}`,
|
|
320
|
+
`letter-spacing:${style.letterSpacing}`,
|
|
321
|
+
`text-transform:${style.textTransform}`,
|
|
322
|
+
].join(';');
|
|
323
|
+
return {
|
|
324
|
+
cacheKey: `browser\u0000${measurementVersion}\u0000${textStyle}`,
|
|
325
|
+
container: measurementElement,
|
|
326
|
+
textStyle,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function fallbackChartTextMeasurement(label: string, rotation: number): ChartTextMeasurement {
|
|
331
|
+
return rotateChartTextMeasurement({ width: Array.from(label).length * 7, height: 12 }, rotation);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function rotateChartTextMeasurement(
|
|
335
|
+
measurement: ChartTextMeasurement,
|
|
336
|
+
rotation: number,
|
|
337
|
+
): ChartTextMeasurement {
|
|
338
|
+
if (rotation === 0) return measurement;
|
|
339
|
+
const radians = (Math.abs(rotation) * Math.PI) / 180;
|
|
340
|
+
return {
|
|
341
|
+
width:
|
|
342
|
+
Math.abs(measurement.width * Math.cos(radians)) +
|
|
343
|
+
Math.abs(measurement.height * Math.sin(radians)),
|
|
344
|
+
height:
|
|
345
|
+
Math.abs(measurement.width * Math.sin(radians)) +
|
|
346
|
+
Math.abs(measurement.height * Math.cos(radians)),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChartAxisConfiguration,
|
|
3
|
+
ChartDataTableVisibility,
|
|
4
|
+
ChartFormatterContext,
|
|
5
|
+
ChartLegendPosition,
|
|
6
|
+
ChartNumericValue,
|
|
7
|
+
ChartPoint,
|
|
8
|
+
ChartTheme,
|
|
9
|
+
ChartXValue,
|
|
10
|
+
ResolvedChartTheme,
|
|
11
|
+
} from '../../components/chart.types.ts';
|
|
12
|
+
|
|
13
|
+
export const chartPalette = [
|
|
14
|
+
'var(--cinder-chart-series-1)',
|
|
15
|
+
'var(--cinder-chart-series-2)',
|
|
16
|
+
'var(--cinder-chart-series-3)',
|
|
17
|
+
'var(--cinder-chart-series-4)',
|
|
18
|
+
'var(--cinder-chart-series-5)',
|
|
19
|
+
'var(--cinder-chart-series-6)',
|
|
20
|
+
'var(--cinder-chart-series-7)',
|
|
21
|
+
'var(--cinder-chart-series-8)',
|
|
22
|
+
] as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolves a palette color for a series by index, wrapping around the palette.
|
|
26
|
+
* Exported so chart components can render legend swatches using the same
|
|
27
|
+
* resolved color the chart uses for the series itself.
|
|
28
|
+
*/
|
|
29
|
+
export function chartPaletteColor(
|
|
30
|
+
index: number,
|
|
31
|
+
palette: readonly string[] = chartPalette,
|
|
32
|
+
): string {
|
|
33
|
+
const resolvedPalette = palette.length > 0 ? palette : chartPalette;
|
|
34
|
+
if (index < 0) return resolvedPalette[0] ?? chartPalette[0];
|
|
35
|
+
return resolvedPalette[index % resolvedPalette.length] ?? chartPalette[0];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function resolveChartTheme(theme: ChartTheme | undefined = undefined): ResolvedChartTheme {
|
|
39
|
+
return {
|
|
40
|
+
foreground: theme?.foreground ?? 'currentColor',
|
|
41
|
+
muted: theme?.muted ?? 'currentColor',
|
|
42
|
+
grid: theme?.grid ?? 'currentColor',
|
|
43
|
+
background: theme?.background ?? 'transparent',
|
|
44
|
+
palette: theme?.palette?.length ? [...theme.palette] : [...chartPalette],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function chartResourceSegment(segment: string): string {
|
|
49
|
+
return encodeURIComponent(segment)
|
|
50
|
+
.replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)
|
|
51
|
+
.replaceAll('_', '_u')
|
|
52
|
+
.replaceAll('-', '_d')
|
|
53
|
+
.replaceAll('%', '_x');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function chartResourceId(idPrefix: string, resource: string, seriesId: string): string {
|
|
57
|
+
// Escape the two sentinel characters as well as URI percent escapes so
|
|
58
|
+
// distinct IDs such as `a/b` and `a_2Fb` cannot collapse to one resource ID.
|
|
59
|
+
return [idPrefix, resource, seriesId].map(chartResourceSegment).join('-');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type ChartMark =
|
|
63
|
+
| { type: 'line'; data: ChartPoint[]; stroke?: string; strokeWidth?: number }
|
|
64
|
+
| { type: 'area'; data: ChartPoint[]; fill?: string; fillOpacity?: number }
|
|
65
|
+
| { type: 'bar'; data: ChartPoint[]; fill?: string }
|
|
66
|
+
| { type: 'point'; data: ChartPoint[]; fill?: string; radius?: number };
|
|
67
|
+
|
|
68
|
+
export type NormalizedXValue = {
|
|
69
|
+
raw: ChartXValue;
|
|
70
|
+
key: string;
|
|
71
|
+
label: string;
|
|
72
|
+
comparable: string | number;
|
|
73
|
+
kind: 'string' | 'number' | 'date';
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type NormalizedPoint = {
|
|
77
|
+
seriesId: string;
|
|
78
|
+
seriesLabel: string;
|
|
79
|
+
color: string;
|
|
80
|
+
x: NormalizedXValue;
|
|
81
|
+
y: number | null;
|
|
82
|
+
originalY: ChartNumericValue;
|
|
83
|
+
index: number;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A point that has been placed in pixel space by the chart model. Components
|
|
88
|
+
* read `pixelX`/`pixelY` directly without a secondary lookup against targets;
|
|
89
|
+
* filled marks use `pixelY0` as their resolved lower baseline.
|
|
90
|
+
*/
|
|
91
|
+
export type PlacedPoint = NormalizedPoint & {
|
|
92
|
+
pixelX: number;
|
|
93
|
+
pixelY: number;
|
|
94
|
+
pixelY0: number;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type ChartTarget = {
|
|
98
|
+
id: string;
|
|
99
|
+
seriesId: string;
|
|
100
|
+
seriesLabel: string;
|
|
101
|
+
xLabel: string;
|
|
102
|
+
valueLabel: string;
|
|
103
|
+
x: number;
|
|
104
|
+
y: number;
|
|
105
|
+
width?: number;
|
|
106
|
+
height?: number;
|
|
107
|
+
color: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export type ChartGeometry = {
|
|
111
|
+
plotWidth: number;
|
|
112
|
+
plotHeight: number;
|
|
113
|
+
marginTop: number;
|
|
114
|
+
marginRight: number;
|
|
115
|
+
marginBottom: number;
|
|
116
|
+
marginLeft: number;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type CartesianChartTableRow = {
|
|
120
|
+
id: string;
|
|
121
|
+
seriesId: string;
|
|
122
|
+
seriesLabel: string;
|
|
123
|
+
xLabel: string;
|
|
124
|
+
valueLabel: string;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export type BarChartTableRow = {
|
|
128
|
+
categoryKey: string;
|
|
129
|
+
categoryLabel: string;
|
|
130
|
+
values: Array<{ seriesId: string; seriesLabel: string; valueLabel: string }>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export type ChartTableRow = CartesianChartTableRow | BarChartTableRow;
|
|
134
|
+
|
|
135
|
+
export type ChartSceneModel<TTableRow extends ChartTableRow = ChartTableRow> = {
|
|
136
|
+
geometry: ChartGeometry;
|
|
137
|
+
targets: ChartTarget[];
|
|
138
|
+
tableRows: TTableRow[];
|
|
139
|
+
empty: boolean;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* An x-axis tick paired with its already-scaled pixel position. Charts render
|
|
144
|
+
* labels at the model-provided `x` so labels and points stay aligned for
|
|
145
|
+
* numeric and date domains.
|
|
146
|
+
*/
|
|
147
|
+
export type ChartXTick = {
|
|
148
|
+
label: string;
|
|
149
|
+
x: number;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type CartesianChartModel = ChartSceneModel<CartesianChartTableRow> & {
|
|
153
|
+
/** Pre-scaled x-axis ticks. Render labels at `tick.x`, not by ordinal index. */
|
|
154
|
+
xTicks: ChartXTick[];
|
|
155
|
+
yTicks: number[];
|
|
156
|
+
normalizedSeries: Array<{
|
|
157
|
+
id: string;
|
|
158
|
+
label: string;
|
|
159
|
+
color: string;
|
|
160
|
+
points: PlacedPoint[];
|
|
161
|
+
path: string;
|
|
162
|
+
areaPath: string;
|
|
163
|
+
hidden: boolean;
|
|
164
|
+
}>;
|
|
165
|
+
/** Targets sorted by `x` (binary-search precondition for nearestTarget). */
|
|
166
|
+
yDomain: [number, number];
|
|
167
|
+
theme: ResolvedChartTheme;
|
|
168
|
+
marks: Array<{ seriesId: string; descriptors: ChartMark[] }>;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type BarChartModel = ChartSceneModel<BarChartTableRow> & {
|
|
172
|
+
categories: NormalizedXValue[];
|
|
173
|
+
yTicks: number[];
|
|
174
|
+
categoryTicks: Array<{
|
|
175
|
+
categoryKey: string;
|
|
176
|
+
label: string;
|
|
177
|
+
fullLabel: string;
|
|
178
|
+
x: number;
|
|
179
|
+
y: number;
|
|
180
|
+
}>;
|
|
181
|
+
bars: import('../../components/chart.types.ts').BarChartPlacedBar[];
|
|
182
|
+
valueDomain: [number, number];
|
|
183
|
+
theme: ResolvedChartTheme;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export function assertValidChartNumber(
|
|
187
|
+
componentId: string,
|
|
188
|
+
rule: string,
|
|
189
|
+
value: number,
|
|
190
|
+
label: string,
|
|
191
|
+
): void {
|
|
192
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`[cinder/${componentId}] rule=${rule} ${label}="${value}": expected a positive finite number.`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function assertValidNonNegativeInteger(
|
|
200
|
+
componentId: string,
|
|
201
|
+
rule: string,
|
|
202
|
+
value: number,
|
|
203
|
+
label: string,
|
|
204
|
+
): void {
|
|
205
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`[cinder/${componentId}] rule=${rule} ${label}="${value}": expected a non-negative integer.`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function assertValidTickCount(componentId: string, axis?: ChartAxisConfiguration): void {
|
|
213
|
+
if (axis?.tickCount === undefined) return;
|
|
214
|
+
if (!Number.isInteger(axis.tickCount) || axis.tickCount <= 0) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`[cinder/${componentId}] rule=invalid-tick-count tickCount="${axis.tickCount}": expected a positive integer.`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function dataTableClass(visibility: ChartDataTableVisibility): string | undefined {
|
|
222
|
+
return visibility === 'screen-reader-only' ? 'cinder-sr-only' : undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function formatDataTableCaption(
|
|
226
|
+
caption: string,
|
|
227
|
+
displayedRowCount: number,
|
|
228
|
+
totalRowCount: number,
|
|
229
|
+
): string {
|
|
230
|
+
return totalRowCount > displayedRowCount
|
|
231
|
+
? `${caption} (showing ${displayedRowCount} of ${totalRowCount} data points)`
|
|
232
|
+
: caption;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function formatXValue(
|
|
236
|
+
value: NormalizedXValue,
|
|
237
|
+
axis: ChartAxisConfiguration | undefined,
|
|
238
|
+
context: ChartFormatterContext,
|
|
239
|
+
): string {
|
|
240
|
+
return axis?.format ? axis.format(value.raw, context) : value.label;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function formatNumericValue(
|
|
244
|
+
value: number,
|
|
245
|
+
axis: ChartAxisConfiguration | undefined,
|
|
246
|
+
formatter: ((value: number, context: ChartFormatterContext) => string) | undefined,
|
|
247
|
+
context: ChartFormatterContext,
|
|
248
|
+
): string {
|
|
249
|
+
if (formatter) return formatter(value, context);
|
|
250
|
+
if (axis?.format) return axis.format(value, context);
|
|
251
|
+
// Locale `undefined` inherits the browser's current locale rather than
|
|
252
|
+
// baking en-US formatting into every consumer.
|
|
253
|
+
return new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }).format(value);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function normalizeXValue(value: ChartXValue): NormalizedXValue {
|
|
257
|
+
if (value instanceof Date) {
|
|
258
|
+
const comparable = value.getTime();
|
|
259
|
+
return {
|
|
260
|
+
raw: value,
|
|
261
|
+
key: `date:${comparable}`,
|
|
262
|
+
label: value.toISOString(),
|
|
263
|
+
comparable,
|
|
264
|
+
kind: 'date',
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
if (typeof value === 'number') {
|
|
268
|
+
return {
|
|
269
|
+
raw: value,
|
|
270
|
+
key: `number:${value}`,
|
|
271
|
+
label: String(value),
|
|
272
|
+
comparable: value,
|
|
273
|
+
kind: 'number',
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
return { raw: value, key: `string:${value}`, label: value, comparable: value, kind: 'string' };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function assertUniqueSeriesIds(componentId: string, series: Array<{ id: string }>): void {
|
|
280
|
+
const seen = new Set<string>();
|
|
281
|
+
for (const item of series) {
|
|
282
|
+
if (seen.has(item.id)) {
|
|
283
|
+
throw new Error(
|
|
284
|
+
`[cinder/${componentId}] rule=duplicate-series-id series="${item.id}": duplicate series ids are not supported.`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
seen.add(item.id);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function toggleSeriesId(hiddenSeriesIds: string[], seriesId: string): string[] {
|
|
292
|
+
return hiddenSeriesIds.includes(seriesId)
|
|
293
|
+
? hiddenSeriesIds.filter((id) => id !== seriesId)
|
|
294
|
+
: [...hiddenSeriesIds, seriesId];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function nearestTarget(
|
|
298
|
+
targets: readonly ChartTarget[],
|
|
299
|
+
x: number,
|
|
300
|
+
y: number,
|
|
301
|
+
axis: 'x' | 'y' = 'x',
|
|
302
|
+
): ChartTarget | undefined {
|
|
303
|
+
if (targets.length === 0) return undefined;
|
|
304
|
+
if (targets.length === 1) return targets[0];
|
|
305
|
+
const pointerKey = axis === 'x' ? x : y;
|
|
306
|
+
// Binary search for the leftmost target whose key is >= pointer, then
|
|
307
|
+
// compare the adjacent dominant-axis buckets. The nearest target can live in
|
|
308
|
+
// either bucket when the pointer is between two x/y positions.
|
|
309
|
+
let low = 0;
|
|
310
|
+
let high = targets.length - 1;
|
|
311
|
+
while (low < high) {
|
|
312
|
+
const mid = (low + high) >> 1;
|
|
313
|
+
const midKey = axis === 'x' ? (targets[mid]?.x ?? 0) : (targets[mid]?.y ?? 0);
|
|
314
|
+
if (midKey < pointerKey) low = mid + 1;
|
|
315
|
+
else high = mid;
|
|
316
|
+
}
|
|
317
|
+
const candidates: ChartTarget[] = [];
|
|
318
|
+
|
|
319
|
+
function addBucket(start: number): void {
|
|
320
|
+
const target = targets[start];
|
|
321
|
+
if (!target) return;
|
|
322
|
+
const bucketKey = axis === 'x' ? target.x : target.y;
|
|
323
|
+
for (let index = start; index >= 0; index--) {
|
|
324
|
+
const candidate = targets[index];
|
|
325
|
+
if (!candidate) continue;
|
|
326
|
+
const candidateKey = axis === 'x' ? candidate.x : candidate.y;
|
|
327
|
+
if (candidateKey !== bucketKey) break;
|
|
328
|
+
candidates.push(candidate);
|
|
329
|
+
}
|
|
330
|
+
for (let index = start + 1; index < targets.length; index++) {
|
|
331
|
+
const candidate = targets[index];
|
|
332
|
+
if (!candidate) continue;
|
|
333
|
+
const candidateKey = axis === 'x' ? candidate.x : candidate.y;
|
|
334
|
+
if (candidateKey !== bucketKey) break;
|
|
335
|
+
candidates.push(candidate);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
addBucket(low);
|
|
340
|
+
if (low > 0) {
|
|
341
|
+
addBucket(low - 1);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let nearest: ChartTarget | undefined;
|
|
345
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
346
|
+
for (const target of candidates) {
|
|
347
|
+
const targetDistance = Math.hypot(target.x - x, target.y - y);
|
|
348
|
+
if (targetDistance >= nearestDistance) continue;
|
|
349
|
+
nearest = target;
|
|
350
|
+
nearestDistance = targetDistance;
|
|
351
|
+
}
|
|
352
|
+
return nearest;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function legendVisible(legendPosition: ChartLegendPosition, seriesCount: number): boolean {
|
|
356
|
+
return legendPosition !== 'none' && seriesCount > 0;
|
|
357
|
+
}
|