@opendata-ai/openchart-engine 7.4.1 → 7.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opendata-ai/openchart-engine",
3
- "version": "7.4.1",
3
+ "version": "7.5.0",
4
4
  "description": "Headless compiler for openchart: spec validation, data compilation, scales, and layout",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Riley Hilliard",
@@ -48,7 +48,7 @@
48
48
  "typecheck": "tsc --noEmit"
49
49
  },
50
50
  "dependencies": {
51
- "@opendata-ai/openchart-core": "7.4.1",
51
+ "@opendata-ai/openchart-core": "7.5.0",
52
52
  "d3-array": "^3.2.0",
53
53
  "d3-format": "^3.1.2",
54
54
  "d3-interpolate": "^3.0.0",
@@ -179,6 +179,40 @@ describe('computeScatterMarks', () => {
179
179
 
180
180
  expect(marks[0].aria.label).toContain('population=');
181
181
  });
182
+
183
+ it('explicit size domain keeps the largest datum below the max radius', () => {
184
+ const base = makeBubbleSpec();
185
+ // Widen the domain past the data max (8000) so nothing reaches radius 30.
186
+ const spec: NormalizedChartSpec = {
187
+ ...base,
188
+ encoding: {
189
+ ...base.encoding,
190
+ size: { field: 'population', type: 'quantitative', scale: { domain: [0, 32000] } },
191
+ },
192
+ };
193
+ const scales = computeScales(spec, chartArea, spec.data);
194
+ const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
195
+
196
+ const largest = marks.find((m) => m.data.population === 8000)!;
197
+ // sqrt(8000/32000) * 30 = 0.5 * 30 = 15, well under the 30px cap.
198
+ expect(largest.r).toBeLessThan(20);
199
+ });
200
+
201
+ it('explicit size range caps the radii', () => {
202
+ const base = makeBubbleSpec();
203
+ const spec: NormalizedChartSpec = {
204
+ ...base,
205
+ encoding: {
206
+ ...base.encoding,
207
+ size: { field: 'population', type: 'quantitative', scale: { range: [2, 12] } },
208
+ },
209
+ };
210
+ const scales = computeScales(spec, chartArea, spec.data);
211
+ const marks = computeScatterMarks(spec, scales, chartArea, fullStrategy);
212
+
213
+ const maxRadius = Math.max(...marks.map((m) => m.r));
214
+ expect(maxRadius).toBeLessThanOrEqual(12);
215
+ });
182
216
  });
183
217
 
184
218
  describe('color encoding', () => {
@@ -113,19 +113,24 @@ export function computeScatterMarks(
113
113
  const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
114
114
  const isSequentialColor = colorEnc?.type === 'quantitative';
115
115
  const colorField = colorEnc?.field;
116
- const sizeField = encoding.size && 'field' in encoding.size ? encoding.size.field : undefined;
116
+ const sizeEnc = encoding.size && 'field' in encoding.size ? encoding.size : undefined;
117
+ const sizeField = sizeEnc?.field;
117
118
 
118
- // Build a size scale for bubble variant
119
+ // Build a size scale for bubble variant. Domain and radius range are
120
+ // author-overridable via `encoding.size.scale.{domain,range}`: an explicit
121
+ // domain (e.g. [0, 900]) keeps the largest datum below the max radius so
122
+ // dense clusters don't blob together, and an explicit range caps the radii.
119
123
  let sizeScale: ((v: number) => number) | undefined;
120
124
  if (sizeField) {
121
125
  const sizeValues = spec.data.map((d) => Number(d[sizeField])).filter((v) => Number.isFinite(v));
122
126
 
123
- const sizeMin = min(sizeValues) ?? 0;
124
- const sizeMax = max(sizeValues) ?? 1;
127
+ const explicitDomain = sizeEnc?.scale?.domain as [number, number] | undefined;
128
+ const [sizeMin, sizeMax] = explicitDomain ?? [min(sizeValues) ?? 0, max(sizeValues) ?? 1];
125
129
 
126
- sizeScale = scaleSqrt()
127
- .domain([sizeMin, sizeMax])
128
- .range([MIN_BUBBLE_RADIUS, MAX_BUBBLE_RADIUS]);
130
+ const explicitRange = sizeEnc?.scale?.range as [number, number] | undefined;
131
+ const [radiusMin, radiusMax] = explicitRange ?? [MIN_BUBBLE_RADIUS, MAX_BUBBLE_RADIUS];
132
+
133
+ sizeScale = scaleSqrt().domain([sizeMin, sizeMax]).range([radiusMin, radiusMax]).clamp(true);
129
134
  }
130
135
 
131
136
  const marks: PointMark[] = [];