main.js
5.69 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
import { itemPoint, HEAT_MAP_COLOR, HEAT_BMAP_COLOR } from '../../constants'
import { getBmap, getAmap, getMapJSON, getFormated } from '../../utils'
import echarts from 'echarts/lib/echarts'
function getAxisList (rows, label) {
const result = []
rows.forEach(row => {
if (!~result.indexOf(row[label])) result.push(row[label])
})
return result
}
function getData (args) {
const { rows, innerXAxisList, innerYAxisList, xDim, yDim, metrics, type, extraMetrics } = args
let result = null
if (type === 'cartesian') {
result = rows.map(row => {
const xIndex = innerXAxisList.indexOf(row[xDim])
const yIndex = innerYAxisList.indexOf(row[yDim])
const value = metrics ? row[metrics] : 1
const extraData = extraMetrics.map(m => row[m] || '-')
return { value: [xIndex, yIndex, value].concat(extraData) }
})
} else {
result = rows.map(row => {
const value = metrics ? row[metrics] : 1
return { value: [row[xDim], row[yDim], value] }
})
}
return result
}
function getAxis (list, name) {
return {
type: 'category',
data: list,
name,
nameLocation: 'end',
splitArea: { show: true }
}
}
function getVisualMap (args) {
const { innerMin: min, innerMax: max, type, heatColor, series } = args
let result = {
min,
max,
calculable: true
}
let extra = null
if (type === 'map') {
extra = {
orient: 'vertical',
left: 0,
bottom: 0,
inRange: { color: heatColor || HEAT_MAP_COLOR }
}
if (!series[0].data.length) extra.show = false
} else if (type === 'bmap' || type === 'amap') {
extra = {
show: false,
orient: 'vertical',
left: 0,
bottom: 0,
inRange: { color: heatColor || HEAT_BMAP_COLOR }
}
} else {
extra = {
orient: 'horizontal',
left: 'center',
bottom: 10,
dimension: 2,
inRange: heatColor && { color: heatColor }
}
}
return Object.assign(result, extra)
}
function getSeries (args) {
const { chartData } = args
return [{
type: 'heatmap',
data: chartData
}]
}
function getTooltip (args) {
const { dataType, innerXAxisList, innerYAxisList, digit, extraMetrics, metrics } = args
return {
trigger: 'item',
formatter ({ color, data: { value: [xDim, yDim, value, ...extraData] } }) {
const tpl = []
tpl.push(`${innerXAxisList[xDim]} ~ ${innerYAxisList[yDim]}<br>`)
extraMetrics.forEach((m, index) => {
tpl.push(`${m}: ${extraData[index]}<br>`)
})
tpl.push(`${itemPoint(color)} ${metrics}: ${getFormated(value, dataType, digit)}<br>`)
return tpl.join('')
}
}
}
export const heatmap = (columns, rows, settings, status) => {
const {
type = 'cartesian', // cartesian, map, bmap,
xAxisList,
yAxisList,
dimension = [columns[0], columns[1]],
metrics = columns[2],
dataType = 'normal',
min,
max,
digit,
bmap,
amap,
geo,
key,
v = '2.0',
position,
positionJsonLink,
beforeRegisterMap,
pointSize = 10,
blurSize = 5,
heatColor,
yAxisName,
xAxisName,
beforeRegisterMapOnce,
mapURLProfix = 'https://unpkg.com/echarts@3.6.2/map/json/',
specialAreas = {}
} = settings
const { tooltipVisible } = status
let innerXAxisList = xAxisList
let innerYAxisList = yAxisList
let chartData = []
// add extraMetrics prop for data which only display in tooltip
const extraMetrics = []
const mainColumn = dimension.concat([metrics])
columns.forEach(column => {
if (!~mainColumn.indexOf(column)) extraMetrics.push(column)
})
if (type === 'cartesian') {
if (!innerXAxisList || !innerXAxisList.length) {
innerXAxisList = getAxisList(rows, dimension[0])
}
if (!innerYAxisList || !innerYAxisList.length) {
innerYAxisList = getAxisList(rows, dimension[1])
}
chartData = getData({
rows,
innerXAxisList,
innerYAxisList,
xDim: dimension[0],
yDim: dimension[1],
metrics,
type,
extraMetrics
})
} else {
chartData = getData({
rows,
xDim: dimension[0],
yDim: dimension[1],
metrics,
type,
extraMetrics
})
}
let metricsList = metrics ? rows.map(row => row[metrics]) : [0, 5]
if (!metricsList.length) metricsList = [0]
const innerMin = min || Math.min.apply(null, metricsList)
const innerMax = max || Math.max.apply(null, metricsList)
const xAxis = getAxis(innerXAxisList, xAxisName)
const yAxis = getAxis(innerYAxisList, yAxisName)
const series = getSeries({ chartData })
const visualMap = getVisualMap({ innerMin, innerMax, type, heatColor, series })
const tooltip = tooltipVisible && getTooltip({
dataType,
innerXAxisList,
innerYAxisList,
digit,
extraMetrics,
metrics
})
const options = { visualMap, series }
if (type === 'bmap') {
Object.assign(options.series[0], { coordinateSystem: 'bmap', pointSize, blurSize })
return getBmap(key, v).then(_ => {
return Object.assign({ bmap }, options)
})
} else if (type === 'map') {
options.series[0].coordinateSystem = 'geo'
return getMapJSON({
position,
positionJsonLink,
beforeRegisterMapOnce,
mapURLProfix
}).then(json => {
const geoAttr = Object.assign({ map: position }, geo)
if (beforeRegisterMap) json = beforeRegisterMap(json)
echarts.registerMap(position, json, specialAreas)
return Object.assign({ geo: geoAttr }, options)
})
} else if (type === 'amap') {
Object.assign(options.series[0], { coordinateSystem: 'amap', pointSize, blurSize })
return getAmap(key, v).then(_ => {
return Object.assign({ amap }, options)
})
} else {
return Object.assign({ xAxis, yAxis, tooltip }, options)
}
}