main.js
3.29 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
import { itemPoint } from '../../constants'
import { getFormated } from '../../utils'
function getRadarLegend (rows, dimension, legendName) {
let legendData = rows.map(row => row[dimension])
return {
data: legendData,
formatter (name) {
return legendName[name] != null ? legendName[name] : name
}
}
}
function getRadarTooltip (dataType, radar, digit) {
const typeTemp = []
const nameTemp = []
radar.indicator.map((item, index) => {
typeTemp[index] = dataType[item.name]
nameTemp[index] = item.name
})
return {
formatter (item) {
const tpl = []
tpl.push(itemPoint(item.color))
tpl.push(`${item.name}<br />`)
item.data.value.forEach((val, index) => {
tpl.push(`${nameTemp[index]}: `)
tpl.push(`${getFormated(val, typeTemp[index], digit)}<br />`)
})
return tpl.join('')
}
}
}
function getRadarSetting (rows, metrics, labelMap) {
const settingBase = {
indicator: [],
shape: 'circle',
splitNumber: 5
}
let indicatorTemp = {}
rows.forEach(items => {
metrics.forEach(item => {
const key = labelMap[item] != null
? labelMap[item]
: item
if (!indicatorTemp[key]) {
indicatorTemp[key] = [items[item]]
} else {
indicatorTemp[key].push(items[item])
}
})
})
settingBase.indicator = Object.keys(indicatorTemp).map(key => {
return {
name: key,
max: Math.max.apply(null, indicatorTemp[key])
}
})
return settingBase
}
function getRadarSeries (args) {
const {
rows,
dimension,
metrics,
radar,
label,
itemStyle,
lineStyle,
labelMap,
areaStyle
} = args
let radarIndexObj = {}
radar.indicator.forEach((item, index) => {
const name = item.name
radarIndexObj[name] = index
})
const seriesData = rows.map(row => {
const serieData = {
value: [],
name: row[dimension]
}
Object.keys(row).forEach(key => {
if (~metrics.indexOf(key)) {
let k = labelMap[key] != null
? radarIndexObj[labelMap[key]]
: radarIndexObj[key]
serieData.value[k] = row[key]
}
})
return serieData
})
const result = {
name: dimension,
type: 'radar',
data: seriesData
}
if (label) result.label = label
if (itemStyle) result.itemStyle = itemStyle
if (lineStyle) result.lineStyle = lineStyle
if (areaStyle) result.areaStyle = areaStyle
return [result]
}
export const radar = (columns, rows, settings, extra) => {
const {
dataType = {},
legendName = {},
labelMap = {},
dimension = columns[0],
digit = 2,
label,
itemStyle,
lineStyle,
areaStyle
} = settings
const { tooltipVisible, legendVisible } = extra
let metrics = columns.slice()
if (settings.metrics) {
metrics = settings.metrics
} else {
metrics.splice(columns.indexOf(dimension), 1)
}
const legend = legendVisible && getRadarLegend(rows, dimension, legendName)
const radar = getRadarSetting(rows, metrics, labelMap)
const tooltip = tooltipVisible && getRadarTooltip(dataType, radar, digit)
const series = getRadarSeries({
rows,
dimension,
metrics,
radar,
label,
itemStyle,
lineStyle,
labelMap,
areaStyle
})
const options = { legend, tooltip, radar, series }
return options
}