@opendata-ai/openchart-vanilla 2.7.0 → 2.8.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-vanilla",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "Vanilla JS renderer for openchart: SVG charts, HTML tables, force-directed graphs",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Riley Hilliard",
@@ -49,8 +49,8 @@
49
49
  "typecheck": "tsc --noEmit"
50
50
  },
51
51
  "dependencies": {
52
- "@opendata-ai/openchart-core": "2.7.0",
53
- "@opendata-ai/openchart-engine": "2.7.0",
52
+ "@opendata-ai/openchart-core": "2.8.0",
53
+ "@opendata-ai/openchart-engine": "2.8.0",
54
54
  "d3-force": "^3.0.0",
55
55
  "d3-quadtree": "^3.0.1"
56
56
  },
@@ -354,6 +354,46 @@ describe('chart chrome rendering', () => {
354
354
  expect(fontSize).toBeGreaterThan(0);
355
355
  });
356
356
 
357
+ it('wraps long title text into tspan elements at narrow widths', () => {
358
+ const longTitleSpec: ChartSpec = {
359
+ type: 'bar',
360
+ data: [
361
+ { name: 'A', value: 10 },
362
+ { name: 'B', value: 20 },
363
+ ],
364
+ encoding: {
365
+ x: { field: 'value', type: 'quantitative' },
366
+ y: { field: 'name', type: 'nominal' },
367
+ },
368
+ chrome: {
369
+ title: 'This is a very long chart title that should definitely wrap at narrow widths',
370
+ },
371
+ };
372
+ // Render at a very narrow width to force wrapping
373
+ const { svg } = renderSpec(longTitleSpec, { width: 250, height: 300 });
374
+ const title = svg.querySelector('.viz-title');
375
+ expect(title).not.toBeNull();
376
+ const tspans = title!.querySelectorAll('tspan');
377
+ expect(tspans.length).toBeGreaterThan(1);
378
+ // Full text should be preserved across tspans
379
+ const fullText = Array.from(tspans)
380
+ .map((t) => t.textContent)
381
+ .join(' ');
382
+ expect(fullText).toBe(
383
+ 'This is a very long chart title that should definitely wrap at narrow widths',
384
+ );
385
+ });
386
+
387
+ it('does not wrap short title text', () => {
388
+ const { svg } = renderSpec(lineSpec);
389
+ const title = svg.querySelector('.viz-title');
390
+ expect(title).not.toBeNull();
391
+ // Short title should have no tspan children, just direct textContent
392
+ const tspans = title!.querySelectorAll('tspan');
393
+ expect(tspans.length).toBe(0);
394
+ expect(title!.textContent).toBe('GDP Growth');
395
+ });
396
+
357
397
  it('chart with no chrome specified renders no chrome text elements', () => {
358
398
  const noChrome: ChartSpec = {
359
399
  type: 'bar',
@@ -91,6 +91,50 @@ function applyTextStyle(el: SVGElement, style: TextStyle): void {
91
91
  // Chrome rendering
92
92
  // ---------------------------------------------------------------------------
93
93
 
94
+ /**
95
+ * Break text into lines that fit within maxWidth using word wrapping.
96
+ * Uses a character-width heuristic (same as text-measure.ts).
97
+ */
98
+ function wrapText(text: string, fontSize: number, fontWeight: number, maxWidth: number): string[] {
99
+ if (maxWidth <= 0) return [text];
100
+
101
+ // Heuristic character width matching text-measure.ts
102
+ const AVG_CHAR_WIDTH = 0.55;
103
+ const WEIGHT_FACTORS: Record<number, number> = {
104
+ 100: 0.9,
105
+ 200: 0.92,
106
+ 300: 0.95,
107
+ 400: 1.0,
108
+ 500: 1.02,
109
+ 600: 1.05,
110
+ 700: 1.08,
111
+ 800: 1.1,
112
+ 900: 1.12,
113
+ };
114
+ const weightFactor = WEIGHT_FACTORS[fontWeight] ?? 1.0;
115
+ const charWidth = fontSize * AVG_CHAR_WIDTH * weightFactor;
116
+ const maxChars = Math.floor(maxWidth / charWidth);
117
+
118
+ if (text.length <= maxChars) return [text];
119
+
120
+ const words = text.split(' ');
121
+ const lines: string[] = [];
122
+ let current = '';
123
+
124
+ for (const word of words) {
125
+ const candidate = current ? `${current} ${word}` : word;
126
+ if (candidate.length > maxChars && current) {
127
+ lines.push(current);
128
+ current = word;
129
+ } else {
130
+ current = candidate;
131
+ }
132
+ }
133
+ if (current) lines.push(current);
134
+
135
+ return lines;
136
+ }
137
+
94
138
  function renderChromeElement(
95
139
  parent: SVGElement,
96
140
  element: ResolvedChromeElement,
@@ -102,7 +146,26 @@ function renderChromeElement(
102
146
  applyTextStyle(text, element.style);
103
147
  text.setAttribute('class', className);
104
148
  text.setAttribute('data-chrome-key', chromeKey);
105
- text.textContent = element.text;
149
+
150
+ const lines = wrapText(
151
+ element.text,
152
+ element.style.fontSize,
153
+ element.style.fontWeight,
154
+ element.maxWidth,
155
+ );
156
+
157
+ if (lines.length === 1) {
158
+ text.textContent = element.text;
159
+ } else {
160
+ const lineHeight = element.style.fontSize * (element.style.lineHeight ?? 1.3);
161
+ for (let i = 0; i < lines.length; i++) {
162
+ const tspan = createSVGElement('tspan');
163
+ setAttrs(tspan, { x: element.x, dy: i === 0 ? 0 : lineHeight });
164
+ tspan.textContent = lines[i];
165
+ text.appendChild(tspan);
166
+ }
167
+ }
168
+
106
169
  parent.appendChild(text);
107
170
  }
108
171