@opendata-ai/openchart-engine 7.11.0 → 8.0.0-rc.2
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 +123 -14
- package/dist/index.js +16166 -10027
- package/dist/index.js.map +1 -1
- package/package.json +11 -4
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +170 -56
- package/src/__tests__/annotation-behavior.test.ts +721 -0
- package/src/__tests__/axes.test.ts +50 -3
- package/src/__tests__/compile-chart.test.ts +30 -0
- package/src/__tests__/compile-footnotes.test.ts +277 -0
- package/src/__tests__/compile-layer.test.ts +120 -1
- package/src/__tests__/helpers/invariants.ts +54 -20
- package/src/__tests__/known-bugs.test.ts +455 -2
- package/src/__tests__/legend.test.ts +69 -4
- package/src/__tests__/ranking-recipes.test.ts +260 -0
- package/src/__tests__/scales.test.ts +9 -3
- package/src/__tests__/series-search.test.ts +92 -0
- package/src/__tests__/spec-sugar.test.ts +801 -0
- package/src/__tests__/you-draw-it.test.ts +160 -0
- package/src/annotations/__tests__/compute.test.ts +207 -92
- package/src/annotations/__tests__/geometry.test.ts +66 -0
- package/src/annotations/__tests__/resolve-text.test.ts +527 -27
- package/src/annotations/__tests__/rich-text.test.ts +91 -0
- package/src/annotations/__tests__/thinning.test.ts +148 -0
- package/src/annotations/collisions.ts +130 -84
- package/src/annotations/compute.ts +83 -85
- package/src/annotations/constants.ts +116 -6
- package/src/annotations/geometry.ts +222 -77
- package/src/annotations/move.ts +135 -0
- package/src/annotations/placement.ts +62 -14
- package/src/annotations/resolve-range.ts +2 -0
- package/src/annotations/resolve-refline.ts +2 -0
- package/src/annotations/resolve-text.ts +312 -111
- package/src/annotations/rich-text.ts +65 -0
- package/src/annotations/thinning.ts +84 -10
- package/src/barlist/compile-barlist.ts +16 -6
- package/src/charts/_shared/format-label-value.ts +9 -8
- package/src/charts/bar/__tests__/compute.test.ts +57 -10
- package/src/charts/bar/__tests__/labels.test.ts +5 -4
- package/src/charts/bar/compute.ts +2 -6
- package/src/charts/bar/index.ts +8 -1
- package/src/charts/bar/labels.ts +3 -3
- package/src/charts/beeswarm/__tests__/compute.test.ts +280 -0
- package/src/charts/beeswarm/__tests__/dodge.test.ts +148 -0
- package/src/charts/beeswarm/compute.ts +203 -0
- package/src/charts/beeswarm/dodge.ts +155 -0
- package/src/charts/beeswarm/index.ts +32 -0
- package/src/charts/builtin.ts +12 -1
- package/src/charts/calendar/__tests__/compute.test.ts +417 -0
- package/src/charts/calendar/compute.ts +319 -0
- package/src/charts/calendar/index.ts +32 -0
- package/src/charts/column/__tests__/compute.test.ts +21 -7
- package/src/charts/column/__tests__/labels.test.ts +76 -1
- package/src/charts/column/compute.ts +2 -6
- package/src/charts/column/index.ts +10 -2
- package/src/charts/column/labels.ts +95 -21
- package/src/charts/dot/index.ts +8 -1
- package/src/charts/dot/labels.ts +3 -3
- package/src/charts/line/__tests__/compute.test.ts +15 -19
- package/src/charts/line/area.ts +8 -23
- package/src/charts/line/compute.ts +32 -2
- package/src/charts/parliament/__tests__/compute.test.ts +411 -0
- package/src/charts/parliament/compute.ts +313 -0
- package/src/charts/parliament/index.ts +32 -0
- package/src/charts/pie/__tests__/compute.test.ts +103 -0
- package/src/charts/pie/compute.ts +63 -8
- package/src/charts/pie/labels.ts +1 -0
- package/src/charts/range/__tests__/compute.test.ts +486 -0
- package/src/charts/range/compute.ts +273 -0
- package/src/charts/range/index.ts +82 -0
- package/src/charts/range/labels.ts +169 -0
- package/src/charts/rect/__tests__/compute.test.ts +120 -0
- package/src/charts/rect/index.ts +127 -0
- package/src/charts/scatter/compute.ts +7 -21
- package/src/charts/text/__tests__/compute.test.ts +307 -0
- package/src/charts/text/index.ts +44 -7
- package/src/charts/tick/__tests__/compute.test.ts +75 -0
- package/src/charts/tick/index.ts +7 -4
- package/src/charts/utils.ts +5 -3
- package/src/charts/waffle/__tests__/compute.test.ts +305 -0
- package/src/charts/waffle/compute.ts +197 -0
- package/src/charts/waffle/index.ts +31 -0
- package/src/compile/__tests__/color-scale-reverse.test.ts +89 -0
- package/src/compile/__tests__/data-clip.test.ts +68 -0
- package/src/compile/__tests__/fill-patterns.test.ts +154 -0
- package/src/compile/__tests__/size-scale.test.ts +108 -0
- package/src/compile/color-scale-range.ts +54 -33
- package/src/compile/data-clip.ts +21 -6
- package/src/compile/fill-patterns.ts +74 -0
- package/src/compile/layer.ts +117 -21
- package/src/compile/size-scale.ts +139 -0
- package/src/compile/spec-sugar.ts +758 -0
- package/src/compile/you-draw-it.ts +123 -0
- package/src/compile.ts +310 -103
- package/src/compiler/__tests__/a11y-warnings.test.ts +148 -0
- package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +20 -0
- package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +22 -0
- package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +14 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +34 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +22 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +41 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +38 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +28 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +28 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +30 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +29 -0
- package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +32 -0
- package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +25 -0
- package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +21 -0
- package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +23 -0
- package/src/compiler/__tests__/keys.test.ts +3 -9
- package/src/compiler/__tests__/normalize.test.ts +65 -1
- package/src/compiler/__tests__/validate-did-you-mean.test.ts +86 -0
- package/src/compiler/__tests__/validate-near-miss.test.ts +89 -0
- package/src/compiler/__tests__/validate.test.ts +156 -0
- package/src/compiler/__tests__/vl-parity.test.ts +68 -0
- package/src/compiler/a11y-warnings.ts +110 -0
- package/src/compiler/keys.ts +5 -4
- package/src/compiler/normalize.ts +150 -7
- package/src/compiler/types.ts +11 -0
- package/src/compiler/validate.ts +664 -18
- package/src/endpoint-labels/__tests__/compute.test.ts +88 -0
- package/src/endpoint-labels/compute.ts +40 -12
- package/src/endpoint-labels/format.ts +45 -15
- package/src/endpoint-labels/predict.ts +50 -36
- package/src/format/__tests__/field-format.test.ts +164 -0
- package/src/format/field-format.ts +27 -0
- package/src/geo/__tests__/compile-map.test.ts +621 -0
- package/src/geo/compile-map.ts +856 -0
- package/src/geo/join.ts +115 -0
- package/src/geo/projections.ts +45 -0
- package/src/geo/types.ts +24 -0
- package/src/graphs/__tests__/compile-graph.test.ts +25 -0
- package/src/graphs/compile-graph.ts +27 -18
- package/src/index.ts +20 -2
- package/src/layout/axes/ticks.ts +59 -10
- package/src/layout/axes.ts +35 -6
- package/src/layout/dimensions.ts +139 -83
- package/src/layout/facet.ts +14 -3
- package/src/layout/plan.ts +83 -33
- package/src/layout/scales.ts +132 -15
- package/src/legend/__tests__/continuous.test.ts +318 -0
- package/src/legend/__tests__/size.test.ts +348 -0
- package/src/legend/__tests__/suppression.test.ts +17 -1
- package/src/legend/compute.ts +119 -13
- package/src/legend/continuous.ts +327 -0
- package/src/legend/size.ts +407 -0
- package/src/legend/suppression.ts +8 -7
- package/src/sankey/compile-sankey.ts +31 -19
- package/src/tables/format-cells.ts +16 -7
- package/src/tilemap/__tests__/compile-tilemap.test.ts +39 -0
- package/src/tilemap/compile-tilemap.ts +24 -7
- package/src/tooltips/__tests__/compute.test.ts +2 -2
- package/src/tooltips/compute.ts +422 -41
- package/src/transforms/__tests__/conditional.test.ts +18 -0
- package/src/transforms/aggregate.ts +2 -1
|
@@ -56,4 +56,72 @@ describe('filterClippedDomains', () => {
|
|
|
56
56
|
{ x: 25, y: 80 },
|
|
57
57
|
]);
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
describe('categorical domains', () => {
|
|
61
|
+
const years = [
|
|
62
|
+
{ year: '2019', pct: 32.4 },
|
|
63
|
+
{ year: '2020', pct: 31.9 },
|
|
64
|
+
{ year: '2021', pct: 33.9 },
|
|
65
|
+
{ year: '2022', pct: 33.6 },
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
it('keeps only rows whose value is in the domain set', () => {
|
|
69
|
+
const encoding: Encoding = {
|
|
70
|
+
x: {
|
|
71
|
+
field: 'year',
|
|
72
|
+
type: 'ordinal',
|
|
73
|
+
scale: { domain: ['2021', '2022'], clip: true },
|
|
74
|
+
},
|
|
75
|
+
y: { field: 'pct', type: 'quantitative' },
|
|
76
|
+
};
|
|
77
|
+
expect(filterClippedDomains(years, encoding)).toEqual([
|
|
78
|
+
{ year: '2021', pct: 33.9 },
|
|
79
|
+
{ year: '2022', pct: 33.6 },
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('matches numeric row values against a string domain', () => {
|
|
84
|
+
const numericYears = [
|
|
85
|
+
{ year: 2020, pct: 31.9 },
|
|
86
|
+
{ year: 2021, pct: 33.9 },
|
|
87
|
+
];
|
|
88
|
+
const encoding: Encoding = {
|
|
89
|
+
x: {
|
|
90
|
+
field: 'year',
|
|
91
|
+
type: 'ordinal',
|
|
92
|
+
scale: { domain: ['2021'], clip: true },
|
|
93
|
+
},
|
|
94
|
+
y: { field: 'pct', type: 'quantitative' },
|
|
95
|
+
};
|
|
96
|
+
expect(filterClippedDomains(numericYears, encoding)).toEqual([{ year: 2021, pct: 33.9 }]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('does not treat a two-category domain as a numeric range', () => {
|
|
100
|
+
const encoding: Encoding = {
|
|
101
|
+
x: {
|
|
102
|
+
field: 'year',
|
|
103
|
+
type: 'ordinal',
|
|
104
|
+
// A range reading would keep 2020 and 2021 too; membership must not.
|
|
105
|
+
scale: { domain: ['2019', '2022'], clip: true },
|
|
106
|
+
},
|
|
107
|
+
y: { field: 'pct', type: 'quantitative' },
|
|
108
|
+
};
|
|
109
|
+
expect(filterClippedDomains(years, encoding)).toEqual([
|
|
110
|
+
{ year: '2019', pct: 32.4 },
|
|
111
|
+
{ year: '2022', pct: 33.6 },
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('passes data through when the channel has no clip', () => {
|
|
116
|
+
const encoding: Encoding = {
|
|
117
|
+
x: {
|
|
118
|
+
field: 'year',
|
|
119
|
+
type: 'ordinal',
|
|
120
|
+
scale: { domain: ['2021', '2022'] },
|
|
121
|
+
},
|
|
122
|
+
y: { field: 'pct', type: 'quantitative' },
|
|
123
|
+
};
|
|
124
|
+
expect(filterClippedDomains(years, encoding)).toBe(years);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
59
127
|
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fill pattern assignment (mark.fillPattern: 'auto').
|
|
3
|
+
*
|
|
4
|
+
* Locks the deterministic pattern-to-series assignment, the opt-in gate,
|
|
5
|
+
* and the minimum-area rule for thin marks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ChartSpec, RectMark } from '@opendata-ai/openchart-core';
|
|
9
|
+
import { describe, expect, it } from 'vitest';
|
|
10
|
+
import { compileChart } from '../../compile';
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Fixtures
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
const FOUR_SERIES_DATA = [
|
|
17
|
+
{ quarter: 'Q1', source: 'Solar', twh: 30 },
|
|
18
|
+
{ quarter: 'Q1', source: 'Wind', twh: 45 },
|
|
19
|
+
{ quarter: 'Q1', source: 'Hydro', twh: 25 },
|
|
20
|
+
{ quarter: 'Q1', source: 'Gas', twh: 60 },
|
|
21
|
+
{ quarter: 'Q2', source: 'Solar', twh: 35 },
|
|
22
|
+
{ quarter: 'Q2', source: 'Wind', twh: 50 },
|
|
23
|
+
{ quarter: 'Q2', source: 'Hydro', twh: 22 },
|
|
24
|
+
{ quarter: 'Q2', source: 'Gas', twh: 55 },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function stackedSpec(fillPattern?: 'auto' | 'none'): ChartSpec {
|
|
28
|
+
return {
|
|
29
|
+
mark: { type: 'bar', ...(fillPattern ? { fillPattern } : {}) },
|
|
30
|
+
data: FOUR_SERIES_DATA,
|
|
31
|
+
encoding: {
|
|
32
|
+
x: { field: 'quarter', type: 'nominal' },
|
|
33
|
+
y: { field: 'twh', type: 'quantitative', stack: 'zero' },
|
|
34
|
+
color: { field: 'source', type: 'nominal' },
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const OPTS = { width: 600, height: 400 };
|
|
40
|
+
|
|
41
|
+
function rectMarks(spec: ChartSpec): RectMark[] {
|
|
42
|
+
return compileChart(spec, OPTS).marks.filter((m): m is RectMark => m.type === 'rect');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// Tests
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
describe('applyFillPatterns via compileChart', () => {
|
|
50
|
+
it('assigns four distinct patterns to a four-series stacked bar', () => {
|
|
51
|
+
const rects = rectMarks(stackedSpec('auto'));
|
|
52
|
+
expect(rects.length).toBeGreaterThan(0);
|
|
53
|
+
|
|
54
|
+
const byColor = new Map<string, string>();
|
|
55
|
+
for (const rect of rects) {
|
|
56
|
+
expect(rect.pattern).toBeDefined();
|
|
57
|
+
byColor.set(String(rect.fill), rect.pattern!.type);
|
|
58
|
+
}
|
|
59
|
+
// 4 series -> 4 distinct fills -> 4 distinct pattern shapes
|
|
60
|
+
expect(byColor.size).toBe(4);
|
|
61
|
+
expect(new Set(byColor.values()).size).toBe(4);
|
|
62
|
+
expect([...byColor.values()].sort()).toEqual(
|
|
63
|
+
['crosshatch', 'diagonal', 'dot', 'vertical'].sort(),
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('is deterministic across repeat compiles', () => {
|
|
68
|
+
const first = rectMarks(stackedSpec('auto')).map((r) => r.pattern);
|
|
69
|
+
const second = rectMarks(stackedSpec('auto')).map((r) => r.pattern);
|
|
70
|
+
expect(second).toEqual(first);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('picks a contrast-aware line color for each pattern', () => {
|
|
74
|
+
for (const rect of rectMarks(stackedSpec('auto'))) {
|
|
75
|
+
expect(['#ffffff', '#111111']).toContain(rect.pattern!.line);
|
|
76
|
+
expect(rect.pattern!.base).toBe(rect.fill);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('does nothing by default or with fillPattern: none', () => {
|
|
81
|
+
for (const rect of rectMarks(stackedSpec())) {
|
|
82
|
+
expect(rect.pattern).toBeUndefined();
|
|
83
|
+
}
|
|
84
|
+
for (const rect of rectMarks(stackedSpec('none'))) {
|
|
85
|
+
expect(rect.pattern).toBeUndefined();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('minimum-area rule: thin segments keep their solid fill', () => {
|
|
90
|
+
// Tiny values force sub-12px stacked segments at 400px height.
|
|
91
|
+
const thinData = FOUR_SERIES_DATA.map((row) =>
|
|
92
|
+
row.source === 'Hydro' ? { ...row, twh: 0.5 } : row,
|
|
93
|
+
);
|
|
94
|
+
const spec = { ...stackedSpec('auto'), data: thinData };
|
|
95
|
+
const rects = rectMarks(spec);
|
|
96
|
+
|
|
97
|
+
const thin = rects.filter((r) => Math.min(r.width, r.height) < 12);
|
|
98
|
+
const thick = rects.filter((r) => Math.min(r.width, r.height) >= 12);
|
|
99
|
+
expect(thin.length).toBeGreaterThan(0);
|
|
100
|
+
expect(thick.length).toBeGreaterThan(0);
|
|
101
|
+
for (const rect of thin) expect(rect.pattern).toBeUndefined();
|
|
102
|
+
for (const rect of thick) expect(rect.pattern).toBeDefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('assignment does not shift when individual marks are too small', () => {
|
|
106
|
+
// Same series order, one series squeezed below the minimum extent:
|
|
107
|
+
// surviving patterns keep the same shape per series color.
|
|
108
|
+
const thinData = FOUR_SERIES_DATA.map((row) =>
|
|
109
|
+
row.source === 'Wind' ? { ...row, twh: 0.5 } : row,
|
|
110
|
+
);
|
|
111
|
+
const base = rectMarks(stackedSpec('auto'));
|
|
112
|
+
const squeezed = rectMarks({ ...stackedSpec('auto'), data: thinData });
|
|
113
|
+
|
|
114
|
+
const patternByFill = (rects: RectMark[]) => {
|
|
115
|
+
const map = new Map<string, string>();
|
|
116
|
+
for (const r of rects) {
|
|
117
|
+
if (r.pattern) map.set(String(r.fill), r.pattern.type);
|
|
118
|
+
}
|
|
119
|
+
return map;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const baseMap = patternByFill(base);
|
|
123
|
+
for (const [fill, type] of patternByFill(squeezed)) {
|
|
124
|
+
expect(baseMap.get(fill)).toBe(type);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('fill patterns on arcs', () => {
|
|
130
|
+
it('patterns pie slices and skips slivers', () => {
|
|
131
|
+
const spec: ChartSpec = {
|
|
132
|
+
mark: { type: 'arc', fillPattern: 'auto' },
|
|
133
|
+
data: [
|
|
134
|
+
{ browser: 'Chrome', share: 64 },
|
|
135
|
+
{ browser: 'Safari', share: 20 },
|
|
136
|
+
{ browser: 'Edge', share: 15 },
|
|
137
|
+
{ browser: 'Other', share: 0.4 },
|
|
138
|
+
],
|
|
139
|
+
encoding: {
|
|
140
|
+
color: { field: 'browser', type: 'nominal' },
|
|
141
|
+
y: { field: 'share', type: 'quantitative' },
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
const layout = compileChart(spec, OPTS);
|
|
145
|
+
const arcs = layout.marks.filter((m) => m.type === 'arc');
|
|
146
|
+
expect(arcs.length).toBe(4);
|
|
147
|
+
|
|
148
|
+
const patterned = arcs.filter((a) => a.pattern);
|
|
149
|
+
const solid = arcs.filter((a) => !a.pattern);
|
|
150
|
+
// The 0.4% sliver falls under the minimum-area rule.
|
|
151
|
+
expect(patterned.length).toBe(3);
|
|
152
|
+
expect(solid.length).toBe(1);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { EncodingChannel } from '@opendata-ai/openchart-core';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { buildSizeScale } from '../size-scale';
|
|
4
|
+
|
|
5
|
+
const DATA = [{ v: 1 }, { v: 4 }, { v: 9 }];
|
|
6
|
+
const SQRT = { curve: 'sqrt' as const, range: [3, 30] as [number, number] };
|
|
7
|
+
const LINEAR = { curve: 'linear' as const, range: [11, 32] as [number, number] };
|
|
8
|
+
|
|
9
|
+
const size = (extra: Partial<EncodingChannel> = {}): EncodingChannel =>
|
|
10
|
+
({ field: 'v', type: 'quantitative', ...extra }) as EncodingChannel;
|
|
11
|
+
|
|
12
|
+
describe('buildSizeScale', () => {
|
|
13
|
+
it('returns null when there is no size encoding', () => {
|
|
14
|
+
expect(buildSizeScale(undefined, DATA, SQRT)).toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('fits the domain to the data extent and the range to the default', () => {
|
|
18
|
+
const resolved = buildSizeScale(size(), DATA, SQRT);
|
|
19
|
+
expect(resolved?.domain).toEqual([1, 9]);
|
|
20
|
+
expect(resolved?.range).toEqual([3, 30]);
|
|
21
|
+
expect(resolved?.field).toBe('v');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('maps circles by area (sqrt), not by radius', () => {
|
|
25
|
+
const { scale } = buildSizeScale(size(), DATA, SQRT)!;
|
|
26
|
+
// Endpoints pin to the range.
|
|
27
|
+
expect(scale(1)).toBeCloseTo(3, 5);
|
|
28
|
+
expect(scale(9)).toBeCloseTo(30, 5);
|
|
29
|
+
// The midpoint of a sqrt scale sits ABOVE the linear midpoint (16.5):
|
|
30
|
+
// r = 3 + 27 * (sqrt(4)-sqrt(1)) / (sqrt(9)-sqrt(1)) = 16.5 -- for v=4
|
|
31
|
+
// that lands exactly on the linear midpoint by coincidence of 1/4/9, so
|
|
32
|
+
// check a value where the two genuinely diverge.
|
|
33
|
+
const linearAt6 = 3 + 27 * ((6 - 1) / (9 - 1)); // 19.875
|
|
34
|
+
expect(scale(6)).toBeGreaterThan(linearAt6);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('maps glyphs by height (linear)', () => {
|
|
38
|
+
const { scale } = buildSizeScale(size(), DATA, LINEAR)!;
|
|
39
|
+
expect(scale(1)).toBeCloseTo(11, 5);
|
|
40
|
+
expect(scale(9)).toBeCloseTo(32, 5);
|
|
41
|
+
// Exactly proportional: no sqrt bulge.
|
|
42
|
+
expect(scale(5)).toBeCloseTo(11 + (32 - 11) * ((5 - 1) / (9 - 1)), 5);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('lets an explicit scale.domain override the data extent', () => {
|
|
46
|
+
const resolved = buildSizeScale(size({ scale: { domain: [0, 100] } }), DATA, SQRT);
|
|
47
|
+
expect(resolved?.domain).toEqual([0, 100]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('lets an explicit scale.range override the default magnitudes', () => {
|
|
51
|
+
const resolved = buildSizeScale(size({ scale: { range: [5, 12] } }), DATA, SQRT);
|
|
52
|
+
expect(resolved?.range).toEqual([5, 12]);
|
|
53
|
+
expect(resolved?.scale(1)).toBeCloseTo(5, 5);
|
|
54
|
+
expect(resolved?.scale(9)).toBeCloseTo(12, 5);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('clamps data outside an explicit domain to the range endpoints', () => {
|
|
58
|
+
// This is why a size legend must key the DOMAIN, not the data extent: every
|
|
59
|
+
// datum above domain[1] renders at max magnitude and is indistinguishable.
|
|
60
|
+
const { scale } = buildSizeScale(size({ scale: { domain: [0, 5] } }), DATA, SQRT)!;
|
|
61
|
+
expect(scale(9)).toBeCloseTo(scale(5), 5);
|
|
62
|
+
expect(scale(-100)).toBeCloseTo(scale(0), 5);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns null for a degenerate domain (every value identical)', () => {
|
|
66
|
+
// A zero-width domain maps every datum to range[0]. There is no magnitude
|
|
67
|
+
// being encoded, so the caller falls back to its static default rather than
|
|
68
|
+
// rendering a field of identical minimum-size marks.
|
|
69
|
+
expect(buildSizeScale(size(), [{ v: 7 }, { v: 7 }], SQRT)).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('returns null when no value is finite', () => {
|
|
73
|
+
expect(buildSizeScale(size(), [{ v: 'x' }, { v: null }], SQRT)).toBeNull();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('ignores non-finite rows when fitting the domain', () => {
|
|
77
|
+
const resolved = buildSizeScale(size(), [{ v: 1 }, { v: 'nope' }, { v: 9 }], SQRT);
|
|
78
|
+
expect(resolved?.domain).toEqual([1, 9]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('treats a null hole as absent data, not as a zero', () => {
|
|
82
|
+
// `Number(null)` is a finite 0, so a hole that reaches the extent silently
|
|
83
|
+
// drags the domain floor to zero and rescales every genuine mark.
|
|
84
|
+
const resolved = buildSizeScale(size(), [{ v: 10 }, { v: 20 }, { v: null }], SQRT);
|
|
85
|
+
expect(resolved?.domain).toEqual([10, 20]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('treats an empty string the same way (Number("") is also 0)', () => {
|
|
89
|
+
const resolved = buildSizeScale(size(), [{ v: 10 }, { v: 20 }, { v: '' }], SQRT);
|
|
90
|
+
expect(resolved?.domain).toEqual([10, 20]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('returns null when the field is absent from every row', () => {
|
|
94
|
+
// `min([])`/`max([])` are undefined; a `?? 0` / `?? 1` fallback would
|
|
95
|
+
// fabricate a live [0, 1] scale out of data that encodes no size at all.
|
|
96
|
+
expect(buildSizeScale(size(), [{ other: 1 }, { other: 2 }], SQRT)).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('returns null on empty data', () => {
|
|
100
|
+
expect(buildSizeScale(size(), [], SQRT)).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('honours an explicit domain even when the data has no usable extent', () => {
|
|
104
|
+
// The domain is the author's, not the data's, so it stands on its own.
|
|
105
|
+
const resolved = buildSizeScale(size({ scale: { domain: [0, 100] } }), [{ v: null }], SQRT);
|
|
106
|
+
expect(resolved?.domain).toEqual([0, 100]);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -14,12 +14,53 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { ACHROMATIC_RAMP, type Encoding, type ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
17
|
-
import type { ScaleLinear, ScaleOrdinal } from 'd3-scale';
|
|
17
|
+
import type { ScaleLinear, ScaleOrdinal, ScaleQuantile } from 'd3-scale';
|
|
18
18
|
import type { ResolvedScales } from '../layout/scales';
|
|
19
|
+
import { sampleRampColors } from '../legend/continuous';
|
|
19
20
|
|
|
20
21
|
/** Neutral gray applied to muted (non-highlighted) series. */
|
|
21
22
|
const MUTED_COLOR = '#bfc3c8';
|
|
22
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Resolve the categorical color range for a color-scale domain.
|
|
26
|
+
*
|
|
27
|
+
* Single source of truth for categorical color assignment: the ordinal scale
|
|
28
|
+
* (which colors the marks) and the legend (which colors the swatches) both go
|
|
29
|
+
* through this, so highlight muting and the accent-neutral series strategy
|
|
30
|
+
* stay in sync. Callers pass the domain in the same order the scale uses.
|
|
31
|
+
*
|
|
32
|
+
* The result is a d3 ordinal *range*, so callers index it the way d3 does:
|
|
33
|
+
* `colors[domainIndex % colors.length]`. In the pass-through cases the full
|
|
34
|
+
* palette comes back unsliced (d3 cycles it), matching the range the scale
|
|
35
|
+
* carried before this was factored out.
|
|
36
|
+
*/
|
|
37
|
+
export function categoricalColorsForDomain(
|
|
38
|
+
domain: string[],
|
|
39
|
+
theme: ResolvedTheme,
|
|
40
|
+
highlight?: string[],
|
|
41
|
+
): string[] {
|
|
42
|
+
const palette = theme.colors.categorical;
|
|
43
|
+
|
|
44
|
+
if (highlight && highlight.length > 0) {
|
|
45
|
+
const highlightSet = new Set(highlight);
|
|
46
|
+
return domain.map((v, i) => (highlightSet.has(v) ? palette[i % palette.length] : MUTED_COLOR));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const strategy = theme.seriesStrategy ?? 'palette';
|
|
50
|
+
if (strategy !== 'accent-neutral') return palette;
|
|
51
|
+
|
|
52
|
+
const count = domain.length;
|
|
53
|
+
if (count <= 1) return [palette[0]];
|
|
54
|
+
if (count > 4) return palette;
|
|
55
|
+
|
|
56
|
+
// Prominence tracks contrast against the surface: on dark canvases the
|
|
57
|
+
// brightest gray reads strongest, on light canvases the darkest.
|
|
58
|
+
const neutrals = theme.isDark
|
|
59
|
+
? [ACHROMATIC_RAMP.fgMuted, ACHROMATIC_RAMP.fgSubtle, ACHROMATIC_RAMP.fgFaint]
|
|
60
|
+
: [ACHROMATIC_RAMP.fgFaint, ACHROMATIC_RAMP.fgSubtle, ACHROMATIC_RAMP.fgMuted];
|
|
61
|
+
return domain.map((_, i) => (i === 0 ? palette[0] : neutrals[(i - 1) % neutrals.length]));
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
/** Mutates `scales.color.scale.range` in place when no explicit palette was set. */
|
|
24
65
|
export function applyColorScaleRange(
|
|
25
66
|
scales: ResolvedScales,
|
|
@@ -42,39 +83,19 @@ export function applyColorScaleRange(
|
|
|
42
83
|
seqStops[0],
|
|
43
84
|
seqStops[seqStops.length - 1],
|
|
44
85
|
]);
|
|
86
|
+
} else if (
|
|
87
|
+
scales.color.type === 'quantile' ||
|
|
88
|
+
scales.color.type === 'quantize' ||
|
|
89
|
+
scales.color.type === 'threshold'
|
|
90
|
+
) {
|
|
91
|
+
// Binned color scales: replace the placeholder colors with the theme's
|
|
92
|
+
// sequential ramp, sampled to the class count. Same ramp the gradient
|
|
93
|
+
// legend and continuous marks use.
|
|
94
|
+
const seqStops = Object.values(theme.colors.sequential)[0] ?? theme.colors.categorical;
|
|
95
|
+
const scale = scales.color.scale as unknown as ScaleQuantile<string>;
|
|
96
|
+
scale.range(sampleRampColors(seqStops, scale.range().length));
|
|
45
97
|
} else {
|
|
46
98
|
const ordinalScale = scales.color.scale as ScaleOrdinal<string, string>;
|
|
47
|
-
|
|
48
|
-
const domain = ordinalScale.domain();
|
|
49
|
-
|
|
50
|
-
if (highlight && highlight.length > 0) {
|
|
51
|
-
const highlightSet = new Set(highlight);
|
|
52
|
-
const colors = domain.map((v, i) =>
|
|
53
|
-
highlightSet.has(v) ? palette[i % palette.length] : MUTED_COLOR,
|
|
54
|
-
);
|
|
55
|
-
ordinalScale.range(colors);
|
|
56
|
-
} else {
|
|
57
|
-
const strategy = theme.seriesStrategy ?? 'palette';
|
|
58
|
-
if (strategy !== 'accent-neutral') {
|
|
59
|
-
ordinalScale.range(palette);
|
|
60
|
-
} else {
|
|
61
|
-
const count = domain.length;
|
|
62
|
-
if (count <= 1) {
|
|
63
|
-
ordinalScale.range([palette[0]]);
|
|
64
|
-
} else if (count <= 4) {
|
|
65
|
-
// Prominence tracks contrast against the surface: on dark canvases
|
|
66
|
-
// the brightest gray reads strongest, on light canvases the darkest.
|
|
67
|
-
const neutrals = theme.isDark
|
|
68
|
-
? [ACHROMATIC_RAMP.fgMuted, ACHROMATIC_RAMP.fgSubtle, ACHROMATIC_RAMP.fgFaint]
|
|
69
|
-
: [ACHROMATIC_RAMP.fgFaint, ACHROMATIC_RAMP.fgSubtle, ACHROMATIC_RAMP.fgMuted];
|
|
70
|
-
const colors = domain.map((_, i) =>
|
|
71
|
-
i === 0 ? palette[0] : neutrals[(i - 1) % neutrals.length],
|
|
72
|
-
);
|
|
73
|
-
ordinalScale.range(colors);
|
|
74
|
-
} else {
|
|
75
|
-
ordinalScale.range(palette);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
99
|
+
ordinalScale.range(categoricalColorsForDomain(ordinalScale.domain(), theme, highlight));
|
|
79
100
|
}
|
|
80
101
|
}
|
package/src/compile/data-clip.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Data filter for clipped scale domains.
|
|
3
3
|
*
|
|
4
|
-
* When an x or y encoding declares `scale.clip: true
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* When an x or y encoding declares `scale.clip: true`, rows whose field value
|
|
5
|
+
* falls outside the declared domain are dropped before scales and marks are
|
|
6
|
+
* computed. Pure and side-effect free.
|
|
7
|
+
*
|
|
8
|
+
* Two domain forms, two semantics:
|
|
9
|
+
* - Numeric `[lo, hi]`: a RANGE. Rows are kept when `lo <= value <= hi`.
|
|
10
|
+
* - Categorical `string[]`: a MEMBERSHIP SET. Rows are kept when the value is
|
|
11
|
+
* listed. A categorical domain enumerates its categories rather than bounding
|
|
12
|
+
* them, so range comparison would be meaningless (and lexicographic on years
|
|
13
|
+
* would only work by accident).
|
|
7
14
|
*/
|
|
8
15
|
|
|
9
16
|
import type { DataRow, Encoding } from '@opendata-ai/openchart-core';
|
|
@@ -11,8 +18,8 @@ import type { DataRow, Encoding } from '@opendata-ai/openchart-core';
|
|
|
11
18
|
/**
|
|
12
19
|
* Return a new data array with rows outside any clipped scale domain removed.
|
|
13
20
|
*
|
|
14
|
-
* Only inspects the `x` and `y` channels.
|
|
15
|
-
* without `scale.
|
|
21
|
+
* Only inspects the `x` and `y` channels. Channels without `scale.clip` (or
|
|
22
|
+
* without a `scale.domain` to clip to) are passed through unchanged.
|
|
16
23
|
*/
|
|
17
24
|
export function filterClippedDomains(data: DataRow[], encoding: Encoding): DataRow[] {
|
|
18
25
|
let result = data;
|
|
@@ -21,12 +28,20 @@ export function filterClippedDomains(data: DataRow[], encoding: Encoding): DataR
|
|
|
21
28
|
if (!enc?.scale?.clip || !enc.scale.domain) continue;
|
|
22
29
|
const domain = enc.scale.domain;
|
|
23
30
|
const field = enc.field;
|
|
24
|
-
if (Array.isArray(domain)
|
|
31
|
+
if (!Array.isArray(domain)) continue;
|
|
32
|
+
|
|
33
|
+
if (domain.length === 2 && typeof domain[0] === 'number') {
|
|
25
34
|
const [lo, hi] = domain as [number, number];
|
|
26
35
|
result = result.filter((row) => {
|
|
27
36
|
const v = Number(row[field]);
|
|
28
37
|
return Number.isFinite(v) && v >= lo && v <= hi;
|
|
29
38
|
});
|
|
39
|
+
} else if (typeof domain[0] === 'string') {
|
|
40
|
+
// Compare as strings: a categorical domain is authored as string[], but
|
|
41
|
+
// the underlying row value may be a number (e.g. a year column), so
|
|
42
|
+
// `domain.includes(row[field])` would miss on a type mismatch.
|
|
43
|
+
const allowed = new Set(domain as string[]);
|
|
44
|
+
result = result.filter((row) => allowed.has(String(row[field])));
|
|
30
45
|
}
|
|
31
46
|
}
|
|
32
47
|
return result;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-series fill pattern assignment for `mark.fillPattern: 'auto'`.
|
|
3
|
+
*
|
|
4
|
+
* Series identity is the mark's resolved fill color (already theme-resolved
|
|
5
|
+
* by the color scale), so assignment is deterministic: the first distinct
|
|
6
|
+
* fill gets the first pattern shape, the second the next, cycling through
|
|
7
|
+
* the four built-in shapes. The pattern line color is contrast-picked
|
|
8
|
+
* against the series color per mode, so patterns read in light and dark.
|
|
9
|
+
*
|
|
10
|
+
* The renderer turns each ResolvedFillPattern into an SVG `<pattern>` def
|
|
11
|
+
* (see packages/vanilla/src/pattern-utils.ts).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
FillPatternType,
|
|
16
|
+
GradientDef,
|
|
17
|
+
Mark,
|
|
18
|
+
MarkDef,
|
|
19
|
+
ResolvedFillPattern,
|
|
20
|
+
ResolvedTheme,
|
|
21
|
+
} from '@opendata-ai/openchart-core';
|
|
22
|
+
import { getRepresentativeColor, pickLabelColor } from '@opendata-ai/openchart-core';
|
|
23
|
+
|
|
24
|
+
/** Assignment order for series patterns. */
|
|
25
|
+
const PATTERN_SEQUENCE: readonly FillPatternType[] = ['diagonal', 'dot', 'crosshatch', 'vertical'];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Minimum-area rule: a mark keeps its solid fill when its thin dimension is
|
|
29
|
+
* below this many pixels. Pattern tiles are 6px; below ~2 tiles the pattern
|
|
30
|
+
* degrades into noise on thin stacked segments and slivers of pie.
|
|
31
|
+
*/
|
|
32
|
+
const MIN_PATTERN_EXTENT = 12;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Stamp `pattern` onto filled marks (rect, area, arc) when the mark def opts
|
|
36
|
+
* in with `fillPattern: 'auto'`. Mutates marks in place (same convention as
|
|
37
|
+
* assignAnimationIndices). No-op otherwise.
|
|
38
|
+
*/
|
|
39
|
+
export function applyFillPatterns(marks: Mark[], markDef: MarkDef, theme: ResolvedTheme): void {
|
|
40
|
+
if (markDef.fillPattern !== 'auto') return;
|
|
41
|
+
|
|
42
|
+
const assignment = new Map<string, ResolvedFillPattern>();
|
|
43
|
+
const resolve = (fill: string | GradientDef): ResolvedFillPattern => {
|
|
44
|
+
const base = getRepresentativeColor(fill);
|
|
45
|
+
let resolved = assignment.get(base);
|
|
46
|
+
if (!resolved) {
|
|
47
|
+
const type = PATTERN_SEQUENCE[assignment.size % PATTERN_SEQUENCE.length];
|
|
48
|
+
resolved = { type, base, line: pickLabelColor(base, theme.isDark) };
|
|
49
|
+
assignment.set(base, resolved);
|
|
50
|
+
}
|
|
51
|
+
return resolved;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
for (const mark of marks) {
|
|
55
|
+
// Resolve BEFORE any size check so pattern-to-series assignment does not
|
|
56
|
+
// depend on which individual marks are large enough to render one.
|
|
57
|
+
if (mark.type === 'rect') {
|
|
58
|
+
const pattern = resolve(mark.fill);
|
|
59
|
+
if (Math.min(mark.width, mark.height) >= MIN_PATTERN_EXTENT) {
|
|
60
|
+
mark.pattern = pattern;
|
|
61
|
+
}
|
|
62
|
+
} else if (mark.type === 'area') {
|
|
63
|
+
mark.pattern = resolve(mark.fill);
|
|
64
|
+
} else if (mark.type === 'arc') {
|
|
65
|
+
const pattern = resolve(mark.fill);
|
|
66
|
+
const radialExtent = mark.outerRadius - mark.innerRadius;
|
|
67
|
+
const midRadius = (mark.innerRadius + mark.outerRadius) / 2;
|
|
68
|
+
const arcExtent = Math.abs(mark.endAngle - mark.startAngle) * midRadius;
|
|
69
|
+
if (Math.min(radialExtent, arcExtent) >= MIN_PATTERN_EXTENT) {
|
|
70
|
+
mark.pattern = pattern;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|