Skip to content

Commit 6c6b52b

Browse files
authored
fix(chart-wordcloud): use the datum index for data-indexes (#251)
* fix(chart-wordcloud): use the datum index for data-indexes The word-cloud structure tagged each word label with `data-indexes={index}` where `index` was the position in `placedWords` — a list sorted by font size descending. The original datum index was captured for the word's color but never carried onto the WordCandidate, so labels were tagged with their sorted render position instead of the data item they represent. Every other structure/item uses the true datum index (e.g. chart-pie: `data-indexes={[originalIndex]}`). The editor reads `data-indexes` to route edits (`getIndexesFromElement` -> `updateItemDatum`), so double-click-renaming a word wrote to the wrong item whenever values weren't already in descending order — silent data corruption. Carry the datum index onto the word and emit `data-indexes={[word.index]}`. The sort-by-size layout is unchanged. * test: cover word cloud data-indexes mapping Add a regression test asserting each word label carries its datum index rather than the font-size sort position. Data is intentionally out of order (Apple:10, Banana:50, Cherry:30) so the size-sorted render order (Banana, Cherry, Apple) differs from the datum indices (1, 2, 0), which is exactly the case the fix corrects.
1 parent 6cfe0bf commit 6c6b52b

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** @jsxImportSource ../../../../src */
2+
import { describe, expect, it } from 'vitest';
3+
import type { ComponentType, ParsedInfographicOptions } from '../../../../src';
4+
import { Rect, renderSVG } from '../../../../src';
5+
import type { BaseItemProps } from '../../../../src/designs/items';
6+
import { ChartWordCloud } from '../../../../src/designs/structures/chart-wordcloud';
7+
import type { ParsedData } from '../../../../src/types';
8+
9+
const Item: ComponentType<
10+
Omit<BaseItemProps, 'themeColors'> &
11+
Partial<Pick<BaseItemProps, 'themeColors'>>
12+
> = ({ x = 0, y = 0 }) => <Rect x={x} y={y} width={20} height={10} />;
13+
14+
describe('ChartWordCloud', () => {
15+
it('tags each word with its datum index, not the font-size sort position', () => {
16+
// Values are intentionally out of order: the size-sort (desc) renders
17+
// Banana, Cherry, Apple, but data-indexes must stay 1, 2, 0 (the original
18+
// item indices) so the editor routes edits to the correct datum.
19+
const data = {
20+
items: [
21+
{ label: 'Apple', value: 10 },
22+
{ label: 'Banana', value: 50 },
23+
{ label: 'Cherry', value: 30 },
24+
],
25+
} as ParsedData;
26+
const options = {
27+
data,
28+
themeConfig: {
29+
colorBg: '#ffffff',
30+
colorPrimary: '#1677ff',
31+
},
32+
} as ParsedInfographicOptions;
33+
34+
const svg = renderSVG(
35+
<ChartWordCloud
36+
Item={Item}
37+
Items={[]}
38+
data={data}
39+
options={options}
40+
enableRotate={false}
41+
/>,
42+
);
43+
44+
const dataIndexFor = (label: string): string | undefined => {
45+
const re = new RegExp(
46+
`data-indexes="([^"]*)"[^>]*>(?:<[^>]*>)*\\s*${label}\\s*<`,
47+
);
48+
return svg.match(re)?.[1];
49+
};
50+
51+
expect(dataIndexFor('Banana')).toBe('1');
52+
expect(dataIndexFor('Cherry')).toBe('2');
53+
expect(dataIndexFor('Apple')).toBe('0');
54+
});
55+
});

‎src/designs/structures/chart-wordcloud.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface WordCandidate {
1414
fontSize: number;
1515
width: number;
1616
height: number;
17+
index: number;
1718
}
1819

1920
interface PlacedWord extends WordCandidate {
@@ -208,6 +209,7 @@ export const ChartWordCloud: ComponentType<ChartWordCloudProps> = (props) => {
208209
getColorPrimary(options) ||
209210
'#333333';
210211
return {
212+
index,
211213
label: datum.label as string,
212214
value: datum.value,
213215
color,
@@ -235,7 +237,7 @@ export const ChartWordCloud: ComponentType<ChartWordCloudProps> = (props) => {
235237
const containerWidth = maxX - minX + padding * 2;
236238
const containerHeight = maxY - minY + padding * 2;
237239

238-
const wordElements: JSXElement[] = placedWords.map((word, index) => {
240+
const wordElements: JSXElement[] = placedWords.map((word) => {
239241
const translateX = word.centerX - word.width / 2 + offsetX;
240242
const translateY = word.centerY - word.height / 2 + offsetY;
241243
const rotationOriginX = word.width / 2;
@@ -252,7 +254,7 @@ export const ChartWordCloud: ComponentType<ChartWordCloudProps> = (props) => {
252254
alignHorizontal="center"
253255
alignVertical="middle"
254256
fill={word.color}
255-
data-indexes={index}
257+
data-indexes={[word.index]}
256258
data-element-type={ElementTypeEnum.ItemLabel}
257259
>
258260
{word.label}

0 commit comments

Comments
 (0)