@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.
Files changed (85) hide show
  1. package/dist/index.d.ts +366 -0
  2. package/dist/index.js +4227 -0
  3. package/dist/index.js.map +1 -0
  4. package/package.json +62 -0
  5. package/src/__test-fixtures__/specs.ts +124 -0
  6. package/src/__tests__/axes.test.ts +114 -0
  7. package/src/__tests__/compile-chart.test.ts +337 -0
  8. package/src/__tests__/dimensions.test.ts +151 -0
  9. package/src/__tests__/legend.test.ts +113 -0
  10. package/src/__tests__/scales.test.ts +109 -0
  11. package/src/annotations/__tests__/compute.test.ts +454 -0
  12. package/src/annotations/compute.ts +603 -0
  13. package/src/charts/__tests__/registry.test.ts +110 -0
  14. package/src/charts/bar/__tests__/compute.test.ts +294 -0
  15. package/src/charts/bar/__tests__/labels.test.ts +75 -0
  16. package/src/charts/bar/compute.ts +205 -0
  17. package/src/charts/bar/index.ts +33 -0
  18. package/src/charts/bar/labels.ts +132 -0
  19. package/src/charts/column/__tests__/compute.test.ts +277 -0
  20. package/src/charts/column/compute.ts +282 -0
  21. package/src/charts/column/index.ts +33 -0
  22. package/src/charts/column/labels.ts +108 -0
  23. package/src/charts/dot/__tests__/compute.test.ts +344 -0
  24. package/src/charts/dot/compute.ts +257 -0
  25. package/src/charts/dot/index.ts +46 -0
  26. package/src/charts/dot/labels.ts +97 -0
  27. package/src/charts/line/__tests__/compute.test.ts +437 -0
  28. package/src/charts/line/__tests__/labels.test.ts +93 -0
  29. package/src/charts/line/area.ts +288 -0
  30. package/src/charts/line/compute.ts +177 -0
  31. package/src/charts/line/index.ts +68 -0
  32. package/src/charts/line/labels.ts +144 -0
  33. package/src/charts/pie/__tests__/compute.test.ts +276 -0
  34. package/src/charts/pie/compute.ts +234 -0
  35. package/src/charts/pie/index.ts +49 -0
  36. package/src/charts/pie/labels.ts +142 -0
  37. package/src/charts/registry.ts +64 -0
  38. package/src/charts/scatter/__tests__/compute.test.ts +304 -0
  39. package/src/charts/scatter/__tests__/trendline.test.ts +191 -0
  40. package/src/charts/scatter/compute.ts +124 -0
  41. package/src/charts/scatter/index.ts +41 -0
  42. package/src/charts/scatter/trendline.ts +100 -0
  43. package/src/charts/utils.ts +120 -0
  44. package/src/compile.ts +368 -0
  45. package/src/compiler/__tests__/compile.test.ts +87 -0
  46. package/src/compiler/__tests__/normalize.test.ts +210 -0
  47. package/src/compiler/__tests__/validate.test.ts +440 -0
  48. package/src/compiler/index.ts +47 -0
  49. package/src/compiler/normalize.ts +269 -0
  50. package/src/compiler/types.ts +148 -0
  51. package/src/compiler/validate.ts +581 -0
  52. package/src/graphs/__tests__/community.test.ts +228 -0
  53. package/src/graphs/__tests__/compile-graph.test.ts +315 -0
  54. package/src/graphs/__tests__/encoding.test.ts +314 -0
  55. package/src/graphs/community.ts +92 -0
  56. package/src/graphs/compile-graph.ts +291 -0
  57. package/src/graphs/encoding.ts +302 -0
  58. package/src/graphs/types.ts +98 -0
  59. package/src/index.ts +74 -0
  60. package/src/layout/axes.ts +194 -0
  61. package/src/layout/dimensions.ts +199 -0
  62. package/src/layout/gridlines.ts +84 -0
  63. package/src/layout/scales.ts +426 -0
  64. package/src/legend/compute.ts +186 -0
  65. package/src/tables/__tests__/bar-column.test.ts +147 -0
  66. package/src/tables/__tests__/category-colors.test.ts +153 -0
  67. package/src/tables/__tests__/compile-table.test.ts +208 -0
  68. package/src/tables/__tests__/format-cells.test.ts +126 -0
  69. package/src/tables/__tests__/heatmap.test.ts +124 -0
  70. package/src/tables/__tests__/pagination.test.ts +78 -0
  71. package/src/tables/__tests__/search.test.ts +94 -0
  72. package/src/tables/__tests__/sort.test.ts +107 -0
  73. package/src/tables/__tests__/sparkline.test.ts +122 -0
  74. package/src/tables/bar-column.ts +94 -0
  75. package/src/tables/category-colors.ts +67 -0
  76. package/src/tables/compile-table.ts +420 -0
  77. package/src/tables/format-cells.ts +110 -0
  78. package/src/tables/heatmap.ts +121 -0
  79. package/src/tables/pagination.ts +46 -0
  80. package/src/tables/search.ts +66 -0
  81. package/src/tables/sort.ts +69 -0
  82. package/src/tables/sparkline.ts +113 -0
  83. package/src/tables/utils.ts +16 -0
  84. package/src/tooltips/__tests__/compute.test.ts +328 -0
  85. package/src/tooltips/compute.ts +231 -0
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Dot plot / lollipop chart mark computation.
3
+ *
4
+ * Category axis (band scale) + value axis (linear scale). Produces
5
+ * PointMark[] for the dots plus RectMark[] for lollipop stems
6
+ * (thin lines from axis baseline to each dot).
7
+ *
8
+ * When a color encoding is present (multi-series), renders as a dumbbell
9
+ * chart: a connecting bar spans min-to-max per category instead of
10
+ * baseline-to-dot stems.
11
+ */
12
+
13
+ import type {
14
+ Encoding,
15
+ LayoutStrategy,
16
+ MarkAria,
17
+ PointMark,
18
+ Rect,
19
+ RectMark,
20
+ } from '@opendata-ai/openchart-core';
21
+ import type { ScaleBand, ScaleLinear } from 'd3-scale';
22
+
23
+ import type { NormalizedChartSpec } from '../../compiler/types';
24
+ import type { ResolvedScales } from '../../layout/scales';
25
+ import { getColor, groupByField } from '../utils';
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Constants
29
+ // ---------------------------------------------------------------------------
30
+
31
+ const DOT_RADIUS = 6;
32
+ const STEM_WIDTH = 2;
33
+ const STEM_COLOR = '#cccccc';
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // Public API
37
+ // ---------------------------------------------------------------------------
38
+
39
+ /**
40
+ * Compute dot plot marks from a normalized chart spec.
41
+ *
42
+ * Y axis uses a band scale for categories. X axis uses a linear scale
43
+ * for values. When no color encoding is present, each data point produces
44
+ * a lollipop stem + dot. When color is present (multi-series), renders
45
+ * connecting bars between min/max values per category (dumbbell style).
46
+ */
47
+ export function computeDotMarks(
48
+ spec: NormalizedChartSpec,
49
+ scales: ResolvedScales,
50
+ _chartArea: Rect,
51
+ _strategy: LayoutStrategy,
52
+ ): (PointMark | RectMark)[] {
53
+ const encoding = spec.encoding as Encoding;
54
+ const xChannel = encoding.x;
55
+ const yChannel = encoding.y;
56
+
57
+ if (!xChannel || !yChannel || !scales.x || !scales.y) {
58
+ return [];
59
+ }
60
+
61
+ const xScale = scales.x.scale as ScaleLinear<number, number>;
62
+ const yScale = scales.y.scale as ScaleBand<string>;
63
+
64
+ // Band scale should provide bandwidth
65
+ if (typeof yScale.bandwidth !== 'function') {
66
+ return [];
67
+ }
68
+
69
+ const bandwidth = yScale.bandwidth();
70
+ const baseline = xScale(0);
71
+ const colorField = encoding.color?.field;
72
+
73
+ // Multi-series: dumbbell chart with connecting bars
74
+ if (colorField) {
75
+ return computeDumbbellMarks(
76
+ spec.data,
77
+ xChannel.field,
78
+ yChannel.field,
79
+ colorField,
80
+ xScale,
81
+ yScale,
82
+ bandwidth,
83
+ scales,
84
+ );
85
+ }
86
+
87
+ // Single series: lollipop stems from baseline
88
+ return computeLollipopMarks(
89
+ spec.data,
90
+ xChannel.field,
91
+ yChannel.field,
92
+ xScale,
93
+ yScale,
94
+ bandwidth,
95
+ baseline,
96
+ scales,
97
+ );
98
+ }
99
+
100
+ // ---------------------------------------------------------------------------
101
+ // Dumbbell (multi-series)
102
+ // ---------------------------------------------------------------------------
103
+
104
+ /** Compute dumbbell marks: connecting bar + colored dots per category. */
105
+ function computeDumbbellMarks(
106
+ data: readonly Record<string, unknown>[],
107
+ valueField: string,
108
+ categoryField: string,
109
+ colorField: string,
110
+ xScale: ScaleLinear<number, number>,
111
+ yScale: ScaleBand<string>,
112
+ bandwidth: number,
113
+ scales: ResolvedScales,
114
+ ): (PointMark | RectMark)[] {
115
+ const marks: (PointMark | RectMark)[] = [];
116
+ const categoryGroups = groupByField([...data], categoryField);
117
+
118
+ for (const [category, rows] of categoryGroups) {
119
+ const bandY = yScale(category);
120
+ if (bandY === undefined) continue;
121
+
122
+ const cy = bandY + bandwidth / 2;
123
+
124
+ // Collect all x-values for this category to find the range
125
+ const xValues: number[] = [];
126
+ for (const row of rows) {
127
+ const value = Number(row[valueField] ?? 0);
128
+ if (Number.isFinite(value)) xValues.push(value);
129
+ }
130
+
131
+ if (xValues.length === 0) continue;
132
+
133
+ const minVal = Math.min(...xValues);
134
+ const maxVal = Math.max(...xValues);
135
+ const xLeft = xScale(minVal);
136
+ const xRight = xScale(maxVal);
137
+ const barWidth = Math.abs(xRight - xLeft);
138
+
139
+ // Connecting bar (rendered first so dots layer on top)
140
+ if (barWidth > 0) {
141
+ const stemAria: MarkAria = {
142
+ label: `Range for ${category}: ${minVal} to ${maxVal}`,
143
+ };
144
+
145
+ marks.push({
146
+ type: 'rect',
147
+ x: Math.min(xLeft, xRight),
148
+ y: cy - STEM_WIDTH / 2,
149
+ width: barWidth,
150
+ height: STEM_WIDTH,
151
+ fill: STEM_COLOR,
152
+ data: rows[0] as Record<string, unknown>,
153
+ aria: stemAria,
154
+ });
155
+ }
156
+
157
+ // Individual dots for each series value
158
+ for (const row of rows) {
159
+ const value = Number(row[valueField] ?? 0);
160
+ if (!Number.isFinite(value)) continue;
161
+
162
+ const cx = xScale(value);
163
+ const colorCategory = String(row[colorField] ?? '');
164
+ const color = getColor(scales, colorCategory);
165
+
166
+ const dotAria: MarkAria = {
167
+ label: `${category}, ${colorCategory}: ${value}`,
168
+ };
169
+
170
+ marks.push({
171
+ type: 'point',
172
+ cx,
173
+ cy,
174
+ r: DOT_RADIUS,
175
+ fill: color,
176
+ stroke: '#ffffff',
177
+ strokeWidth: 2,
178
+ data: row as Record<string, unknown>,
179
+ aria: dotAria,
180
+ });
181
+ }
182
+ }
183
+
184
+ return marks;
185
+ }
186
+
187
+ // ---------------------------------------------------------------------------
188
+ // Lollipop (single series)
189
+ // ---------------------------------------------------------------------------
190
+
191
+ /** Compute lollipop marks: stem from baseline + dot. */
192
+ function computeLollipopMarks(
193
+ data: readonly Record<string, unknown>[],
194
+ valueField: string,
195
+ categoryField: string,
196
+ xScale: ScaleLinear<number, number>,
197
+ yScale: ScaleBand<string>,
198
+ bandwidth: number,
199
+ baseline: number,
200
+ scales: ResolvedScales,
201
+ ): (PointMark | RectMark)[] {
202
+ const marks: (PointMark | RectMark)[] = [];
203
+
204
+ for (const row of data) {
205
+ const category = String(row[categoryField] ?? '');
206
+ const value = Number(row[valueField] ?? 0);
207
+ if (!Number.isFinite(value)) continue;
208
+
209
+ const bandY = yScale(category);
210
+ if (bandY === undefined) continue;
211
+
212
+ const cx = xScale(value);
213
+ const cy = bandY + bandwidth / 2;
214
+
215
+ const color = getColor(scales, '__default__');
216
+
217
+ // Stem: thin rectangle from baseline to dot center
218
+ const stemX = Math.min(baseline, cx);
219
+ const stemWidth = Math.abs(cx - baseline);
220
+
221
+ if (stemWidth > 0) {
222
+ const stemAria: MarkAria = {
223
+ label: `Stem for ${category}`,
224
+ };
225
+
226
+ marks.push({
227
+ type: 'rect',
228
+ x: stemX,
229
+ y: cy - STEM_WIDTH / 2,
230
+ width: stemWidth,
231
+ height: STEM_WIDTH,
232
+ fill: STEM_COLOR,
233
+ data: row as Record<string, unknown>,
234
+ aria: stemAria,
235
+ });
236
+ }
237
+
238
+ // Dot
239
+ const dotAria: MarkAria = {
240
+ label: `${category}: ${value}`,
241
+ };
242
+
243
+ marks.push({
244
+ type: 'point',
245
+ cx,
246
+ cy,
247
+ r: DOT_RADIUS,
248
+ fill: color,
249
+ stroke: '#ffffff',
250
+ strokeWidth: 2,
251
+ data: row as Record<string, unknown>,
252
+ aria: dotAria,
253
+ });
254
+ }
255
+
256
+ return marks;
257
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Dot plot / lollipop chart module.
3
+ *
4
+ * Exports the dot chart renderer and computation functions.
5
+ */
6
+
7
+ import type { Mark, PointMark } from '@opendata-ai/openchart-core';
8
+ import type { ChartRenderer } from '../registry';
9
+ import { computeDotMarks } from './compute';
10
+ import { computeDotLabels } from './labels';
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Dot chart renderer
14
+ // ---------------------------------------------------------------------------
15
+
16
+ /**
17
+ * Dot/lollipop chart renderer.
18
+ *
19
+ * Produces stem (RectMark) and dot (PointMark) pairs for each data point.
20
+ * Value labels are attached to the dot marks.
21
+ */
22
+ export const dotRenderer: ChartRenderer = (spec, scales, chartArea, strategy, _theme) => {
23
+ const marks = computeDotMarks(spec, scales, chartArea, strategy);
24
+
25
+ // Extract just the point marks for label computation
26
+ const pointMarks = marks.filter((m): m is PointMark => m.type === 'point');
27
+
28
+ // Compute and attach labels to point marks (respects spec.labels.density)
29
+ const labels = computeDotLabels(pointMarks, chartArea, spec.labels.density);
30
+ let labelIdx = 0;
31
+ for (const mark of marks) {
32
+ if (mark.type === 'point' && labelIdx < labels.length) {
33
+ mark.label = labels[labelIdx];
34
+ labelIdx++;
35
+ }
36
+ }
37
+
38
+ return marks as Mark[];
39
+ };
40
+
41
+ // ---------------------------------------------------------------------------
42
+ // Public exports
43
+ // ---------------------------------------------------------------------------
44
+
45
+ export { computeDotMarks } from './compute';
46
+ export { computeDotLabels } from './labels';
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Dot chart label computation.
3
+ *
4
+ * Produces value labels positioned to the right of each dot.
5
+ *
6
+ * Respects the spec's label density setting:
7
+ * - 'all': show every label, skip collision detection
8
+ * - 'auto': existing behavior (collision detection)
9
+ * - 'endpoints': first and last dots only
10
+ * - 'none': return empty array
11
+ */
12
+
13
+ import type {
14
+ LabelCandidate,
15
+ LabelDensity,
16
+ PointMark,
17
+ Rect,
18
+ ResolvedLabel,
19
+ } from '@opendata-ai/openchart-core';
20
+ import { estimateTextWidth, resolveCollisions } from '@opendata-ai/openchart-core';
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // Constants
24
+ // ---------------------------------------------------------------------------
25
+
26
+ const LABEL_FONT_SIZE = 11;
27
+ const LABEL_FONT_WEIGHT = 600;
28
+ const LABEL_OFFSET_X = 10;
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Public API
32
+ // ---------------------------------------------------------------------------
33
+
34
+ /**
35
+ * Compute value labels for dot marks.
36
+ *
37
+ * Places labels to the right of each dot point.
38
+ */
39
+ export function computeDotLabels(
40
+ marks: PointMark[],
41
+ _chartArea: Rect,
42
+ density: LabelDensity = 'auto',
43
+ ): ResolvedLabel[] {
44
+ // 'none': no labels at all
45
+ if (density === 'none') return [];
46
+
47
+ // Filter marks for 'endpoints' density
48
+ const targetMarks =
49
+ density === 'endpoints' && marks.length > 1 ? [marks[0], marks[marks.length - 1]] : marks;
50
+
51
+ const candidates: LabelCandidate[] = [];
52
+
53
+ for (const mark of targetMarks) {
54
+ // Extract the display value from the aria label.
55
+ // Format is "category: value". Use the last colon to handle colons in category names.
56
+ const ariaLabel = mark.aria.label;
57
+ const lastColon = ariaLabel.lastIndexOf(':');
58
+ const valuePart = lastColon >= 0 ? ariaLabel.slice(lastColon + 1).trim() : '';
59
+ if (!valuePart) continue;
60
+
61
+ const textWidth = estimateTextWidth(valuePart, LABEL_FONT_SIZE, LABEL_FONT_WEIGHT);
62
+ const textHeight = LABEL_FONT_SIZE * 1.2;
63
+
64
+ candidates.push({
65
+ text: valuePart,
66
+ anchorX: mark.cx + mark.r + LABEL_OFFSET_X,
67
+ anchorY: mark.cy - textHeight / 2,
68
+ width: textWidth,
69
+ height: textHeight,
70
+ priority: 'data',
71
+ style: {
72
+ fontFamily: 'system-ui, -apple-system, sans-serif',
73
+ fontSize: LABEL_FONT_SIZE,
74
+ fontWeight: LABEL_FONT_WEIGHT,
75
+ fill: mark.fill,
76
+ lineHeight: 1.2,
77
+ textAnchor: 'start',
78
+ dominantBaseline: 'central',
79
+ },
80
+ });
81
+ }
82
+
83
+ if (candidates.length === 0) return [];
84
+
85
+ // 'all': skip collision detection, mark everything visible
86
+ if (density === 'all') {
87
+ return candidates.map((c) => ({
88
+ text: c.text,
89
+ x: c.anchorX,
90
+ y: c.anchorY,
91
+ style: c.style,
92
+ visible: true,
93
+ }));
94
+ }
95
+
96
+ return resolveCollisions(candidates);
97
+ }