@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,276 @@
|
|
|
1
|
+
import type { LayoutStrategy, 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 { computePieMarks } from '../compute';
|
|
6
|
+
import { computePieLabels } from '../labels';
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Shared fixtures
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
const chartArea: Rect = { x: 50, y: 20, width: 400, height: 400 };
|
|
13
|
+
|
|
14
|
+
const fullStrategy: LayoutStrategy = {
|
|
15
|
+
labelMode: 'all',
|
|
16
|
+
legendPosition: 'right',
|
|
17
|
+
annotationPosition: 'inline',
|
|
18
|
+
axisLabelDensity: 'full',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function makeBasicPieSpec(): NormalizedChartSpec {
|
|
22
|
+
return {
|
|
23
|
+
type: 'pie',
|
|
24
|
+
data: [
|
|
25
|
+
{ category: 'A', value: 40 },
|
|
26
|
+
{ category: 'B', value: 30 },
|
|
27
|
+
{ category: 'C', value: 20 },
|
|
28
|
+
{ category: 'D', value: 10 },
|
|
29
|
+
],
|
|
30
|
+
encoding: {
|
|
31
|
+
y: { field: 'value', type: 'quantitative' },
|
|
32
|
+
color: { field: 'category', type: 'nominal' },
|
|
33
|
+
},
|
|
34
|
+
chrome: {},
|
|
35
|
+
annotations: [],
|
|
36
|
+
responsive: true,
|
|
37
|
+
theme: {},
|
|
38
|
+
darkMode: 'off',
|
|
39
|
+
labels: { density: 'auto', format: '' },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function makeSmallSlicePieSpec(): NormalizedChartSpec {
|
|
44
|
+
return {
|
|
45
|
+
type: 'pie',
|
|
46
|
+
data: [
|
|
47
|
+
{ category: 'Big', value: 90 },
|
|
48
|
+
{ category: 'Medium', value: 7 },
|
|
49
|
+
{ category: 'Tiny1', value: 1 },
|
|
50
|
+
{ category: 'Tiny2', value: 1 },
|
|
51
|
+
{ category: 'Tiny3', value: 1 },
|
|
52
|
+
],
|
|
53
|
+
encoding: {
|
|
54
|
+
y: { field: 'value', type: 'quantitative' },
|
|
55
|
+
color: { field: 'category', type: 'nominal' },
|
|
56
|
+
},
|
|
57
|
+
chrome: {},
|
|
58
|
+
annotations: [],
|
|
59
|
+
responsive: true,
|
|
60
|
+
theme: {},
|
|
61
|
+
darkMode: 'off',
|
|
62
|
+
labels: { density: 'auto', format: '' },
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function makeDonutSpec(): NormalizedChartSpec {
|
|
67
|
+
return {
|
|
68
|
+
type: 'donut',
|
|
69
|
+
data: [
|
|
70
|
+
{ segment: 'Desktop', users: 55 },
|
|
71
|
+
{ segment: 'Mobile', users: 35 },
|
|
72
|
+
{ segment: 'Tablet', users: 10 },
|
|
73
|
+
],
|
|
74
|
+
encoding: {
|
|
75
|
+
y: { field: 'users', type: 'quantitative' },
|
|
76
|
+
color: { field: 'segment', type: 'nominal' },
|
|
77
|
+
},
|
|
78
|
+
chrome: {},
|
|
79
|
+
annotations: [],
|
|
80
|
+
responsive: true,
|
|
81
|
+
theme: {},
|
|
82
|
+
darkMode: 'off',
|
|
83
|
+
labels: { density: 'auto', format: '' },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// computePieMarks tests
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
describe('computePieMarks', () => {
|
|
92
|
+
describe('basic pie', () => {
|
|
93
|
+
it('produces ArcMarks for each category', () => {
|
|
94
|
+
const spec = makeBasicPieSpec();
|
|
95
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
96
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
97
|
+
|
|
98
|
+
expect(marks).toHaveLength(4);
|
|
99
|
+
expect(marks.every((m) => m.type === 'arc')).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('arc paths are non-empty strings', () => {
|
|
103
|
+
const spec = makeBasicPieSpec();
|
|
104
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
105
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
106
|
+
|
|
107
|
+
for (const mark of marks) {
|
|
108
|
+
expect(mark.path).toBeTruthy();
|
|
109
|
+
expect(mark.path.length).toBeGreaterThan(0);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('arcs are sorted largest first', () => {
|
|
114
|
+
const spec = makeBasicPieSpec();
|
|
115
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
116
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
117
|
+
|
|
118
|
+
// First arc should be largest (A: 40)
|
|
119
|
+
const firstArc = marks[0];
|
|
120
|
+
expect(firstArc.aria.label).toContain('A');
|
|
121
|
+
|
|
122
|
+
// Arc angles should be in descending order (largest angle first)
|
|
123
|
+
const angles = marks.map((m) => m.endAngle - m.startAngle);
|
|
124
|
+
for (let i = 0; i < angles.length - 1; i++) {
|
|
125
|
+
expect(angles[i]).toBeGreaterThanOrEqual(angles[i + 1] - 0.02); // pad angle tolerance
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('pie has zero inner radius', () => {
|
|
130
|
+
const spec = makeBasicPieSpec();
|
|
131
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
132
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
133
|
+
|
|
134
|
+
for (const mark of marks) {
|
|
135
|
+
expect(mark.innerRadius).toBe(0);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('arcs have white stroke for separation', () => {
|
|
140
|
+
const spec = makeBasicPieSpec();
|
|
141
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
142
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
143
|
+
|
|
144
|
+
for (const mark of marks) {
|
|
145
|
+
expect(mark.stroke).toBe('#ffffff');
|
|
146
|
+
expect(mark.strokeWidth).toBeGreaterThan(0);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('each arc has aria label with value and percentage', () => {
|
|
151
|
+
const spec = makeBasicPieSpec();
|
|
152
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
153
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
154
|
+
|
|
155
|
+
expect(marks[0].aria.label).toContain('40');
|
|
156
|
+
expect(marks[0].aria.label).toContain('%');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('each arc has a centroid point for label positioning', () => {
|
|
160
|
+
const spec = makeBasicPieSpec();
|
|
161
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
162
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
163
|
+
|
|
164
|
+
for (const mark of marks) {
|
|
165
|
+
expect(mark.centroid).toBeDefined();
|
|
166
|
+
expect(typeof mark.centroid.x).toBe('number');
|
|
167
|
+
expect(typeof mark.centroid.y).toBe('number');
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('small-slice grouping', () => {
|
|
173
|
+
it('groups slices below threshold into "Other"', () => {
|
|
174
|
+
const spec = makeSmallSlicePieSpec();
|
|
175
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
176
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
177
|
+
|
|
178
|
+
// Tiny1, Tiny2, Tiny3 are each 1% (< 3% threshold) -> grouped into "Other"
|
|
179
|
+
const labels = marks.map((m) => m.aria.label);
|
|
180
|
+
const otherSlice = labels.find((l) => l.includes('Other'));
|
|
181
|
+
expect(otherSlice).toBeTruthy();
|
|
182
|
+
|
|
183
|
+
// Should be 3 marks: Big, Medium, Other
|
|
184
|
+
expect(marks).toHaveLength(3);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe('donut variant', () => {
|
|
189
|
+
it('donut has positive inner radius', () => {
|
|
190
|
+
const spec = makeDonutSpec();
|
|
191
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
192
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, true);
|
|
193
|
+
|
|
194
|
+
for (const mark of marks) {
|
|
195
|
+
expect(mark.innerRadius).toBeGreaterThan(0);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('donut inner radius is about 60% of outer radius', () => {
|
|
200
|
+
const spec = makeDonutSpec();
|
|
201
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
202
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, true);
|
|
203
|
+
|
|
204
|
+
const ratio = marks[0].innerRadius / marks[0].outerRadius;
|
|
205
|
+
expect(ratio).toBeCloseTo(0.6, 1);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('edge cases', () => {
|
|
210
|
+
it('returns empty array when no value encoding', () => {
|
|
211
|
+
const spec: NormalizedChartSpec = {
|
|
212
|
+
type: 'pie',
|
|
213
|
+
data: [{ category: 'A' }],
|
|
214
|
+
encoding: {
|
|
215
|
+
color: { field: 'category', type: 'nominal' },
|
|
216
|
+
},
|
|
217
|
+
chrome: {},
|
|
218
|
+
annotations: [],
|
|
219
|
+
responsive: true,
|
|
220
|
+
theme: {},
|
|
221
|
+
darkMode: 'off',
|
|
222
|
+
labels: { density: 'auto', format: '' },
|
|
223
|
+
};
|
|
224
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
225
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
226
|
+
expect(marks).toHaveLength(0);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('returns empty array for empty data', () => {
|
|
230
|
+
const spec: NormalizedChartSpec = {
|
|
231
|
+
type: 'pie',
|
|
232
|
+
data: [],
|
|
233
|
+
encoding: {
|
|
234
|
+
y: { field: 'value', type: 'quantitative' },
|
|
235
|
+
color: { field: 'category', type: 'nominal' },
|
|
236
|
+
},
|
|
237
|
+
chrome: {},
|
|
238
|
+
annotations: [],
|
|
239
|
+
responsive: true,
|
|
240
|
+
theme: {},
|
|
241
|
+
darkMode: 'off',
|
|
242
|
+
labels: { density: 'auto', format: '' },
|
|
243
|
+
};
|
|
244
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
245
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
246
|
+
expect(marks).toHaveLength(0);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// computePieLabels tests
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
describe('computePieLabels', () => {
|
|
256
|
+
it('produces labels for each arc mark', () => {
|
|
257
|
+
const spec = makeBasicPieSpec();
|
|
258
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
259
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
260
|
+
const labels = computePieLabels(marks, chartArea);
|
|
261
|
+
|
|
262
|
+
expect(labels).toHaveLength(marks.length);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('labels have connector lines to centroids', () => {
|
|
266
|
+
const spec = makeBasicPieSpec();
|
|
267
|
+
const scales = computeScales(spec, chartArea, spec.data);
|
|
268
|
+
const marks = computePieMarks(spec, scales, chartArea, fullStrategy, false);
|
|
269
|
+
const labels = computePieLabels(marks, chartArea);
|
|
270
|
+
|
|
271
|
+
const visibleLabels = labels.filter((l) => l.visible);
|
|
272
|
+
for (const label of visibleLabels) {
|
|
273
|
+
expect(label.connector).toBeDefined();
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pie / donut chart mark computation.
|
|
3
|
+
*
|
|
4
|
+
* Uses d3.pie() for angle calculation and d3.arc() for SVG path
|
|
5
|
+
* generation. Supports sorting by value (largest first), small-slice
|
|
6
|
+
* grouping into "Other", and donut variant with inner radius.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
ArcMark,
|
|
11
|
+
DataRow,
|
|
12
|
+
Encoding,
|
|
13
|
+
LayoutStrategy,
|
|
14
|
+
MarkAria,
|
|
15
|
+
Rect,
|
|
16
|
+
} from '@opendata-ai/openchart-core';
|
|
17
|
+
import type { PieArcDatum } from 'd3-shape';
|
|
18
|
+
import { arc as d3Arc, pie as d3Pie } from 'd3-shape';
|
|
19
|
+
|
|
20
|
+
import type { NormalizedChartSpec } from '../../compiler/types';
|
|
21
|
+
import type { ResolvedScales } from '../../layout/scales';
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Constants
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
/** Slices smaller than this fraction are grouped into "Other". */
|
|
28
|
+
const SMALL_SLICE_THRESHOLD = 0.03;
|
|
29
|
+
|
|
30
|
+
/** Default color palette when no color scale is available. */
|
|
31
|
+
const DEFAULT_PALETTE = [
|
|
32
|
+
'#1b7fa3',
|
|
33
|
+
'#c44e52',
|
|
34
|
+
'#6a9f58',
|
|
35
|
+
'#d47215',
|
|
36
|
+
'#507e79',
|
|
37
|
+
'#9a6a8d',
|
|
38
|
+
'#c4636b',
|
|
39
|
+
'#9c755f',
|
|
40
|
+
'#a88f22',
|
|
41
|
+
'#858078',
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// Types
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
interface SliceData {
|
|
49
|
+
label: string;
|
|
50
|
+
value: number;
|
|
51
|
+
originalRow: DataRow;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Helpers
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
/** Group small slices (< threshold) into an "Other" category. */
|
|
59
|
+
function groupSmallSlices(slices: SliceData[], threshold: number): SliceData[] {
|
|
60
|
+
const total = slices.reduce((sum, s) => sum + s.value, 0);
|
|
61
|
+
if (total === 0) return slices;
|
|
62
|
+
|
|
63
|
+
const big: SliceData[] = [];
|
|
64
|
+
let otherValue = 0;
|
|
65
|
+
|
|
66
|
+
for (const slice of slices) {
|
|
67
|
+
if (slice.value / total < threshold) {
|
|
68
|
+
otherValue += slice.value;
|
|
69
|
+
} else {
|
|
70
|
+
big.push(slice);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (otherValue > 0) {
|
|
75
|
+
big.push({
|
|
76
|
+
label: 'Other',
|
|
77
|
+
value: otherValue,
|
|
78
|
+
originalRow: { label: 'Other', value: otherValue },
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return big;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Public API
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Compute pie or donut arc marks from a normalized chart spec.
|
|
91
|
+
*
|
|
92
|
+
* Extracts category and value from the encoding channels. Categories
|
|
93
|
+
* come from the color field, values from the quantitative y (or x) field.
|
|
94
|
+
* Slices are sorted largest first. Small slices are grouped into "Other".
|
|
95
|
+
*
|
|
96
|
+
* @param isDonut - When true, creates a donut with inner radius at 60% of outer.
|
|
97
|
+
*/
|
|
98
|
+
export function computePieMarks(
|
|
99
|
+
spec: NormalizedChartSpec,
|
|
100
|
+
scales: ResolvedScales,
|
|
101
|
+
chartArea: Rect,
|
|
102
|
+
_strategy: LayoutStrategy,
|
|
103
|
+
isDonut = false,
|
|
104
|
+
): ArcMark[] {
|
|
105
|
+
const encoding = spec.encoding as Encoding;
|
|
106
|
+
|
|
107
|
+
// For pie/donut charts, we need a value field (typically y or x) and
|
|
108
|
+
// a category field (typically color). The value field provides the slice sizes.
|
|
109
|
+
const valueChannel = encoding.y ?? encoding.x;
|
|
110
|
+
const categoryField = encoding.color?.field;
|
|
111
|
+
|
|
112
|
+
if (!valueChannel) return [];
|
|
113
|
+
|
|
114
|
+
// Build slices from data
|
|
115
|
+
let slices: SliceData[] = [];
|
|
116
|
+
|
|
117
|
+
if (categoryField) {
|
|
118
|
+
// Aggregate by category
|
|
119
|
+
const categoryTotals = new Map<string, number>();
|
|
120
|
+
const categoryRows = new Map<string, DataRow>();
|
|
121
|
+
|
|
122
|
+
for (const row of spec.data) {
|
|
123
|
+
const cat = String(row[categoryField] ?? '');
|
|
124
|
+
const val = Number(row[valueChannel.field] ?? 0);
|
|
125
|
+
if (!Number.isFinite(val) || val < 0) continue;
|
|
126
|
+
|
|
127
|
+
categoryTotals.set(cat, (categoryTotals.get(cat) ?? 0) + val);
|
|
128
|
+
if (!categoryRows.has(cat)) {
|
|
129
|
+
categoryRows.set(cat, row);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (const [label, value] of categoryTotals) {
|
|
134
|
+
slices.push({
|
|
135
|
+
label,
|
|
136
|
+
value,
|
|
137
|
+
originalRow: categoryRows.get(label) ?? {
|
|
138
|
+
[categoryField]: label,
|
|
139
|
+
[valueChannel.field]: value,
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
// Each data row is a slice. Use a label field if present, or index.
|
|
145
|
+
for (let i = 0; i < spec.data.length; i++) {
|
|
146
|
+
const row = spec.data[i];
|
|
147
|
+
const val = Number(row[valueChannel.field] ?? 0);
|
|
148
|
+
if (!Number.isFinite(val) || val < 0) continue;
|
|
149
|
+
|
|
150
|
+
// Try common label fields
|
|
151
|
+
const label = String(row.label ?? row.name ?? row.category ?? `Slice ${i + 1}`);
|
|
152
|
+
|
|
153
|
+
slices.push({ label, value: val, originalRow: row });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (slices.length === 0) return [];
|
|
158
|
+
|
|
159
|
+
// Sort by value descending (largest first)
|
|
160
|
+
slices.sort((a, b) => b.value - a.value);
|
|
161
|
+
|
|
162
|
+
// Group small slices into "Other"
|
|
163
|
+
slices = groupSmallSlices(slices, SMALL_SLICE_THRESHOLD);
|
|
164
|
+
|
|
165
|
+
// Compute pie layout
|
|
166
|
+
const pieGenerator = d3Pie<SliceData>()
|
|
167
|
+
.value((d) => d.value)
|
|
168
|
+
.sort(null) // Already sorted
|
|
169
|
+
.padAngle(0.01);
|
|
170
|
+
|
|
171
|
+
const arcs = pieGenerator(slices);
|
|
172
|
+
|
|
173
|
+
// Compute arc dimensions
|
|
174
|
+
const centerX = chartArea.x + chartArea.width / 2;
|
|
175
|
+
const centerY = chartArea.y + chartArea.height / 2;
|
|
176
|
+
const outerRadius = (Math.min(chartArea.width, chartArea.height) / 2) * 0.85;
|
|
177
|
+
const innerRadius = isDonut ? outerRadius * 0.6 : 0;
|
|
178
|
+
|
|
179
|
+
const arcGenerator = d3Arc<PieArcDatum<SliceData>>()
|
|
180
|
+
.innerRadius(innerRadius)
|
|
181
|
+
.outerRadius(outerRadius);
|
|
182
|
+
|
|
183
|
+
// Build arc marks
|
|
184
|
+
const marks: ArcMark[] = [];
|
|
185
|
+
const center = { x: centerX, y: centerY };
|
|
186
|
+
const total = slices.reduce((sum, s) => sum + s.value, 0);
|
|
187
|
+
|
|
188
|
+
for (let i = 0; i < arcs.length; i++) {
|
|
189
|
+
const arcDatum = arcs[i];
|
|
190
|
+
const slice = arcDatum.data;
|
|
191
|
+
|
|
192
|
+
// Get color from scale or default palette
|
|
193
|
+
let color: string;
|
|
194
|
+
if (scales.color && categoryField) {
|
|
195
|
+
const colorScale = scales.color.scale as (v: string) => string;
|
|
196
|
+
color = colorScale(slice.label);
|
|
197
|
+
} else {
|
|
198
|
+
color = DEFAULT_PALETTE[i % DEFAULT_PALETTE.length];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Generate SVG path (relative to 0,0; renderer wraps in translate)
|
|
202
|
+
const path = arcGenerator(arcDatum) ?? '';
|
|
203
|
+
|
|
204
|
+
// Compute centroid (for label positioning), offset to chart center
|
|
205
|
+
const centroidResult = arcGenerator.centroid(arcDatum);
|
|
206
|
+
|
|
207
|
+
const percentage = total > 0 ? ((slice.value / total) * 100).toFixed(1) : '0';
|
|
208
|
+
|
|
209
|
+
const aria: MarkAria = {
|
|
210
|
+
label: `${slice.label}: ${slice.value} (${percentage}%)`,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
marks.push({
|
|
214
|
+
type: 'arc',
|
|
215
|
+
path,
|
|
216
|
+
centroid: {
|
|
217
|
+
x: centroidResult[0] + centerX,
|
|
218
|
+
y: centroidResult[1] + centerY,
|
|
219
|
+
},
|
|
220
|
+
center,
|
|
221
|
+
innerRadius,
|
|
222
|
+
outerRadius,
|
|
223
|
+
startAngle: arcDatum.startAngle,
|
|
224
|
+
endAngle: arcDatum.endAngle,
|
|
225
|
+
fill: color,
|
|
226
|
+
stroke: '#ffffff',
|
|
227
|
+
strokeWidth: 2,
|
|
228
|
+
data: slice.originalRow as Record<string, unknown>,
|
|
229
|
+
aria,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return marks;
|
|
234
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pie and donut chart module.
|
|
3
|
+
*
|
|
4
|
+
* Exports pie and donut chart renderers and computation functions.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Mark } from '@opendata-ai/openchart-core';
|
|
8
|
+
import type { ChartRenderer } from '../registry';
|
|
9
|
+
import { computePieMarks } from './compute';
|
|
10
|
+
import { computePieLabels } from './labels';
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Pie chart renderer
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
export const pieRenderer: ChartRenderer = (spec, scales, chartArea, strategy, theme) => {
|
|
17
|
+
const marks = computePieMarks(spec, scales, chartArea, strategy, false);
|
|
18
|
+
|
|
19
|
+
// Compute and attach labels (respects spec.labels.density)
|
|
20
|
+
const labels = computePieLabels(marks, chartArea, spec.labels.density, theme.colors.text);
|
|
21
|
+
for (let i = 0; i < marks.length && i < labels.length; i++) {
|
|
22
|
+
marks[i].label = labels[i];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return marks as Mark[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Donut chart renderer
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
export const donutRenderer: ChartRenderer = (spec, scales, chartArea, strategy, theme) => {
|
|
33
|
+
const marks = computePieMarks(spec, scales, chartArea, strategy, true);
|
|
34
|
+
|
|
35
|
+
// Compute and attach labels (respects spec.labels.density)
|
|
36
|
+
const labels = computePieLabels(marks, chartArea, spec.labels.density, theme.colors.text);
|
|
37
|
+
for (let i = 0; i < marks.length && i < labels.length; i++) {
|
|
38
|
+
marks[i].label = labels[i];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return marks as Mark[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// Public exports
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
export { computePieMarks } from './compute';
|
|
49
|
+
export { computePieLabels } from './labels';
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pie/donut chart label computation.
|
|
3
|
+
*
|
|
4
|
+
* Produces leader-line labels positioned outside each arc slice.
|
|
5
|
+
* Labels are placed at the midpoint of each arc's angle, extended
|
|
6
|
+
* outward from the centroid. Collision detection resolves overlaps.
|
|
7
|
+
*
|
|
8
|
+
* Respects the spec's label density setting:
|
|
9
|
+
* - 'all': show every label, skip collision detection
|
|
10
|
+
* - 'auto': existing behavior (collision detection)
|
|
11
|
+
* - 'endpoints': first and last slices only
|
|
12
|
+
* - 'none': return empty array
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
ArcMark,
|
|
17
|
+
LabelCandidate,
|
|
18
|
+
LabelDensity,
|
|
19
|
+
Rect,
|
|
20
|
+
ResolvedLabel,
|
|
21
|
+
} from '@opendata-ai/openchart-core';
|
|
22
|
+
import { estimateTextWidth, resolveCollisions } from '@opendata-ai/openchart-core';
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Constants
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
const LABEL_FONT_SIZE = 10;
|
|
29
|
+
const LABEL_FONT_WEIGHT = 500;
|
|
30
|
+
const LEADER_LINE_OFFSET = 12;
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Public API
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Compute leader-line labels for pie/donut arc marks.
|
|
38
|
+
*
|
|
39
|
+
* Each label is positioned outward from the arc centroid with a connector
|
|
40
|
+
* line from the centroid to the label. Labels go through collision
|
|
41
|
+
* detection to avoid overlap.
|
|
42
|
+
*/
|
|
43
|
+
export function computePieLabels(
|
|
44
|
+
marks: ArcMark[],
|
|
45
|
+
_chartArea: Rect,
|
|
46
|
+
density: LabelDensity = 'auto',
|
|
47
|
+
_textFill = '#333333',
|
|
48
|
+
): ResolvedLabel[] {
|
|
49
|
+
if (marks.length === 0) return [];
|
|
50
|
+
|
|
51
|
+
// 'none': no labels at all
|
|
52
|
+
if (density === 'none') return [];
|
|
53
|
+
|
|
54
|
+
// Get the pie center from the first mark's center property
|
|
55
|
+
const centerX = marks[0].center.x;
|
|
56
|
+
const centerY = marks[0].center.y;
|
|
57
|
+
|
|
58
|
+
// Filter marks for 'endpoints' density
|
|
59
|
+
const targetMarks =
|
|
60
|
+
density === 'endpoints' && marks.length > 1 ? [marks[0], marks[marks.length - 1]] : marks;
|
|
61
|
+
|
|
62
|
+
const candidates: LabelCandidate[] = [];
|
|
63
|
+
const targetMarkIndices: number[] = [];
|
|
64
|
+
|
|
65
|
+
for (let mi = 0; mi < targetMarks.length; mi++) {
|
|
66
|
+
const mark = targetMarks[mi];
|
|
67
|
+
// Extract the label text (category name) from the aria label.
|
|
68
|
+
// Format is "Category: value (percent%)". Split on the first colon
|
|
69
|
+
// to handle category names that might contain colons.
|
|
70
|
+
const ariaLabel = mark.aria.label;
|
|
71
|
+
const firstColon = ariaLabel.indexOf(':');
|
|
72
|
+
const labelText = firstColon >= 0 ? ariaLabel.slice(0, firstColon).trim() : '';
|
|
73
|
+
if (!labelText) continue;
|
|
74
|
+
|
|
75
|
+
const textWidth = estimateTextWidth(labelText, LABEL_FONT_SIZE, LABEL_FONT_WEIGHT);
|
|
76
|
+
const textHeight = LABEL_FONT_SIZE * 1.2;
|
|
77
|
+
|
|
78
|
+
// Position label outward from centroid
|
|
79
|
+
const midAngle = (mark.startAngle + mark.endAngle) / 2;
|
|
80
|
+
const labelRadius = mark.outerRadius + LEADER_LINE_OFFSET;
|
|
81
|
+
|
|
82
|
+
const labelX = centerX + Math.sin(midAngle) * labelRadius;
|
|
83
|
+
const labelY = centerY - Math.cos(midAngle) * labelRadius;
|
|
84
|
+
|
|
85
|
+
// Determine text anchor based on which side of the pie the label is on
|
|
86
|
+
const isRight = Math.sin(midAngle) > 0;
|
|
87
|
+
|
|
88
|
+
candidates.push({
|
|
89
|
+
text: labelText,
|
|
90
|
+
anchorX: isRight ? labelX : labelX - textWidth,
|
|
91
|
+
anchorY: labelY - textHeight / 2,
|
|
92
|
+
width: textWidth,
|
|
93
|
+
height: textHeight,
|
|
94
|
+
priority: 'data',
|
|
95
|
+
style: {
|
|
96
|
+
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
97
|
+
fontSize: LABEL_FONT_SIZE,
|
|
98
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
99
|
+
fill: _textFill,
|
|
100
|
+
lineHeight: 1.2,
|
|
101
|
+
textAnchor: isRight ? 'start' : 'end',
|
|
102
|
+
dominantBaseline: 'central',
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
targetMarkIndices.push(mi);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (candidates.length === 0) return [];
|
|
110
|
+
|
|
111
|
+
// 'all': skip collision detection, mark everything visible
|
|
112
|
+
let resolved: ResolvedLabel[];
|
|
113
|
+
if (density === 'all') {
|
|
114
|
+
resolved = candidates.map((c) => ({
|
|
115
|
+
text: c.text,
|
|
116
|
+
x: c.anchorX,
|
|
117
|
+
y: c.anchorY,
|
|
118
|
+
style: c.style,
|
|
119
|
+
visible: true,
|
|
120
|
+
}));
|
|
121
|
+
} else {
|
|
122
|
+
// Run collision detection
|
|
123
|
+
resolved = resolveCollisions(candidates);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Add connector lines from centroid to label
|
|
127
|
+
for (let i = 0; i < resolved.length && i < targetMarks.length; i++) {
|
|
128
|
+
const label = resolved[i];
|
|
129
|
+
const mark = targetMarks[i];
|
|
130
|
+
|
|
131
|
+
if (label.visible) {
|
|
132
|
+
label.connector = {
|
|
133
|
+
from: { x: label.x, y: label.y },
|
|
134
|
+
to: { x: mark.centroid.x, y: mark.centroid.y },
|
|
135
|
+
stroke: _textFill,
|
|
136
|
+
style: 'straight',
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return resolved;
|
|
142
|
+
}
|