@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,302 @@
1
+ /**
2
+ * Graph encoding resolution.
3
+ *
4
+ * Maps graph encoding channels (nodeSize, nodeColor, edgeWidth, edgeColor,
5
+ * nodeLabel) to computed visual properties on nodes and edges. Uses d3 scales
6
+ * the same way scatter/bubble charts do: scaleSqrt for size, scaleOrdinal
7
+ * for categorical color, scaleLinear for quantitative color.
8
+ */
9
+
10
+ import type {
11
+ GraphEdge,
12
+ GraphEncoding,
13
+ GraphNode,
14
+ ResolvedTheme,
15
+ } from '@opendata-ai/openchart-core';
16
+ import { max, min } from 'd3-array';
17
+ import { scaleLinear, scaleOrdinal, scaleSqrt } from 'd3-scale';
18
+
19
+ import type { CompiledGraphEdge, CompiledGraphNode } from './types';
20
+
21
+ // ---------------------------------------------------------------------------
22
+ // Constants
23
+ // ---------------------------------------------------------------------------
24
+
25
+ const DEFAULT_NODE_RADIUS = 5;
26
+ const MIN_NODE_RADIUS = 3;
27
+ const MAX_NODE_RADIUS = 20;
28
+
29
+ const DEFAULT_EDGE_WIDTH = 1;
30
+ const MIN_EDGE_WIDTH = 0.5;
31
+ const MAX_EDGE_WIDTH = 4;
32
+
33
+ const DEFAULT_STROKE_WIDTH = 1;
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // Helpers
37
+ // ---------------------------------------------------------------------------
38
+
39
+ /**
40
+ * Darken a hex color by a percentage.
41
+ *
42
+ * Doesn't use d3-color (engine doesn't depend on it). Operates directly
43
+ * on hex RGB channels. Falls back to the original color on parse failure.
44
+ */
45
+ export function darkenColor(hex: string, amount: number = 0.2): string {
46
+ // Strip # prefix
47
+ const clean = hex.replace(/^#/, '');
48
+ if (clean.length !== 6 && clean.length !== 3) return hex;
49
+
50
+ // Expand shorthand
51
+ const full =
52
+ clean.length === 3
53
+ ? clean
54
+ .split('')
55
+ .map((c) => c + c)
56
+ .join('')
57
+ : clean;
58
+
59
+ const r = Math.max(0, Math.round(parseInt(full.substring(0, 2), 16) * (1 - amount)));
60
+ const g = Math.max(0, Math.round(parseInt(full.substring(2, 4), 16) * (1 - amount)));
61
+ const b = Math.max(0, Math.round(parseInt(full.substring(4, 6), 16) * (1 - amount)));
62
+
63
+ return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
64
+ }
65
+
66
+ /**
67
+ * Apply opacity to a hex color, returning an rgba string.
68
+ */
69
+ function hexWithOpacity(hex: string, opacity: number): string {
70
+ const clean = hex.replace(/^#/, '');
71
+ if (clean.length !== 6 && clean.length !== 3) {
72
+ // Non-hex input: return as-is with opacity via CSS
73
+ return hex;
74
+ }
75
+
76
+ const full =
77
+ clean.length === 3
78
+ ? clean
79
+ .split('')
80
+ .map((c) => c + c)
81
+ .join('')
82
+ : clean;
83
+
84
+ const r = parseInt(full.substring(0, 2), 16);
85
+ const g = parseInt(full.substring(2, 4), 16);
86
+ const b = parseInt(full.substring(4, 6), 16);
87
+
88
+ return `rgba(${r}, ${g}, ${b}, ${opacity})`;
89
+ }
90
+
91
+ /**
92
+ * Compute the degree of each node (number of edges touching it).
93
+ */
94
+ function computeDegrees(nodes: GraphNode[], edges: GraphEdge[]): Map<string, number> {
95
+ const degrees = new Map<string, number>();
96
+ for (const node of nodes) {
97
+ degrees.set(node.id, 0);
98
+ }
99
+ for (const edge of edges) {
100
+ degrees.set(edge.source, (degrees.get(edge.source) ?? 0) + 1);
101
+ degrees.set(edge.target, (degrees.get(edge.target) ?? 0) + 1);
102
+ }
103
+ return degrees;
104
+ }
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // Node visual resolution
108
+ // ---------------------------------------------------------------------------
109
+
110
+ /**
111
+ * Resolve visual properties for all graph nodes.
112
+ *
113
+ * Applies nodeSize, nodeColor, and nodeLabel encoding channels from the
114
+ * spec to produce CompiledGraphNode objects with computed fill, radius,
115
+ * stroke, label, and label priority.
116
+ */
117
+ export function resolveNodeVisuals(
118
+ nodes: GraphNode[],
119
+ encoding: GraphEncoding,
120
+ edges: GraphEdge[],
121
+ theme: ResolvedTheme,
122
+ ): CompiledGraphNode[] {
123
+ const degrees = computeDegrees(nodes, edges);
124
+ const maxDegree = Math.max(1, ...degrees.values());
125
+
126
+ // Build node size scale
127
+ let sizeScale: ((v: number) => number) | undefined;
128
+ if (encoding.nodeSize?.field) {
129
+ const field = encoding.nodeSize.field;
130
+ const values = nodes.map((n) => Number(n[field])).filter((v) => Number.isFinite(v));
131
+
132
+ const sizeMin = min(values) ?? 0;
133
+ const sizeMax = max(values) ?? 1;
134
+
135
+ sizeScale = scaleSqrt().domain([sizeMin, sizeMax]).range([MIN_NODE_RADIUS, MAX_NODE_RADIUS]);
136
+ }
137
+
138
+ // Build node color scale
139
+ let colorFn: ((node: GraphNode) => string) | undefined;
140
+ if (encoding.nodeColor?.field) {
141
+ const field = encoding.nodeColor.field;
142
+ const fieldType = encoding.nodeColor.type ?? 'nominal';
143
+
144
+ if (fieldType === 'quantitative') {
145
+ const values = nodes.map((n) => Number(n[field])).filter((v) => Number.isFinite(v));
146
+ const colorMin = min(values) ?? 0;
147
+ const colorMax = max(values) ?? 1;
148
+
149
+ // Use first sequential palette
150
+ const seqPalettes = Object.values(theme.colors.sequential);
151
+ const palette = seqPalettes.length > 0 ? seqPalettes[0] : ['#ccc', '#333'];
152
+ const colorScale = scaleLinear<string>()
153
+ .domain([colorMin, colorMax])
154
+ .range([palette[0], palette[palette.length - 1]]);
155
+
156
+ colorFn = (node: GraphNode) => {
157
+ const val = Number(node[field]);
158
+ return Number.isFinite(val) ? colorScale(val) : theme.colors.categorical[0];
159
+ };
160
+ } else {
161
+ // nominal/ordinal
162
+ const uniqueValues = [...new Set(nodes.map((n) => String(n[field] ?? '')))];
163
+ const ordinalScale = scaleOrdinal<string>()
164
+ .domain(uniqueValues)
165
+ .range(theme.colors.categorical);
166
+
167
+ colorFn = (node: GraphNode) => ordinalScale(String(node[field] ?? ''));
168
+ }
169
+ }
170
+
171
+ const defaultColor = theme.colors.categorical[0];
172
+
173
+ return nodes.map((node) => {
174
+ // Radius
175
+ let radius = DEFAULT_NODE_RADIUS;
176
+ if (sizeScale && encoding.nodeSize?.field) {
177
+ const val = Number(node[encoding.nodeSize.field]);
178
+ if (Number.isFinite(val)) {
179
+ radius = sizeScale(val);
180
+ }
181
+ }
182
+
183
+ // Color
184
+ const fill = colorFn ? colorFn(node) : defaultColor;
185
+
186
+ // Stroke: darken fill by 20%
187
+ const stroke = darkenColor(fill);
188
+
189
+ // Label
190
+ let label: string | undefined;
191
+ if (encoding.nodeLabel?.field) {
192
+ const labelVal = node[encoding.nodeLabel.field];
193
+ label = labelVal != null ? String(labelVal) : undefined;
194
+ } else {
195
+ label = node.id;
196
+ }
197
+
198
+ // Label priority: degree / maxDegree (0 to 1)
199
+ const degree = degrees.get(node.id) ?? 0;
200
+ const labelPriority = maxDegree > 0 ? degree / maxDegree : 0;
201
+
202
+ // Data: spread all original node fields
203
+ const { id: _id, ...rest } = node;
204
+ const data: Record<string, unknown> = { id: node.id, ...rest };
205
+
206
+ return {
207
+ id: node.id,
208
+ radius,
209
+ fill,
210
+ stroke,
211
+ strokeWidth: DEFAULT_STROKE_WIDTH,
212
+ label,
213
+ labelPriority,
214
+ community: undefined,
215
+ data,
216
+ };
217
+ });
218
+ }
219
+
220
+ // ---------------------------------------------------------------------------
221
+ // Edge visual resolution
222
+ // ---------------------------------------------------------------------------
223
+
224
+ /**
225
+ * Resolve visual properties for all graph edges.
226
+ *
227
+ * Applies edgeWidth and edgeColor encoding channels to produce
228
+ * CompiledGraphEdge objects with computed stroke, strokeWidth, and style.
229
+ */
230
+ export function resolveEdgeVisuals(
231
+ edges: GraphEdge[],
232
+ encoding: GraphEncoding,
233
+ theme: ResolvedTheme,
234
+ ): CompiledGraphEdge[] {
235
+ // Edge width scale
236
+ let widthScale: ((v: number) => number) | undefined;
237
+ if (encoding.edgeWidth?.field) {
238
+ const field = encoding.edgeWidth.field;
239
+ const values = edges.map((e) => Number(e[field])).filter((v) => Number.isFinite(v));
240
+
241
+ const widthMin = min(values) ?? 0;
242
+ const widthMax = max(values) ?? 1;
243
+
244
+ widthScale = scaleLinear().domain([widthMin, widthMax]).range([MIN_EDGE_WIDTH, MAX_EDGE_WIDTH]);
245
+ }
246
+
247
+ // Edge color scale
248
+ let edgeColorFn: ((edge: GraphEdge) => string) | undefined;
249
+ if (encoding.edgeColor?.field) {
250
+ const field = encoding.edgeColor.field;
251
+ const fieldType = encoding.edgeColor.type ?? 'nominal';
252
+
253
+ if (fieldType === 'quantitative') {
254
+ const values = edges.map((e) => Number(e[field])).filter((v) => Number.isFinite(v));
255
+ const colorMin = min(values) ?? 0;
256
+ const colorMax = max(values) ?? 1;
257
+
258
+ const seqPalettes = Object.values(theme.colors.sequential);
259
+ const palette = seqPalettes.length > 0 ? seqPalettes[0] : ['#ccc', '#333'];
260
+ const colorScale = scaleLinear<string>()
261
+ .domain([colorMin, colorMax])
262
+ .range([palette[0], palette[palette.length - 1]]);
263
+
264
+ edgeColorFn = (edge: GraphEdge) => {
265
+ const val = Number(edge[field]);
266
+ return Number.isFinite(val) ? colorScale(val) : hexWithOpacity(theme.colors.axis, 0.4);
267
+ };
268
+ } else {
269
+ const uniqueValues = [...new Set(edges.map((e) => String(e[field] ?? '')))];
270
+ const ordinalScale = scaleOrdinal<string>()
271
+ .domain(uniqueValues)
272
+ .range(theme.colors.categorical);
273
+
274
+ edgeColorFn = (edge: GraphEdge) => ordinalScale(String(edge[field] ?? ''));
275
+ }
276
+ }
277
+
278
+ const defaultEdgeColor = hexWithOpacity(theme.colors.axis, 0.4);
279
+
280
+ return edges.map((edge) => {
281
+ const { source, target, ...rest } = edge;
282
+
283
+ let strokeWidth = DEFAULT_EDGE_WIDTH;
284
+ if (widthScale && encoding.edgeWidth?.field) {
285
+ const val = Number(edge[encoding.edgeWidth.field]);
286
+ if (Number.isFinite(val)) {
287
+ strokeWidth = widthScale(val);
288
+ }
289
+ }
290
+
291
+ const stroke = edgeColorFn ? edgeColorFn(edge) : defaultEdgeColor;
292
+
293
+ return {
294
+ source,
295
+ target,
296
+ stroke,
297
+ strokeWidth,
298
+ style: 'solid' as const,
299
+ data: { source, target, ...rest } as Record<string, unknown>,
300
+ };
301
+ });
302
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Graph compilation types.
3
+ *
4
+ * These types represent the engine output for graph specs. Unlike GraphLayout
5
+ * (which includes x/y positions for adapter rendering), GraphCompilation
6
+ * contains resolved visual properties WITHOUT positional layout. The force
7
+ * simulation in the adapter sets node positions at runtime.
8
+ */
9
+
10
+ import type {
11
+ A11yMetadata,
12
+ LegendLayout,
13
+ ResolvedChrome,
14
+ ResolvedTheme,
15
+ TooltipContent,
16
+ } from '@opendata-ai/openchart-core';
17
+
18
+ /** A compiled graph node with resolved visual properties (no x/y position). */
19
+ export interface CompiledGraphNode {
20
+ /** Node identifier from the spec. */
21
+ id: string;
22
+ /** Computed radius from nodeSize encoding (3-20px range, default 5px). */
23
+ radius: number;
24
+ /** Computed fill color from nodeColor encoding or community assignment. */
25
+ fill: string;
26
+ /** Stroke color, slightly darker than fill. */
27
+ stroke: string;
28
+ /** Stroke width in pixels. Default 1. */
29
+ strokeWidth: number;
30
+ /** Label text from nodeLabel encoding or node id. */
31
+ label: string | undefined;
32
+ /** Label priority for level-of-detail rendering (0-1, degree/maxDegree). */
33
+ labelPriority: number;
34
+ /** Community/cluster assignment from the clustering field. */
35
+ community: string | undefined;
36
+ /** Original node data (all fields from the spec node). */
37
+ data: Record<string, unknown>;
38
+ }
39
+
40
+ /** A compiled graph edge with resolved visual properties (no positional endpoints). */
41
+ export interface CompiledGraphEdge {
42
+ /** Source node id. */
43
+ source: string;
44
+ /** Target node id. */
45
+ target: string;
46
+ /** Stroke color from edgeColor encoding or theme default. */
47
+ stroke: string;
48
+ /** Stroke width from edgeWidth encoding (0.5-4px range, default 1px). */
49
+ strokeWidth: number;
50
+ /** Line style. */
51
+ style: 'solid' | 'dashed' | 'dotted';
52
+ /** Original edge data (all fields from the spec edge). */
53
+ data: Record<string, unknown>;
54
+ }
55
+
56
+ /** Configuration for the force simulation, derived from the spec layout. */
57
+ export interface SimulationConfig {
58
+ /** Repulsion strength between nodes. Negative = repulsion. */
59
+ chargeStrength: number;
60
+ /** Target distance between linked nodes. */
61
+ linkDistance: number;
62
+ /** Clustering configuration, or null if no clustering. */
63
+ clustering: { field: string; strength: number } | null;
64
+ /** How quickly the simulation cools. Default 0.0228. */
65
+ alphaDecay: number;
66
+ /** Velocity damping. Default 0.4. */
67
+ velocityDecay: number;
68
+ /** Collision radius: max node radius + padding. */
69
+ collisionRadius: number;
70
+ }
71
+
72
+ /**
73
+ * The complete engine output for graph specs.
74
+ *
75
+ * Contains resolved visual properties for nodes and edges, but does NOT
76
+ * include x/y positions. The adapter's force simulation assigns positions
77
+ * at runtime using the simulationConfig.
78
+ */
79
+ export interface GraphCompilation {
80
+ /** Compiled nodes with visual properties. */
81
+ nodes: CompiledGraphNode[];
82
+ /** Compiled edges with visual properties. */
83
+ edges: CompiledGraphEdge[];
84
+ /** Legend layout (community colors or nodeColor categories). */
85
+ legend: LegendLayout;
86
+ /** Resolved chrome text elements. */
87
+ chrome: ResolvedChrome;
88
+ /** Tooltip descriptors keyed by node id. */
89
+ tooltipDescriptors: Map<string, TooltipContent>;
90
+ /** Accessibility metadata. */
91
+ a11y: A11yMetadata;
92
+ /** Resolved theme used for rendering. */
93
+ theme: ResolvedTheme;
94
+ /** Total available dimensions. */
95
+ dimensions: { width: number; height: number };
96
+ /** Force simulation configuration. */
97
+ simulationConfig: SimulationConfig;
98
+ }
package/src/index.ts ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @opendata-ai/openchart-engine
3
+ *
4
+ * Headless computation engine that takes a declarative spec and produces
5
+ * structured layout objects (ChartLayout, TableLayout, GraphCompilation).
6
+ *
7
+ * The engine does pure math and data transformation. It knows nothing about
8
+ * the DOM, React, or any rendering target.
9
+ */
10
+
11
+ // ---------------------------------------------------------------------------
12
+ // Main compile API
13
+ // ---------------------------------------------------------------------------
14
+
15
+ export { compileChart, compileGraph, compileTable } from './compile';
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Graph compilation types
19
+ // ---------------------------------------------------------------------------
20
+
21
+ export type {
22
+ CompiledGraphEdge,
23
+ CompiledGraphNode,
24
+ GraphCompilation,
25
+ SimulationConfig,
26
+ } from './graphs/types';
27
+
28
+ // ---------------------------------------------------------------------------
29
+ // Compiler pipeline (spec validation, normalization, generic compile)
30
+ // ---------------------------------------------------------------------------
31
+
32
+ export type {
33
+ CompileResult,
34
+ NormalizedChartSpec,
35
+ NormalizedChrome,
36
+ NormalizedGraphSpec,
37
+ NormalizedSpec,
38
+ NormalizedTableSpec,
39
+ ValidationError,
40
+ ValidationErrorCode,
41
+ ValidationResult,
42
+ } from './compiler/index';
43
+ export {
44
+ compile,
45
+ normalizeSpec,
46
+ validateSpec,
47
+ } from './compiler/index';
48
+
49
+ // ---------------------------------------------------------------------------
50
+ // Chart renderer plugin API
51
+ // ---------------------------------------------------------------------------
52
+
53
+ export type { ChartRenderer } from './charts/registry';
54
+ export {
55
+ clearRenderers,
56
+ getChartRenderer,
57
+ registerChartRenderer,
58
+ } from './charts/registry';
59
+
60
+ // ---------------------------------------------------------------------------
61
+ // Re-export core types for convenience
62
+ // ---------------------------------------------------------------------------
63
+
64
+ export type {
65
+ ChartLayout,
66
+ ChartSpec,
67
+ CompileOptions,
68
+ CompileTableOptions,
69
+ GraphLayout,
70
+ GraphSpec,
71
+ TableLayout,
72
+ TableSpec,
73
+ VizSpec,
74
+ } from '@opendata-ai/openchart-core';
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Axis computation: tick positions, labels, and axis lines.
3
+ *
4
+ * Generates ticks manually (no d3-axis) so we have full control over
5
+ * responsive tick density and formatting.
6
+ */
7
+
8
+ import type {
9
+ AxisLabelDensity,
10
+ AxisLayout,
11
+ AxisTick,
12
+ Gridline,
13
+ LayoutStrategy,
14
+ Rect,
15
+ ResolvedTheme,
16
+ TextStyle,
17
+ } from '@opendata-ai/openchart-core';
18
+ import { abbreviateNumber, formatDate, formatNumber } from '@opendata-ai/openchart-core';
19
+ import type { ScaleBand } from 'd3-scale';
20
+ import type {
21
+ D3CategoricalScale,
22
+ D3ContinuousScale,
23
+ ResolvedScale,
24
+ ResolvedScales,
25
+ } from './scales';
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Constants
29
+ // ---------------------------------------------------------------------------
30
+
31
+ /** Base tick counts by axis label density. */
32
+ const TICK_COUNTS: Record<AxisLabelDensity, number> = {
33
+ full: 8,
34
+ reduced: 5,
35
+ minimal: 3,
36
+ };
37
+
38
+ // ---------------------------------------------------------------------------
39
+ // Tick generation
40
+ // ---------------------------------------------------------------------------
41
+
42
+ /** Generate ticks for a continuous scale (linear, time, log). */
43
+ function continuousTicks(resolvedScale: ResolvedScale, density: AxisLabelDensity): AxisTick[] {
44
+ const scale = resolvedScale.scale as D3ContinuousScale;
45
+ const count = resolvedScale.channel.axis?.tickCount ?? TICK_COUNTS[density];
46
+ const ticks: unknown[] = scale.ticks(count);
47
+
48
+ return ticks.map((value: unknown) => ({
49
+ value,
50
+ position: scale(value as number & Date) as number,
51
+ label: formatTickLabel(value, resolvedScale),
52
+ }));
53
+ }
54
+
55
+ /** Generate ticks for a band/point/ordinal scale. */
56
+ function categoricalTicks(resolvedScale: ResolvedScale, density: AxisLabelDensity): AxisTick[] {
57
+ const scale = resolvedScale.scale as D3CategoricalScale;
58
+ const domain: string[] = scale.domain();
59
+ const maxTicks = TICK_COUNTS[density];
60
+
61
+ // Band scales (bar charts) should always show all category labels.
62
+ // Only thin point/ordinal scales used for continuous-like axes (e.g. line charts).
63
+ let selectedValues = domain;
64
+ if (resolvedScale.type !== 'band' && domain.length > maxTicks) {
65
+ const step = Math.ceil(domain.length / maxTicks);
66
+ selectedValues = domain.filter((_: string, i: number) => i % step === 0);
67
+ }
68
+
69
+ return selectedValues.map((value: string) => {
70
+ // Band scales: use the center of the band
71
+ const bandScale = resolvedScale.type === 'band' ? (scale as ScaleBand<string>) : null;
72
+ const pos = bandScale
73
+ ? (bandScale(value) ?? 0) + bandScale.bandwidth() / 2
74
+ : ((scale(value) as number | undefined) ?? 0);
75
+
76
+ return {
77
+ value,
78
+ position: pos,
79
+ label: value,
80
+ };
81
+ });
82
+ }
83
+
84
+ /** Format a tick value based on the scale type. */
85
+ function formatTickLabel(value: unknown, resolvedScale: ResolvedScale): string {
86
+ const formatStr = resolvedScale.channel.axis?.format;
87
+
88
+ if (resolvedScale.type === 'time') {
89
+ if (formatStr) return String(value); // Custom format not implemented yet
90
+ return formatDate(value as Date);
91
+ }
92
+
93
+ if (resolvedScale.type === 'linear' || resolvedScale.type === 'log') {
94
+ const num = value as number;
95
+ if (formatStr) return formatNumber(num);
96
+ // Abbreviate large numbers for axis labels
97
+ if (Math.abs(num) >= 1000) return abbreviateNumber(num);
98
+ return formatNumber(num);
99
+ }
100
+
101
+ return String(value);
102
+ }
103
+
104
+ // ---------------------------------------------------------------------------
105
+ // Public API
106
+ // ---------------------------------------------------------------------------
107
+
108
+ /** Output of computeAxes. */
109
+ export interface AxesResult {
110
+ x?: AxisLayout;
111
+ y?: AxisLayout;
112
+ }
113
+
114
+ /**
115
+ * Compute axis layouts with tick positions, labels, and axis lines.
116
+ *
117
+ * @param scales - Resolved scales from computeScales.
118
+ * @param chartArea - The chart drawing area.
119
+ * @param strategy - Responsive layout strategy.
120
+ * @param theme - Resolved theme for styling.
121
+ */
122
+ export function computeAxes(
123
+ scales: ResolvedScales,
124
+ chartArea: Rect,
125
+ strategy: LayoutStrategy,
126
+ theme: ResolvedTheme,
127
+ ): AxesResult {
128
+ const result: AxesResult = {};
129
+ const density = strategy.axisLabelDensity;
130
+
131
+ const tickLabelStyle: TextStyle = {
132
+ fontFamily: theme.fonts.family,
133
+ fontSize: theme.fonts.sizes.axisTick,
134
+ fontWeight: theme.fonts.weights.normal,
135
+ fill: theme.colors.axis,
136
+ lineHeight: 1.2,
137
+ fontVariant: 'tabular-nums',
138
+ };
139
+
140
+ const axisLabelStyle: TextStyle = {
141
+ fontFamily: theme.fonts.family,
142
+ fontSize: theme.fonts.sizes.body,
143
+ fontWeight: theme.fonts.weights.medium,
144
+ fill: theme.colors.text,
145
+ lineHeight: 1.3,
146
+ };
147
+
148
+ if (scales.x) {
149
+ const ticks =
150
+ scales.x.type === 'band' || scales.x.type === 'point' || scales.x.type === 'ordinal'
151
+ ? categoricalTicks(scales.x, density)
152
+ : continuousTicks(scales.x, density);
153
+
154
+ const gridlines: Gridline[] = ticks.map((t) => ({
155
+ position: t.position,
156
+ major: true,
157
+ }));
158
+
159
+ result.x = {
160
+ ticks,
161
+ gridlines: scales.x.channel.axis?.grid ? gridlines : [],
162
+ label: scales.x.channel.axis?.label,
163
+ labelStyle: axisLabelStyle,
164
+ tickLabelStyle,
165
+ start: { x: chartArea.x, y: chartArea.y + chartArea.height },
166
+ end: { x: chartArea.x + chartArea.width, y: chartArea.y + chartArea.height },
167
+ };
168
+ }
169
+
170
+ if (scales.y) {
171
+ const ticks =
172
+ scales.y.type === 'band' || scales.y.type === 'point' || scales.y.type === 'ordinal'
173
+ ? categoricalTicks(scales.y, density)
174
+ : continuousTicks(scales.y, density);
175
+
176
+ const gridlines: Gridline[] = ticks.map((t) => ({
177
+ position: t.position,
178
+ major: true,
179
+ }));
180
+
181
+ result.y = {
182
+ ticks,
183
+ // Y-axis gridlines are shown by default (standard editorial practice)
184
+ gridlines,
185
+ label: scales.y.channel.axis?.label,
186
+ labelStyle: axisLabelStyle,
187
+ tickLabelStyle,
188
+ start: { x: chartArea.x, y: chartArea.y },
189
+ end: { x: chartArea.x, y: chartArea.y + chartArea.height },
190
+ };
191
+ }
192
+
193
+ return result;
194
+ }