main.js
1.75 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
import { isArray } from 'utils-lite'
function getSeries (args) {
const {
dimension,
metrics,
rows,
color,
sizeMax,
sizeMin,
shape
} = args
const baseType = {
type: 'wordCloud',
textStyle: {
normal: {
color: !isArray(color) && !!color ? color : () => {
return 'rgb(' + [
Math.round(Math.random() * 160),
Math.round(Math.random() * 160),
Math.round(Math.random() * 160)
].join(',') + ')'
}
}
},
shape: shape,
sizeRange: [sizeMin, sizeMax]
}
const len = isArray(color) ? color.length : 0
const data = rows.slice().map(row => {
const text = {
name: row[dimension],
value: row[metrics]
}
if (len > 0) {
text.textStyle = {
normal: {
color: color[Math.floor(Math.random() * len)]
}
}
}
return text
})
baseType.data = data
return [baseType]
}
function getTooltip (args) {
const { tooltipFormatter } = args
return {
show: true,
formatter (params) {
const {
data: {
name,
value
}
} = params
if (tooltipFormatter) {
return tooltipFormatter.apply(null, params)
}
return `${name}: ${value}`
}
}
}
export const wordcloud = (columns, rows, settings, extra) => {
const {
dimension = columns[0],
metrics = columns[1],
color = '',
sizeMax = 60,
sizeMin = 12,
shape = 'circle'
} = settings
const {
tooltipVisible,
tooltipFormatter
} = extra
const series = getSeries({ dimension, metrics, rows, color, sizeMax, sizeMin, shape })
const tooltip = tooltipVisible && getTooltip({ tooltipFormatter })
return {
series,
tooltip
}
}