@opendata-ai/openchart-core 2.0.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 (51) hide show
  1. package/README.md +130 -0
  2. package/dist/index.d.ts +2030 -0
  3. package/dist/index.js +1176 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/styles.css +757 -0
  6. package/package.json +61 -0
  7. package/src/accessibility/__tests__/alt-text.test.ts +110 -0
  8. package/src/accessibility/__tests__/aria.test.ts +125 -0
  9. package/src/accessibility/alt-text.ts +120 -0
  10. package/src/accessibility/aria.ts +73 -0
  11. package/src/accessibility/index.ts +6 -0
  12. package/src/colors/__tests__/colorblind.test.ts +63 -0
  13. package/src/colors/__tests__/contrast.test.ts +71 -0
  14. package/src/colors/__tests__/palettes.test.ts +54 -0
  15. package/src/colors/colorblind.ts +122 -0
  16. package/src/colors/contrast.ts +94 -0
  17. package/src/colors/index.ts +27 -0
  18. package/src/colors/palettes.ts +118 -0
  19. package/src/helpers/__tests__/spec-builders.test.ts +336 -0
  20. package/src/helpers/spec-builders.ts +410 -0
  21. package/src/index.ts +129 -0
  22. package/src/labels/__tests__/collision.test.ts +197 -0
  23. package/src/labels/collision.ts +154 -0
  24. package/src/labels/index.ts +6 -0
  25. package/src/layout/__tests__/chrome.test.ts +114 -0
  26. package/src/layout/__tests__/text-measure.test.ts +49 -0
  27. package/src/layout/chrome.ts +223 -0
  28. package/src/layout/index.ts +6 -0
  29. package/src/layout/text-measure.ts +54 -0
  30. package/src/locale/__tests__/format.test.ts +90 -0
  31. package/src/locale/format.ts +132 -0
  32. package/src/locale/index.ts +6 -0
  33. package/src/responsive/__tests__/breakpoints.test.ts +58 -0
  34. package/src/responsive/breakpoints.ts +92 -0
  35. package/src/responsive/index.ts +18 -0
  36. package/src/styles/viz.css +757 -0
  37. package/src/theme/__tests__/dark-mode.test.ts +68 -0
  38. package/src/theme/__tests__/defaults.test.ts +47 -0
  39. package/src/theme/__tests__/resolve.test.ts +61 -0
  40. package/src/theme/dark-mode.ts +123 -0
  41. package/src/theme/defaults.ts +85 -0
  42. package/src/theme/index.ts +7 -0
  43. package/src/theme/resolve.ts +190 -0
  44. package/src/types/__tests__/spec.test.ts +387 -0
  45. package/src/types/encoding.ts +144 -0
  46. package/src/types/events.ts +96 -0
  47. package/src/types/index.ts +141 -0
  48. package/src/types/layout.ts +794 -0
  49. package/src/types/spec.ts +563 -0
  50. package/src/types/table.ts +105 -0
  51. package/src/types/theme.ts +159 -0
package/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # @opendata-ai/openchart-core
2
+
3
+ Types, spec builders, formatters, and palettes for the OpenChart visualization library.
4
+
5
+ This is the foundation package. It has no DOM dependencies and runs in any JavaScript environment. The framework packages (`openchart-react`, `openchart-vue`, `openchart-svelte`) all depend on it.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @opendata-ai/openchart-core
11
+ ```
12
+
13
+ ## Spec builders
14
+
15
+ Convenience functions that create chart and table specs from data. They infer field types automatically (quantitative, temporal, nominal) by sampling your data, so you don't have to specify types manually.
16
+
17
+ ```typescript
18
+ import { lineChart, columnChart, barChart } from '@opendata-ai/openchart-core';
19
+
20
+ const line = lineChart(data, 'date', 'value');
21
+ const columns = columnChart(data, 'month', 'revenue');
22
+ const bars = barChart(data, 'category', 'count');
23
+ ```
24
+
25
+ All chart builders accept an optional `options` argument for color encoding, annotations, chrome (title/subtitle/source), theme overrides, and dark mode.
26
+
27
+ ```typescript
28
+ const spec = lineChart(data, 'date', 'value', {
29
+ color: 'region',
30
+ chrome: { title: 'Sales by Region', subtitle: 'Q1 2025' },
31
+ });
32
+ ```
33
+
34
+ ### Available builders
35
+
36
+ | Function | Chart type | Params |
37
+ |----------|-----------|--------|
38
+ | `lineChart(data, x, y, options?)` | Line | x/y position fields |
39
+ | `barChart(data, category, value, options?)` | Horizontal bar | category on y-axis, value on x-axis |
40
+ | `columnChart(data, x, y, options?)` | Vertical column | x/y position fields |
41
+ | `pieChart(data, category, value, options?)` | Pie | category for color, value for size |
42
+ | `donutChart(data, category, value, options?)` | Donut | category for color, value for size |
43
+ | `areaChart(data, x, y, options?)` | Area | x/y position fields |
44
+ | `dotChart(data, x, y, options?)` | Dot/strip | x/y position fields |
45
+ | `scatterChart(data, x, y, options?)` | Scatter | x/y position fields |
46
+ | `dataTable(data, options?)` | Data table | columns auto-generated if omitted |
47
+
48
+ Field refs can be a plain string (field name) or a full `EncodingChannel` object when you need control over type, axis, scale, or aggregation.
49
+
50
+ ## dataTable()
51
+
52
+ The table builder is worth calling out specifically. If you pass data without a `columns` config, it auto-generates column definitions from your data keys. It infers types from the values: numbers get right-aligned, text gets left-aligned.
53
+
54
+ ```typescript
55
+ import { dataTable } from '@opendata-ai/openchart-core';
56
+
57
+ // Columns auto-generated from data keys
58
+ const spec = dataTable([
59
+ { name: 'Alice', age: 30, city: 'Portland' },
60
+ { name: 'Bob', age: 25, city: 'Seattle' },
61
+ ]);
62
+ // age column auto-detected as quantitative, right-aligned
63
+ // name and city columns detected as nominal, left-aligned
64
+ ```
65
+
66
+ For more control, pass explicit column configs:
67
+
68
+ ```typescript
69
+ const spec = dataTable(data, {
70
+ columns: [
71
+ { key: 'name', label: 'Name' },
72
+ { key: 'age', label: 'Age', align: 'right' },
73
+ ],
74
+ search: true,
75
+ pagination: { pageSize: 20 },
76
+ });
77
+ ```
78
+
79
+ ## StoredVizSpec types
80
+
81
+ When you need to persist specs without bulky data arrays (for storage, serialization, or database records), use the data-stripped type variants:
82
+
83
+ ```typescript
84
+ import type {
85
+ StoredVizSpec,
86
+ ChartSpecWithoutData,
87
+ TableSpecWithoutData,
88
+ GraphSpecWithoutData,
89
+ } from '@opendata-ai/openchart-core';
90
+
91
+ // Store the spec shape without data
92
+ const stored: ChartSpecWithoutData = {
93
+ type: 'line',
94
+ encoding: {
95
+ x: { field: 'date', type: 'temporal' },
96
+ y: { field: 'value', type: 'quantitative' },
97
+ },
98
+ };
99
+
100
+ // StoredVizSpec is the union of all three
101
+ function saveSpec(spec: StoredVizSpec) {
102
+ localStorage.setItem('viz', JSON.stringify(spec));
103
+ }
104
+ ```
105
+
106
+ ## Type guards
107
+
108
+ Runtime narrowing for `VizSpec` values. Useful when you have a generic spec and need to branch on its type:
109
+
110
+ ```typescript
111
+ import { isChartSpec, isTableSpec, isGraphSpec } from '@opendata-ai/openchart-core';
112
+ import type { VizSpec } from '@opendata-ai/openchart-core';
113
+
114
+ function describe(spec: VizSpec): string {
115
+ if (isChartSpec(spec)) return `Chart: ${spec.type}`;
116
+ if (isTableSpec(spec)) return 'Data table';
117
+ if (isGraphSpec(spec)) return 'Network graph';
118
+ }
119
+ ```
120
+
121
+ ## Other exports
122
+
123
+ Beyond spec builders, this package provides:
124
+
125
+ - **Theme system** - `DEFAULT_THEME`, `resolveTheme()`, `adaptTheme()` for dark mode adaptation
126
+ - **Color palettes** - `CATEGORICAL_PALETTE`, `SEQUENTIAL_PALETTES`, `DIVERGING_PALETTES`
127
+ - **Accessibility** - `generateAltText()`, `generateAriaLabels()`, contrast ratio utilities
128
+ - **Formatting** - `formatNumber()`, `formatDate()`, `abbreviateNumber()`
129
+ - **Layout** - `computeChrome()`, `estimateTextWidth()`, responsive breakpoints
130
+ - **Label collision** - `resolveCollisions()` for automatic label placement