-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathgetChartSpecWithContext_circlePacking.test.ts
More file actions
240 lines (215 loc) · 7.62 KB
/
getChartSpecWithContext_circlePacking.test.ts
File metadata and controls
240 lines (215 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { Dict } from '@visactor/vutils';
import { getChartSpecWithContext } from '../../src/atom/chartGenerator/spec';
import type { GenerateChartCellContext } from '../../src/atom/chartGenerator/type';
import { ChartType } from '../../src/types';
import { builtinThemeMap } from '../../src/atom/chartGenerator/const';
import { BuiltinThemeType } from '../../src/atom/chartGenerator/const';
import { COLOR_THEMES } from '../../src/atom/chartGenerator/spec/constants';
const CHART_TYPE_LIST = [
'Bar Chart',
'Line Chart',
'Area Chart',
'Pie Chart',
'Scatter Plot',
'Word Cloud',
'Rose Chart',
'Radar Chart',
'Sankey Chart',
'Funnel Chart',
'Dual Axis Chart',
'Waterfall Chart',
'Box Plot',
'Linear Progress chart',
'Circular Progress chart',
'Liquid Chart',
'Bubble Circle Packing',
'Map Chart',
'Range Column Chart',
'Sunburst Chart',
'Treemap Chart',
'Gauge Chart',
'Basic Heat Map',
'Venn Chart',
'Dynamic Bar Chart'
];
const generateRandomValue = (min = 100, max = 2000) => Math.floor(Math.random() * (max - min + 1)) + min;
function generateDataStructure() {
const createProducts = () => [
{ name: 'Office Supplies', value: generateRandomValue() },
{ name: 'Furniture', value: generateRandomValue() },
{ name: 'Electronic equipment', value: generateRandomValue() }
];
const createRegions = (count: number) =>
Array.from({ length: count }, (_, i) => ({
name: `Region${i + 1}`,
children: createProducts()
}));
const createCountries = (names: string[]) =>
names.map(country => ({
name: country,
children: createRegions(6)
}));
return [
{
name: 'root',
children: createCountries(['Country A', 'Country B', 'Country C'])
}
];
}
const dataItem = generateDataStructure();
describe('getChartSpecWithContext', () => {
const baseContext = {
chartTypeList: CHART_TYPE_LIST,
cell: { x: 'category', size: 'value' },
dataTable: dataItem,
chartType: ChartType.BubbleCirclePacking.toUpperCase(),
totalTime: 5
};
it('should generate full spec for circlepacking chart with diverse inputs', () => {
const context = {
...baseContext,
chartTheme: 'semiThemeLight',
simpleVChartSpec: {
type: 'circlePacking',
title: [
{ text: 'CirclePacking Chart-1', orient: 'top' },
{ text: 'CirclePacking Chart-2', orient: 'left' }
],
dataZoom: [{ orient: 'left' }, { orient: 'bottom' }],
label: [{ position: 'top', style: { fill: 'black' } }],
indicator: [
{
title: 'Cluster A',
content: ['Overlap A']
}
],
palette: ['#123456', '#abcdef'],
background: '#ffffff'
}
};
const result = getChartSpecWithContext(context);
expect(result.spec).toBeDefined();
expect(result.spec.type).toBe('circlePacking');
expect(result.spec.data.values).toEqual(dataItem);
expect(result.spec.color).toEqual(['#123456', '#abcdef']);
expect(result.spec.background).toEqual('#ffffff');
expect(result.spec.title).toEqual([
{ text: 'CirclePacking Chart-1', orient: 'top' },
{ text: 'CirclePacking Chart-2', orient: 'left' }
]);
expect(result.spec.label).toEqual([{ position: 'top', style: { fill: 'black' }, visible: true }]);
expect(result.spec.indicator).toEqual([
{
title: { style: { text: 'Cluster A' } },
content: [{ style: { text: 'Overlap A' } }]
}
]);
expect(result.spec.dataZoom).toEqual([{ orient: 'left' }, { orient: 'bottom' }]);
expect(result.spec.valueField).toEqual('value');
expect(result.spec.categoryField).toEqual('category');
expect(result.spec.drill).toEqual(true);
expect(result.spec.layoutPadding).toEqual(5);
expect(result.spec.animationEnter).toEqual({ easing: 'cubicInOut' });
expect(result.spec.animationExit).toEqual({ easing: 'cubicInOut' });
expect(result.spec.animationUpdate).toEqual({ easing: 'cubicInOut' });
expect(result.spec.theme).toEqual(builtinThemeMap[BuiltinThemeType.SemiThemeLight]);
});
it('should apply default colors if colors and palette are not given', () => {
const context = { ...baseContext };
const result = getChartSpecWithContext(context);
expect(result.spec.color).toBeDefined();
expect(result.spec.color).toEqual(COLOR_THEMES.default);
});
it('should apply custom colors if colors are given', () => {
const context = {
...baseContext,
colors: ['#f57c6e', '#f2b56f', '#f2a7da', '#84c3b7', '#88d8db', '#71b7ed', '#b8aeeb', '#f2a7da', '#fae69e']
};
const result = getChartSpecWithContext(context);
expect(result.spec.color).toBeDefined();
expect(result.spec.color).toEqual([
'#f57c6e',
'#f2b56f',
'#f2a7da',
'#84c3b7',
'#88d8db',
'#71b7ed',
'#b8aeeb',
'#f2a7da',
'#fae69e'
]);
});
it('should apply string theme and colors are undefined', () => {
const context = {
...baseContext,
chartTheme: 'semiThemeLight',
colors: ['#f57c6e', '#f2b56f', '#f2a7da', '#84c3b7', '#88d8db', '#71b7ed', '#b8aeeb', '#f2a7da', '#fae69e']
};
const result = getChartSpecWithContext(context);
expect(result.spec.theme).toEqual(builtinThemeMap[BuiltinThemeType.SemiThemeLight]);
expect(result.spec.color).toBeUndefined();
});
it('should apply custom object theme and colors are undefined', () => {
const context = {
...baseContext,
chartTheme: { colorScheme: ['#1a2b3c'] },
colors: ['#f57c6e', '#f2b56f', '#f2a7da', '#84c3b7', '#88d8db', '#71b7ed', '#b8aeeb', '#f2a7da', '#fae69e']
};
const result = getChartSpecWithContext(context);
expect(result.spec.theme).toEqual({ colorScheme: ['#1a2b3c'] });
expect(result.spec.color).toBeUndefined();
});
// test for `bubbleCirclePackingData`
it('should preserve "value" field when size is "value"', () => {
const context = {
chartTypeList: CHART_TYPE_LIST,
cell: { x: 'category', size: 'other' },
chartType: ChartType.BubbleCirclePacking.toUpperCase(),
totalTime: 5,
dataTable: [
{ category: 'A', value: 20, other: 5 },
{ category: 'B', value: 15, other: 3 }
]
};
const result = getChartSpecWithContext(context);
expect(result.spec.data.values).toMatchObject([
{ category: 'A', value: 5 },
{ category: 'B', value: 3 }
]);
});
// test for `bubbleCirclePackingField`
it('should generate correct categoryField', () => {
const context = {
chartTypeList: CHART_TYPE_LIST,
cell: { color: 'category', size: 'value', x: 'hahaha' },
chartType: ChartType.BubbleCirclePacking.toUpperCase(),
totalTime: 5,
dataTable: [
{ category: 'A', value: 20 },
{ category: 'B', value: 15 }
]
};
const result = getChartSpecWithContext(context);
expect(result.spec.categoryField).toEqual(context.cell.color || context.cell.x);
});
it('should handle missing dataTable fields gracefully', () => {
const context = {
chartType: ChartType.BubbleCirclePacking.toUpperCase(),
cell: { x: 'name', size: 'value' },
dataTable: [{}]
};
const result = getChartSpecWithContext(context);
expect(result.spec).toBeDefined();
expect(result.spec.type).toBe('circlePacking');
});
it('should handle missing cell fields gracefully', () => {
const context = {
chartType: ChartType.BubbleCirclePacking.toUpperCase(),
cell: { x: 'name', size: 'value' },
dataTable: dataItem
};
const result = getChartSpecWithContext(context);
expect(result.spec).toBeDefined();
expect(result.spec.type).toBe('circlePacking');
});
});