@opendata-ai/openchart-engine 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +366 -0
- package/dist/index.js +4227 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
- package/src/__test-fixtures__/specs.ts +124 -0
- package/src/__tests__/axes.test.ts +114 -0
- package/src/__tests__/compile-chart.test.ts +337 -0
- package/src/__tests__/dimensions.test.ts +151 -0
- package/src/__tests__/legend.test.ts +113 -0
- package/src/__tests__/scales.test.ts +109 -0
- package/src/annotations/__tests__/compute.test.ts +454 -0
- package/src/annotations/compute.ts +603 -0
- package/src/charts/__tests__/registry.test.ts +110 -0
- package/src/charts/bar/__tests__/compute.test.ts +294 -0
- package/src/charts/bar/__tests__/labels.test.ts +75 -0
- package/src/charts/bar/compute.ts +205 -0
- package/src/charts/bar/index.ts +33 -0
- package/src/charts/bar/labels.ts +132 -0
- package/src/charts/column/__tests__/compute.test.ts +277 -0
- package/src/charts/column/compute.ts +282 -0
- package/src/charts/column/index.ts +33 -0
- package/src/charts/column/labels.ts +108 -0
- package/src/charts/dot/__tests__/compute.test.ts +344 -0
- package/src/charts/dot/compute.ts +257 -0
- package/src/charts/dot/index.ts +46 -0
- package/src/charts/dot/labels.ts +97 -0
- package/src/charts/line/__tests__/compute.test.ts +437 -0
- package/src/charts/line/__tests__/labels.test.ts +93 -0
- package/src/charts/line/area.ts +288 -0
- package/src/charts/line/compute.ts +177 -0
- package/src/charts/line/index.ts +68 -0
- package/src/charts/line/labels.ts +144 -0
- package/src/charts/pie/__tests__/compute.test.ts +276 -0
- package/src/charts/pie/compute.ts +234 -0
- package/src/charts/pie/index.ts +49 -0
- package/src/charts/pie/labels.ts +142 -0
- package/src/charts/registry.ts +64 -0
- package/src/charts/scatter/__tests__/compute.test.ts +304 -0
- package/src/charts/scatter/__tests__/trendline.test.ts +191 -0
- package/src/charts/scatter/compute.ts +124 -0
- package/src/charts/scatter/index.ts +41 -0
- package/src/charts/scatter/trendline.ts +100 -0
- package/src/charts/utils.ts +120 -0
- package/src/compile.ts +368 -0
- package/src/compiler/__tests__/compile.test.ts +87 -0
- package/src/compiler/__tests__/normalize.test.ts +210 -0
- package/src/compiler/__tests__/validate.test.ts +440 -0
- package/src/compiler/index.ts +47 -0
- package/src/compiler/normalize.ts +269 -0
- package/src/compiler/types.ts +148 -0
- package/src/compiler/validate.ts +581 -0
- package/src/graphs/__tests__/community.test.ts +228 -0
- package/src/graphs/__tests__/compile-graph.test.ts +315 -0
- package/src/graphs/__tests__/encoding.test.ts +314 -0
- package/src/graphs/community.ts +92 -0
- package/src/graphs/compile-graph.ts +291 -0
- package/src/graphs/encoding.ts +302 -0
- package/src/graphs/types.ts +98 -0
- package/src/index.ts +74 -0
- package/src/layout/axes.ts +194 -0
- package/src/layout/dimensions.ts +199 -0
- package/src/layout/gridlines.ts +84 -0
- package/src/layout/scales.ts +426 -0
- package/src/legend/compute.ts +186 -0
- package/src/tables/__tests__/bar-column.test.ts +147 -0
- package/src/tables/__tests__/category-colors.test.ts +153 -0
- package/src/tables/__tests__/compile-table.test.ts +208 -0
- package/src/tables/__tests__/format-cells.test.ts +126 -0
- package/src/tables/__tests__/heatmap.test.ts +124 -0
- package/src/tables/__tests__/pagination.test.ts +78 -0
- package/src/tables/__tests__/search.test.ts +94 -0
- package/src/tables/__tests__/sort.test.ts +107 -0
- package/src/tables/__tests__/sparkline.test.ts +122 -0
- package/src/tables/bar-column.ts +94 -0
- package/src/tables/category-colors.ts +67 -0
- package/src/tables/compile-table.ts +420 -0
- package/src/tables/format-cells.ts +110 -0
- package/src/tables/heatmap.ts +121 -0
- package/src/tables/pagination.ts +46 -0
- package/src/tables/search.ts +66 -0
- package/src/tables/sort.ts +69 -0
- package/src/tables/sparkline.ts +113 -0
- package/src/tables/utils.ts +16 -0
- package/src/tooltips/__tests__/compute.test.ts +328 -0
- package/src/tooltips/compute.ts +231 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart renderer registry.
|
|
3
|
+
*
|
|
4
|
+
* Each chart type (line, bar, column, scatter, pie, donut, dot) registers
|
|
5
|
+
* a renderer that produces marks from normalized specs and resolved scales.
|
|
6
|
+
* The registry pattern decouples chart-type logic from the compile pipeline.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { LayoutStrategy, Mark, Rect, ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
10
|
+
|
|
11
|
+
import type { NormalizedChartSpec } from '../compiler/types';
|
|
12
|
+
import type { ResolvedScales } from '../layout/scales';
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Types
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A chart renderer function.
|
|
20
|
+
*
|
|
21
|
+
* Takes a normalized spec, resolved scales, chart area, layout strategy,
|
|
22
|
+
* and the resolved theme for theme-aware styling (e.g. label colors).
|
|
23
|
+
* Returns an array of marks to render.
|
|
24
|
+
*/
|
|
25
|
+
export type ChartRenderer = (
|
|
26
|
+
spec: NormalizedChartSpec,
|
|
27
|
+
scales: ResolvedScales,
|
|
28
|
+
chartArea: Rect,
|
|
29
|
+
strategy: LayoutStrategy,
|
|
30
|
+
theme: ResolvedTheme,
|
|
31
|
+
) => Mark[];
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Registry
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
const renderers = new Map<string, ChartRenderer>();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Register a chart renderer for a specific chart type.
|
|
41
|
+
*
|
|
42
|
+
* @param type - Chart type string (e.g. "line", "bar").
|
|
43
|
+
* @param renderer - The renderer function.
|
|
44
|
+
*/
|
|
45
|
+
export function registerChartRenderer(type: string, renderer: ChartRenderer): void {
|
|
46
|
+
renderers.set(type, renderer);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the registered chart renderer for a type.
|
|
51
|
+
*
|
|
52
|
+
* @param type - Chart type string.
|
|
53
|
+
* @returns The renderer, or undefined if no renderer is registered.
|
|
54
|
+
*/
|
|
55
|
+
export function getChartRenderer(type: string): ChartRenderer | undefined {
|
|
56
|
+
return renderers.get(type);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Clear all registered renderers. Useful for testing.
|
|
61
|
+
*/
|
|
62
|
+
export function clearRenderers(): void {
|
|
63
|
+
renderers.clear();
|
|
64
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import type { LayoutStrategy, PointMark, Rect } from '@opendata-ai/openchart-core';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import type { NormalizedChartSpec } from '../../../compiler/types';
|
|
4
|
+
import { computeScales } from '../../../layout/scales';
|
|
5
|
+
import { computeScatterMarks } from '../compute';
|
|
6
|
+
import { computeTrendLine } from '../trendline';
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Shared fixtures
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
const chartArea: Rect = { x: 50, y: 20, width: 500, height: 300 };
|
|
13
|
+
|
|
14
|
+
const fullStrategy: LayoutStrategy = {
|
|
15
|
+
labelMode: 'all',
|
|
16
|
+
legendPosition: 'right',
|
|
17
|
+
annotationPosition: 'inline',
|
|
18
|
+
axisLabelDensity: 'full',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function makeBasicScatterSpec(): NormalizedChartSpec {
|
|
22
|
+
return {
|
|
23
|
+
type: 'scatter',
|
|
24
|
+
data: [
|
|
25
|
+
{ x: 10, y: 20 },
|
|
26
|
+
{ x: 30, y: 50 },
|
|
27
|
+
{ x: 50, y: 40 },
|
|
28
|
+
{ x: 70, y: 80 },
|
|
29
|
+
{ x: 90, y: 60 },
|
|
30
|
+
],
|
|
31
|
+
encoding: {
|
|
32
|
+
x: { field: 'x', type: 'quantitative' },
|
|
33
|
+
y: { field: 'y', type: 'quantitative' },
|
|
34
|
+
},
|
|
35
|
+
chrome: {},
|
|
36
|
+
annotations: [],
|
|
37
|
+
responsive: true,
|
|
38
|
+
theme: {},
|
|
39
|
+
darkMode: 'off',
|
|
40
|
+
labels: { density: 'auto', format: '' },
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function makeBubbleSpec(): NormalizedChartSpec {
|
|
45
|
+
return {
|
|
46
|
+
type: 'scatter',
|
|
47
|
+
data: [
|
|
48
|
+
{ gdp: 10, life: 60, population: 1000 },
|
|
49
|
+
{ gdp: 30, life: 70, population: 5000 },
|
|
50
|
+
{ gdp: 50, life: 75, population: 300 },
|
|
51
|
+
{ gdp: 70, life: 80, population: 8000 },
|
|
52
|
+
],
|
|
53
|
+
encoding: {
|
|
54
|
+
x: { field: 'gdp', type: 'quantitative' },
|
|
55
|
+
y: { field: 'life', type: 'quantitative' },
|
|
56
|
+
size: { field: 'population', type: 'quantitative' },
|
|
57
|
+
},
|
|
58
|
+
chrome: {},
|
|
59
|
+
annotations: [],
|
|
60
|
+
responsive: true,
|
|
61
|
+
theme: {},
|
|
62
|
+
darkMode: 'off',
|
|
63
|
+
labels: { density: 'auto', format: '' },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function makeColoredScatterSpec(): NormalizedChartSpec {
|
|
68
|
+
return {
|
|
69
|
+
type: 'scatter',
|
|
70
|
+
data: [
|
|
71
|
+
{ x: 10, y: 20, group: 'A' },
|
|
72
|
+
{ x: 30, y: 50, group: 'A' },
|
|
73
|
+
{ x: 50, y: 40, group: 'B' },
|
|
74
|
+
{ x: 70, y: 80, group: 'B' },
|
|
75
|
+
{ x: 90, y: 60, group: 'C' },
|
|
76
|
+
],
|
|
77
|
+
encoding: {
|
|
78
|
+
x: { field: 'x', type: 'quantitative' },
|
|
79
|
+
y: { field: 'y', type: 'quantitative' },
|
|
80
|
+
color: { field: 'group', type: 'nominal' },
|
|
81
|
+
},
|
|
82
|
+
chrome: {},
|
|
83
|
+
annotations: [],
|
|
84
|
+
responsive: true,
|
|
85
|
+
theme: {},
|
|
86
|
+
darkMode: 'off',
|
|
87
|
+
labels: { density: 'auto', format: '' },
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// computeScatterMarks tests
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
describe('computeScatterMarks', () => {
|
|
96
|
+
describe('basic scatter', () => {
|
|
97
|
+
it('produces one PointMark per data row', () => {
|
|
98
|
+
const spec = makeBasicScatterSpec();
|
|
99
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
100
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
101
|
+
|
|
102
|
+
expect(marks).toHaveLength(5);
|
|
103
|
+
expect(marks.every((m) => m.type === 'point')).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('point positions are within chart area bounds', () => {
|
|
107
|
+
const spec = makeBasicScatterSpec();
|
|
108
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
109
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
110
|
+
|
|
111
|
+
for (const mark of marks) {
|
|
112
|
+
expect(mark.cx).toBeGreaterThanOrEqual(chartArea.x);
|
|
113
|
+
expect(mark.cx).toBeLessThanOrEqual(chartArea.x + chartArea.width);
|
|
114
|
+
expect(mark.cy).toBeGreaterThanOrEqual(chartArea.y);
|
|
115
|
+
expect(mark.cy).toBeLessThanOrEqual(chartArea.y + chartArea.height);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('points have default radius when no size encoding', () => {
|
|
120
|
+
const spec = makeBasicScatterSpec();
|
|
121
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
122
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
123
|
+
|
|
124
|
+
for (const mark of marks) {
|
|
125
|
+
expect(mark.r).toBe(5);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('points have partial fill opacity for overlap visibility', () => {
|
|
130
|
+
const spec = makeBasicScatterSpec();
|
|
131
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
132
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
133
|
+
|
|
134
|
+
for (const mark of marks) {
|
|
135
|
+
expect(mark.fillOpacity).toBeGreaterThan(0);
|
|
136
|
+
expect(mark.fillOpacity).toBeLessThan(1);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('each point has an aria label with field values', () => {
|
|
141
|
+
const spec = makeBasicScatterSpec();
|
|
142
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
143
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
144
|
+
|
|
145
|
+
expect(marks[0].aria.label).toContain('x=10');
|
|
146
|
+
expect(marks[0].aria.label).toContain('y=20');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe('bubble variant (size encoding)', () => {
|
|
151
|
+
it('points have varying radii based on size field', () => {
|
|
152
|
+
const spec = makeBubbleSpec();
|
|
153
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
154
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
155
|
+
|
|
156
|
+
const radii = marks.map((m) => m.r);
|
|
157
|
+
const uniqueRadii = new Set(radii);
|
|
158
|
+
expect(uniqueRadii.size).toBeGreaterThan(1);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('larger size values produce larger radii', () => {
|
|
162
|
+
const spec = makeBubbleSpec();
|
|
163
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
164
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
165
|
+
|
|
166
|
+
// population: 8000 should have largest radius
|
|
167
|
+
const largest = marks.find((m) => m.data.population === 8000)!;
|
|
168
|
+
const smallest = marks.find((m) => m.data.population === 300)!;
|
|
169
|
+
expect(largest.r).toBeGreaterThan(smallest.r);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('aria label includes size field', () => {
|
|
173
|
+
const spec = makeBubbleSpec();
|
|
174
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
175
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
176
|
+
|
|
177
|
+
expect(marks[0].aria.label).toContain('population=');
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('color encoding', () => {
|
|
182
|
+
it('points in different groups have different colors', () => {
|
|
183
|
+
const spec = makeColoredScatterSpec();
|
|
184
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
185
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
186
|
+
|
|
187
|
+
const groupA = marks.find((m) => m.data.group === 'A')!;
|
|
188
|
+
const groupB = marks.find((m) => m.data.group === 'B')!;
|
|
189
|
+
expect(groupA.fill).not.toBe(groupB.fill);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('points in the same group share a color', () => {
|
|
193
|
+
const spec = makeColoredScatterSpec();
|
|
194
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
195
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
196
|
+
|
|
197
|
+
const groupAMarks = marks.filter((m) => m.data.group === 'A');
|
|
198
|
+
expect(groupAMarks[0].fill).toBe(groupAMarks[1].fill);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('edge cases', () => {
|
|
203
|
+
it('returns empty array when no x encoding', () => {
|
|
204
|
+
const spec: NormalizedChartSpec = {
|
|
205
|
+
type: 'scatter',
|
|
206
|
+
data: [{ y: 10 }],
|
|
207
|
+
encoding: {
|
|
208
|
+
y: { field: 'y', type: 'quantitative' },
|
|
209
|
+
},
|
|
210
|
+
chrome: {},
|
|
211
|
+
annotations: [],
|
|
212
|
+
responsive: true,
|
|
213
|
+
theme: {},
|
|
214
|
+
darkMode: 'off',
|
|
215
|
+
labels: { density: 'auto', format: '' },
|
|
216
|
+
};
|
|
217
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
218
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
219
|
+
expect(marks).toHaveLength(0);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('skips rows with non-finite values', () => {
|
|
223
|
+
const spec: NormalizedChartSpec = {
|
|
224
|
+
type: 'scatter',
|
|
225
|
+
data: [
|
|
226
|
+
{ x: 10, y: 20 },
|
|
227
|
+
{ x: NaN, y: 30 },
|
|
228
|
+
{ x: 50, y: 40 },
|
|
229
|
+
],
|
|
230
|
+
encoding: {
|
|
231
|
+
x: { field: 'x', type: 'quantitative' },
|
|
232
|
+
y: { field: 'y', type: 'quantitative' },
|
|
233
|
+
},
|
|
234
|
+
chrome: {},
|
|
235
|
+
annotations: [],
|
|
236
|
+
responsive: true,
|
|
237
|
+
theme: {},
|
|
238
|
+
darkMode: 'off',
|
|
239
|
+
labels: { density: 'auto', format: '' },
|
|
240
|
+
};
|
|
241
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
242
|
+
const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
243
|
+
expect(marks).toHaveLength(2);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// ---------------------------------------------------------------------------
|
|
249
|
+
// computeTrendLine tests
|
|
250
|
+
// ---------------------------------------------------------------------------
|
|
251
|
+
|
|
252
|
+
describe('computeTrendLine', () => {
|
|
253
|
+
it('produces a LineMark with two points', () => {
|
|
254
|
+
const spec = makeBasicScatterSpec();
|
|
255
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
256
|
+
const points = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
257
|
+
const trendLine = computeTrendLine(points);
|
|
258
|
+
|
|
259
|
+
expect(trendLine).not.toBeNull();
|
|
260
|
+
expect(trendLine!.type).toBe('line');
|
|
261
|
+
expect(trendLine!.points).toHaveLength(2);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('trend line spans the full x-range of points', () => {
|
|
265
|
+
const spec = makeBasicScatterSpec();
|
|
266
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
267
|
+
const points = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
268
|
+
const trendLine = computeTrendLine(points);
|
|
269
|
+
|
|
270
|
+
const minCx = Math.min(...points.map((p) => p.cx));
|
|
271
|
+
const maxCx = Math.max(...points.map((p) => p.cx));
|
|
272
|
+
|
|
273
|
+
expect(trendLine!.points[0].x).toBeCloseTo(minCx, 1);
|
|
274
|
+
expect(trendLine!.points[1].x).toBeCloseTo(maxCx, 1);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('trend line is dashed', () => {
|
|
278
|
+
const spec = makeBasicScatterSpec();
|
|
279
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
280
|
+
const points = computeScatterMarks(spec, scales, chartArea, fullStrategy);
|
|
281
|
+
const trendLine = computeTrendLine(points);
|
|
282
|
+
|
|
283
|
+
expect(trendLine!.strokeDasharray).toBeTruthy();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('returns null for fewer than 2 points', () => {
|
|
287
|
+
const singlePoint: PointMark[] = [
|
|
288
|
+
{
|
|
289
|
+
type: 'point',
|
|
290
|
+
cx: 100,
|
|
291
|
+
cy: 100,
|
|
292
|
+
r: 5,
|
|
293
|
+
fill: '#000',
|
|
294
|
+
stroke: '#fff',
|
|
295
|
+
strokeWidth: 1,
|
|
296
|
+
data: {},
|
|
297
|
+
aria: { label: 'test' },
|
|
298
|
+
},
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
expect(computeTrendLine(singlePoint)).toBeNull();
|
|
302
|
+
expect(computeTrendLine([])).toBeNull();
|
|
303
|
+
});
|
|
304
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { PointMark } from '@opendata-ai/openchart-core';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { computeTrendLine } from '../trendline';
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Helpers
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/** Create a PointMark with the given center coordinates. */
|
|
10
|
+
function makePoint(cx: number, cy: number): PointMark {
|
|
11
|
+
return {
|
|
12
|
+
type: 'point',
|
|
13
|
+
cx,
|
|
14
|
+
cy,
|
|
15
|
+
r: 5,
|
|
16
|
+
fill: '#333',
|
|
17
|
+
stroke: '#fff',
|
|
18
|
+
strokeWidth: 1,
|
|
19
|
+
data: {},
|
|
20
|
+
aria: { label: `point at (${cx}, ${cy})` },
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Tests
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
describe('computeTrendLine', () => {
|
|
29
|
+
describe('linear regression computation', () => {
|
|
30
|
+
it('returns a LineMark with two endpoint coordinates', () => {
|
|
31
|
+
const points = [makePoint(100, 200), makePoint(200, 150), makePoint(300, 100)];
|
|
32
|
+
const result = computeTrendLine(points);
|
|
33
|
+
|
|
34
|
+
expect(result).not.toBeNull();
|
|
35
|
+
expect(result!.type).toBe('line');
|
|
36
|
+
expect(result!.points).toHaveLength(2);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('computes correct regression for a perfect positive slope', () => {
|
|
40
|
+
// y = 2x + 10: when x=0,y=10; x=100,y=210; x=200,y=410
|
|
41
|
+
// Note: these are pixel coordinates, not data coordinates
|
|
42
|
+
const points = [makePoint(0, 10), makePoint(100, 210), makePoint(200, 410)];
|
|
43
|
+
const result = computeTrendLine(points)!;
|
|
44
|
+
|
|
45
|
+
// Line spans from x=0 to x=200
|
|
46
|
+
expect(result.points[0].x).toBeCloseTo(0, 1);
|
|
47
|
+
expect(result.points[1].x).toBeCloseTo(200, 1);
|
|
48
|
+
|
|
49
|
+
// y at x=0 should be 10, y at x=200 should be 410
|
|
50
|
+
expect(result.points[0].y).toBeCloseTo(10, 1);
|
|
51
|
+
expect(result.points[1].y).toBeCloseTo(410, 1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('computes correct regression for a perfect negative slope', () => {
|
|
55
|
+
// y = -x + 300: when x=0,y=300; x=100,y=200; x=200,y=100
|
|
56
|
+
const points = [makePoint(0, 300), makePoint(100, 200), makePoint(200, 100)];
|
|
57
|
+
const result = computeTrendLine(points)!;
|
|
58
|
+
|
|
59
|
+
expect(result.points[0].x).toBeCloseTo(0, 1);
|
|
60
|
+
expect(result.points[0].y).toBeCloseTo(300, 1);
|
|
61
|
+
expect(result.points[1].x).toBeCloseTo(200, 1);
|
|
62
|
+
expect(result.points[1].y).toBeCloseTo(100, 1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('computes a flat line for horizontal data', () => {
|
|
66
|
+
// All y values are the same
|
|
67
|
+
const points = [makePoint(50, 150), makePoint(150, 150), makePoint(250, 150)];
|
|
68
|
+
const result = computeTrendLine(points)!;
|
|
69
|
+
|
|
70
|
+
expect(result.points[0].y).toBeCloseTo(150, 1);
|
|
71
|
+
expect(result.points[1].y).toBeCloseTo(150, 1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('spans from minimum to maximum x of input points', () => {
|
|
75
|
+
const points = [makePoint(80, 100), makePoint(250, 200), makePoint(120, 130)];
|
|
76
|
+
const result = computeTrendLine(points)!;
|
|
77
|
+
|
|
78
|
+
expect(result.points[0].x).toBeCloseTo(80, 1);
|
|
79
|
+
expect(result.points[1].x).toBeCloseTo(250, 1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('fits a noisy dataset with approximate best-fit line', () => {
|
|
83
|
+
// Roughly y = 0.5x + 50, with noise
|
|
84
|
+
const points = [
|
|
85
|
+
makePoint(0, 48),
|
|
86
|
+
makePoint(50, 76),
|
|
87
|
+
makePoint(100, 98),
|
|
88
|
+
makePoint(150, 122),
|
|
89
|
+
makePoint(200, 152),
|
|
90
|
+
];
|
|
91
|
+
const result = computeTrendLine(points)!;
|
|
92
|
+
|
|
93
|
+
// The slope should be approximately 0.5
|
|
94
|
+
const slope =
|
|
95
|
+
(result.points[1].y - result.points[0].y) / (result.points[1].x - result.points[0].x);
|
|
96
|
+
expect(slope).toBeCloseTo(0.5, 0);
|
|
97
|
+
|
|
98
|
+
// Intercept at x=0 should be approximately 50
|
|
99
|
+
expect(result.points[0].y).toBeCloseTo(50, -1); // within 10
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('visual properties', () => {
|
|
104
|
+
it('renders with a dashed stroke pattern', () => {
|
|
105
|
+
const points = [makePoint(100, 200), makePoint(300, 100)];
|
|
106
|
+
const result = computeTrendLine(points)!;
|
|
107
|
+
|
|
108
|
+
expect(result.strokeDasharray).toBe('6 4');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('has a subdued stroke color', () => {
|
|
112
|
+
const points = [makePoint(100, 200), makePoint(300, 100)];
|
|
113
|
+
const result = computeTrendLine(points)!;
|
|
114
|
+
|
|
115
|
+
expect(result.stroke).toBe('#666666');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('has a thin stroke width', () => {
|
|
119
|
+
const points = [makePoint(100, 200), makePoint(300, 100)];
|
|
120
|
+
const result = computeTrendLine(points)!;
|
|
121
|
+
|
|
122
|
+
expect(result.strokeWidth).toBe(1.5);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('has an aria label describing the trend', () => {
|
|
126
|
+
const points = [makePoint(100, 200), makePoint(300, 100)];
|
|
127
|
+
const result = computeTrendLine(points)!;
|
|
128
|
+
|
|
129
|
+
expect(result.aria.label).toContain('Trend line');
|
|
130
|
+
expect(result.aria.label).toContain('linear regression');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('edge cases', () => {
|
|
135
|
+
it('returns null for an empty array', () => {
|
|
136
|
+
expect(computeTrendLine([])).toBeNull();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('returns null for a single point', () => {
|
|
140
|
+
expect(computeTrendLine([makePoint(100, 200)])).toBeNull();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('handles exactly two points (deterministic line)', () => {
|
|
144
|
+
const points = [makePoint(100, 300), makePoint(400, 150)];
|
|
145
|
+
const result = computeTrendLine(points)!;
|
|
146
|
+
|
|
147
|
+
// With two points, the regression line passes through both
|
|
148
|
+
expect(result.points[0].x).toBeCloseTo(100, 1);
|
|
149
|
+
expect(result.points[0].y).toBeCloseTo(300, 1);
|
|
150
|
+
expect(result.points[1].x).toBeCloseTo(400, 1);
|
|
151
|
+
expect(result.points[1].y).toBeCloseTo(150, 1);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('returns null for vertical data (all same x)', () => {
|
|
155
|
+
// All points have the same x coordinate: denominator in regression is 0
|
|
156
|
+
const points = [makePoint(100, 50), makePoint(100, 150), makePoint(100, 250)];
|
|
157
|
+
const result = computeTrendLine(points);
|
|
158
|
+
|
|
159
|
+
expect(result).toBeNull();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('handles points at the same location gracefully', () => {
|
|
163
|
+
// All points are identical: zero variance in both x and y
|
|
164
|
+
const points = [makePoint(100, 100), makePoint(100, 100)];
|
|
165
|
+
const result = computeTrendLine(points);
|
|
166
|
+
|
|
167
|
+
// Denominator is zero, so regression returns null
|
|
168
|
+
expect(result).toBeNull();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('handles large coordinate values without overflow', () => {
|
|
172
|
+
const points = [makePoint(10000, 50000), makePoint(20000, 60000), makePoint(30000, 70000)];
|
|
173
|
+
const result = computeTrendLine(points)!;
|
|
174
|
+
|
|
175
|
+
expect(result.points[0].x).toBeCloseTo(10000, 0);
|
|
176
|
+
expect(result.points[1].x).toBeCloseTo(30000, 0);
|
|
177
|
+
// Perfect slope = 1
|
|
178
|
+
const slope =
|
|
179
|
+
(result.points[1].y - result.points[0].y) / (result.points[1].x - result.points[0].x);
|
|
180
|
+
expect(slope).toBeCloseTo(1, 5);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('handles negative coordinates', () => {
|
|
184
|
+
const points = [makePoint(-200, -100), makePoint(0, 0), makePoint(200, 100)];
|
|
185
|
+
const result = computeTrendLine(points)!;
|
|
186
|
+
|
|
187
|
+
expect(result.points[0].x).toBeCloseTo(-200, 1);
|
|
188
|
+
expect(result.points[1].x).toBeCloseTo(200, 1);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scatter / bubble chart mark computation.
|
|
3
|
+
*
|
|
4
|
+
* Takes a normalized chart spec with resolved scales and produces
|
|
5
|
+
* PointMark[] for rendering scatter plots. Both axes are quantitative.
|
|
6
|
+
* Optional size encoding produces area-proportional bubbles via sqrt
|
|
7
|
+
* scaling, and color encoding groups points by category.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
Encoding,
|
|
12
|
+
LayoutStrategy,
|
|
13
|
+
MarkAria,
|
|
14
|
+
PointMark,
|
|
15
|
+
Rect,
|
|
16
|
+
} from '@opendata-ai/openchart-core';
|
|
17
|
+
import { max, min } from 'd3-array';
|
|
18
|
+
import type { ScaleLinear } from 'd3-scale';
|
|
19
|
+
import { scaleSqrt } from 'd3-scale';
|
|
20
|
+
|
|
21
|
+
import type { NormalizedChartSpec } from '../../compiler/types';
|
|
22
|
+
import type { ResolvedScales } from '../../layout/scales';
|
|
23
|
+
import { getColor } from '../utils';
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Constants
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
const DEFAULT_POINT_RADIUS = 5;
|
|
30
|
+
const MIN_BUBBLE_RADIUS = 3;
|
|
31
|
+
const MAX_BUBBLE_RADIUS = 30;
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Public API
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Compute scatter/bubble marks from a normalized chart spec.
|
|
39
|
+
*
|
|
40
|
+
* Both x and y are quantitative (linear scales). Optional size encoding
|
|
41
|
+
* maps a data field to point radius using sqrt scale (area-proportional).
|
|
42
|
+
* Optional color encoding groups points by category with distinct colors.
|
|
43
|
+
*/
|
|
44
|
+
export function computeScatterMarks(
|
|
45
|
+
spec: NormalizedChartSpec,
|
|
46
|
+
scales: ResolvedScales,
|
|
47
|
+
_chartArea: Rect,
|
|
48
|
+
_strategy: LayoutStrategy,
|
|
49
|
+
): PointMark[] {
|
|
50
|
+
const encoding = spec.encoding as Encoding;
|
|
51
|
+
const xChannel = encoding.x;
|
|
52
|
+
const yChannel = encoding.y;
|
|
53
|
+
|
|
54
|
+
if (!xChannel || !yChannel || !scales.x || !scales.y) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const xScale = scales.x.scale as ScaleLinear<number, number>;
|
|
59
|
+
const yScale = scales.y.scale as ScaleLinear<number, number>;
|
|
60
|
+
|
|
61
|
+
const colorField = encoding.color?.field;
|
|
62
|
+
const sizeField = encoding.size?.field;
|
|
63
|
+
|
|
64
|
+
// Build a size scale for bubble variant
|
|
65
|
+
let sizeScale: ((v: number) => number) | undefined;
|
|
66
|
+
if (sizeField) {
|
|
67
|
+
const sizeValues = spec.data.map((d) => Number(d[sizeField])).filter((v) => Number.isFinite(v));
|
|
68
|
+
|
|
69
|
+
const sizeMin = min(sizeValues) ?? 0;
|
|
70
|
+
const sizeMax = max(sizeValues) ?? 1;
|
|
71
|
+
|
|
72
|
+
sizeScale = scaleSqrt()
|
|
73
|
+
.domain([sizeMin, sizeMax])
|
|
74
|
+
.range([MIN_BUBBLE_RADIUS, MAX_BUBBLE_RADIUS]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const marks: PointMark[] = [];
|
|
78
|
+
|
|
79
|
+
for (const row of spec.data) {
|
|
80
|
+
const xVal = Number(row[xChannel.field]);
|
|
81
|
+
const yVal = Number(row[yChannel.field]);
|
|
82
|
+
|
|
83
|
+
if (!Number.isFinite(xVal) || !Number.isFinite(yVal)) continue;
|
|
84
|
+
|
|
85
|
+
const cx = xScale(xVal);
|
|
86
|
+
const cy = yScale(yVal);
|
|
87
|
+
|
|
88
|
+
const category = colorField ? String(row[colorField] ?? '') : undefined;
|
|
89
|
+
const color = getColor(scales, category ?? '__default__');
|
|
90
|
+
|
|
91
|
+
let radius = DEFAULT_POINT_RADIUS;
|
|
92
|
+
if (sizeScale && sizeField) {
|
|
93
|
+
const sizeVal = Number(row[sizeField]);
|
|
94
|
+
if (Number.isFinite(sizeVal)) {
|
|
95
|
+
radius = sizeScale(sizeVal);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const labelParts = [`${xChannel.field}=${xVal}`, `${yChannel.field}=${yVal}`];
|
|
100
|
+
if (category) labelParts.push(`${colorField}=${category}`);
|
|
101
|
+
if (sizeField && row[sizeField] != null) {
|
|
102
|
+
labelParts.push(`${sizeField}=${row[sizeField]}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const aria: MarkAria = {
|
|
106
|
+
label: `Data point: ${labelParts.join(', ')}`,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
marks.push({
|
|
110
|
+
type: 'point',
|
|
111
|
+
cx,
|
|
112
|
+
cy,
|
|
113
|
+
r: radius,
|
|
114
|
+
fill: color,
|
|
115
|
+
stroke: '#ffffff',
|
|
116
|
+
strokeWidth: 1,
|
|
117
|
+
fillOpacity: 0.7,
|
|
118
|
+
data: row as Record<string, unknown>,
|
|
119
|
+
aria,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return marks;
|
|
124
|
+
}
|