/
advisor.ts
464 lines (384 loc) · 13.4 KB
/
advisor.ts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { CKBJson, LevelOfMeasurement as LOM, ChartID as ChartType } from '@antv/knowledge';
import Rules, { Rule, Preferences } from './rules';
import * as DWAnalyzer from '@antv/dw-analyzer';
import { translate } from './util';
import { ChartLibrary, getMappingForLib } from './chartLibMapping';
const Wiki = CKBJson('en-US', true);
/**
* @beta
*/
export interface Channels {
x?: string;
x2?: string;
y?: string;
y2?: string;
color?: string;
angle?: string;
radius?: string;
series?: string;
size?: string;
}
/**
* @beta
*/
export interface Advice {
type: string;
channels: Channels;
score: number;
}
/**
* @public
*/
export interface AdvisorOptions {
/**
* 分析目的
*/
purpose?: string;
/**
* 偏好设置
*/
preferences?: Preferences;
/**
* 标题
*/
title?: string;
/**
* 描述
*/
description?: string;
}
/**
* @beta
*/
export interface FieldInfo extends DWAnalyzer.FieldInfo {
name: string;
levelOfMeasurements: LOM[];
}
function compare(f1: FieldInfo, f2: FieldInfo): number {
if (f1.distinct < f2.distinct) {
return 1;
} else if (f1.distinct > f2.distinct) {
return -1;
} else {
return 0;
}
}
function hasSubset(array1: any[], array2: any[]): boolean {
return array2.every((e) => array1.includes(e));
}
function intersects(array1: any[], array2: any[]): boolean {
return array2.some((e) => array1.includes(e));
}
/**
* Return Data Properties of dataset.
* @beta
*/
export function dataToDataProps(data: any[]): FieldInfo[] {
const dataTypeInfos = DWAnalyzer.typeAll(data);
const dataProps: FieldInfo[] = [];
dataTypeInfos.forEach((info) => {
const lom = [];
if (DWAnalyzer.isNominal(info)) lom.push('Nominal');
if (DWAnalyzer.isOrdinal(info)) lom.push('Ordinal');
if (DWAnalyzer.isInterval(info)) lom.push('Interval');
if (DWAnalyzer.isDiscrete(info)) lom.push('Discrete');
if (DWAnalyzer.isContinuous(info)) lom.push('Continuous');
if (DWAnalyzer.isTime(info)) lom.push('Time');
const newInfo: FieldInfo = { ...info, levelOfMeasurements: lom as LOM[] };
dataProps.push(newInfo);
});
return dataProps;
}
/**
* Return Specification list of recommend charts from data properties.
* @beta
*/
export function dataPropsToSpecs(dataProps: FieldInfo[], options?: AdvisorOptions): Advice[] {
const purpose = options ? options.purpose : '';
const preferences = options ? options.preferences : undefined;
const allTypes = Object.keys(Wiki) as ChartType[];
const list: Advice[] = allTypes.map((t) => {
// anaylze score
let score = 0;
// for log
const record: Record<string, number> = {};
let hardScore = 1;
Rules.filter((r: Rule) => r.hardOrSoft === 'HARD' && r.specChartTypes.includes(t as ChartType)).forEach(
(hr: Rule) => {
const score = hr.check({ dataProps, chartType: t, purpose, preferences });
hardScore *= score;
// console.log('H rule: ', hr.id, ' ; charttype: ', t);
// console.log(score);
record[hr.id] = score;
}
);
let softScore = 0;
Rules.filter((r: Rule) => r.hardOrSoft === 'SOFT' && r.specChartTypes.includes(t as ChartType)).forEach(
(sr: Rule) => {
const score = sr.check({ dataProps, chartType: t, purpose, preferences });
softScore += score;
// console.log('S rule: ', sr.id, ' ; charttype: ', t);
// console.log(score);
record[sr.id] = score;
}
);
score = hardScore * (1 + softScore);
console.log('💯score: ', score, '=', hardScore, '* (1 +', softScore, ') ;charttype: ', t);
console.log(record);
// analyze channels
const channels: Channels = {};
// for Pie | Donut
if (t === 'pie_chart' || t === 'donut_chart') {
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const field4Angle = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4Angle && field4Color) {
channels.color = field4Color.name;
channels.angle = field4Angle.name;
} else {
score = 0;
}
}
// for Line
if (t === 'line_chart' || t == 'step_line_chart') {
const field4X = dataProps.find((field) => intersects(field.levelOfMeasurements, ['Time', 'Ordinal']));
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
if (field4Color) {
channels.color = field4Color.name;
}
if (field4X && field4Y) {
channels.x = field4X.name;
channels.y = field4Y.name;
} else {
score = 0;
}
}
// for Area
if (t === 'area_chart') {
const field4X = dataProps.find((field) => intersects(field.levelOfMeasurements, ['Time', 'Ordinal']));
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X && field4Y) {
channels.x = field4X.name;
channels.y = field4Y.name;
} else {
score = 0;
}
}
// for Bar
if (t === 'bar_chart') {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const sortedNominalFields = nominalFields.sort(compare);
const field4Y = sortedNominalFields[0];
const field4Color = sortedNominalFields[1];
const field4X = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X && field4Y) {
channels.y = field4Y.name;
channels.x = field4X.name;
if (field4Color) {
channels.color = field4Color.name;
}
} else {
score = 0;
}
}
// for Column
if (t === 'column_chart') {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const sortedNominalFields = nominalFields.sort(compare);
const field4X = sortedNominalFields[0];
const field4Color = sortedNominalFields[1];
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X && field4Y) {
channels.y = field4Y.name;
channels.x = field4X.name;
if (field4Color) {
channels.color = field4Color.name;
}
} else {
score = 0;
}
}
// for GroupedBar | StackedBar | PercentageStackedBar
if (t === 'grouped_bar_chart' || t === 'stacked_bar_chart' || t === 'percent_stacked_bar_chart') {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const sortedNominalFields = nominalFields.sort(compare);
const field4Y1 = sortedNominalFields[0];
const field4Y2 = sortedNominalFields[1];
const field4X = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4Y1 && field4Y2 && field4X) {
channels.y = field4Y1.name;
channels.y2 = field4Y2.name;
channels.x = field4X.name;
} else {
score = 0;
}
}
// for GroupedColumn | StackedColumn | PercentageStackedColumn
if (t === 'grouped_column_chart' || t === 'stacked_column_chart' || t === 'percent_stacked_column_chart') {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const sortedNominalFields = nominalFields.sort(compare);
const field4X1 = sortedNominalFields[0];
const field4X2 = sortedNominalFields[1];
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X1 && field4X2 && field4Y) {
channels.x = field4X1.name;
channels.x2 = field4X2.name;
channels.y = field4Y.name;
} else {
score = 0;
}
}
// for StackedArea | PercentageStackedArea
if (t === 'stacked_area_chart' || t === 'percent_stacked_area_chart') {
const field4X1 = dataProps.find((field) => intersects(field.levelOfMeasurements, ['Time', 'Ordinal']));
const field4X2 = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X1 && field4X2 && field4Y) {
channels.x = field4X1.name;
channels.x2 = field4X2.name;
channels.y = field4Y.name;
} else {
score = 0;
}
}
// for Radar
if (t === 'radar_chart') {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const sortedNominalFields = nominalFields.sort(compare);
const field4Angle = sortedNominalFields[0];
const field4Series = sortedNominalFields[1];
const field4Radius = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4Angle && field4Series && field4Radius) {
channels.angle = field4Angle.name;
channels.series = field4Series.name;
channels.radius = field4Radius.name;
} else {
score = 0;
}
}
// for Scatter
if (t === 'scatter_plot') {
const intervalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
const sortedIntervalFields = intervalFields.sort(compare);
const field4X = sortedIntervalFields[0];
const field4Y = sortedIntervalFields[1];
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
if (field4X && field4Y) {
channels.x = field4X.name;
channels.y = field4Y.name;
if (field4Color) {
channels.color = field4Color.name;
}
} else {
score = 0;
}
}
// for Bubble
if (t === 'bubble_chart') {
const intervalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
const triple = {
x: intervalFields[0],
y: intervalFields[1],
corr: 0,
size: intervalFields[2],
};
for (let i = 0; i < intervalFields.length; i++) {
for (let j = i + 1; j < intervalFields.length; j++) {
const p = DWAnalyzer.pearson(intervalFields[i], intervalFields[j]);
if (Math.abs(p) > triple.corr) {
triple.x = intervalFields[i];
triple.y = intervalFields[j];
triple.corr = p;
triple.size = intervalFields[[...Array(intervalFields.length).keys()].find((e) => e !== i && e !== j) || 0];
}
}
}
const field4X = triple.x;
const field4Y = triple.y;
const field4Size = triple.size;
const field4Color = dataProps.find((field) => intersects(field.levelOfMeasurements, ['Nominal']));
if (field4X && field4Y && field4Size && field4Color) {
channels.x = field4X.name;
channels.y = field4Y.name;
channels.size = field4Size.name;
channels.color = field4Color.name;
} else {
score = 0;
}
}
// for Histogram
if (t === 'histogram') {
const field = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field) {
channels.x = field.name;
} else {
score = 0;
}
}
// for heatmap
if (t === 'heatmap') {
const axisFields = dataProps.filter((field) => intersects(field.levelOfMeasurements, ['Nominal', 'Ordinal']));
const sortedFields = axisFields.sort(compare);
const field4X = sortedFields[0];
const field4Y = sortedFields[1];
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
if (field4X && field4Y && field4Color) {
channels.x = field4X.name;
channels.y = field4Y.name;
channels.color = field4Color.name;
} else {
score = 0;
}
}
return {
type: t,
channels,
score,
};
});
// sort list
function compareAdvices(chart1: Advice, chart2: Advice) {
if (chart1.score < chart2.score) {
return 1;
} else if (chart1.score > chart2.score) {
return -1;
} else {
return 0;
}
}
const resultList = list.filter((e) => e.score && e.score !== 0 && translate(e.type)).sort(compareAdvices);
console.log('🍒🍒🍒🍒🍒🍒 resultList 🍒🍒🍒🍒🍒🍒');
console.log(resultList);
return resultList;
}
/**
* Return Specification list of recommend charts from dataset.
* @todo rename it as `dataToSpecs` and export it
*/
export function analyze(data: any[], options?: AdvisorOptions): Advice[] {
console.log('💠💠💠💠💠💠 data 💠💠💠💠💠💠');
console.log(data);
console.log('🍯🍯🍯🍯🍯🍯 options 🍯🍯🍯🍯🍯🍯');
console.log(options);
const dataProps = dataToDataProps(data);
console.log('🔶🔶🔶🔶🔶🔶 dataset analysis 🔶🔶🔶🔶🔶🔶');
console.log(dataProps);
const adviceList: Advice[] = dataPropsToSpecs(dataProps, options);
return adviceList;
}
/**
* Return configs for specific charting library from advice.
* @beta
*/
export function specToLibConfig(advice: Advice, libraryName: ChartLibrary) {
const mapping = getMappingForLib(libraryName);
const { type, channels } = advice;
const configs: any = {};
for (const [key, value] of Object.entries(channels)) {
const channel = mapping[type][key as keyof Channels];
if (channel) {
configs[channel] = value;
}
}
return configs;
}