main.js
5.42 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
import { getFormated, getStackMap } from '../../utils'
import { isArray } from 'utils-lite'
function getLineXAxis (args) {
const { dimension, rows, xAxisName, axisVisible, xAxisType } = args
return dimension.map((item, index) => ({
type: xAxisType,
nameLocation: 'middle',
nameGap: 22,
name: xAxisName[index] || '',
axisTick: { show: true, lineStyle: { color: '#eee' } },
data: rows.map(row => row[item]),
show: axisVisible
}))
}
function getLineSeries (args) {
const {
rows,
axisSite,
metrics,
area,
stack,
nullAddZero,
labelMap,
label,
itemStyle,
lineStyle,
areaStyle,
dimension
} = args
let series = []
const dataTemp = {}
const stackMap = stack && getStackMap(stack)
metrics.forEach(item => { dataTemp[item] = [] })
rows.forEach(row => {
metrics.forEach(item => {
let value = null
if (row[item] != null) {
value = row[item]
} else if (nullAddZero) {
value = 0
}
dataTemp[item].push([row[dimension[0]], value])
})
})
metrics.forEach(item => {
let seriesItem = {
name: labelMap[item] != null ? labelMap[item] : item,
type: 'line',
data: dataTemp[item]
}
if (area) seriesItem.areaStyle = { normal: {} }
if (axisSite.right) {
seriesItem.yAxisIndex = ~axisSite.right.indexOf(item) ? 1 : 0
}
if (stack && stackMap[item]) seriesItem.stack = stackMap[item]
if (label) seriesItem.label = label
if (itemStyle) seriesItem.itemStyle = itemStyle
if (lineStyle) seriesItem.lineStyle = lineStyle
if (areaStyle) seriesItem.areaStyle = areaStyle
series.push(seriesItem)
})
return series
}
function getLineYAxis (args) {
const {
yAxisName,
yAxisType,
axisVisible,
scale,
min,
max,
digit
} = args
const yAxisBase = {
type: 'value',
axisTick: {
show: false
},
show: axisVisible
}
let yAxis = []
for (let i = 0; i < 2; i++) {
if (yAxisType[i]) {
yAxis[i] = Object.assign({}, yAxisBase, {
axisLabel: {
formatter (val) {
return getFormated(val, yAxisType[i], digit)
}
}
})
} else {
yAxis[i] = Object.assign({}, yAxisBase)
}
yAxis[i].name = yAxisName[i] || ''
yAxis[i].scale = scale[i] || false
yAxis[i].min = min[i] || null
yAxis[i].max = max[i] || null
}
return yAxis
}
function getLineTooltip (args) {
const { axisSite, yAxisType, digit, labelMap, tooltipFormatter } = args
const rightItems = axisSite.right || []
const rightList = labelMap
? rightItems.map(item => {
return labelMap[item] === undefined ? item : labelMap[item]
})
: rightItems
return {
trigger: 'axis',
formatter (items) {
if (tooltipFormatter) {
return tooltipFormatter.apply(null, arguments)
}
let tpl = []
const { name, axisValueLabel } = items[0]
const title = name || axisValueLabel
tpl.push(`${title}<br>`)
items.forEach(({ seriesName, data, marker }) => {
let showData = null
const type = ~rightList.indexOf(seriesName) ? yAxisType[1] : yAxisType[0]
const itemData = isArray(data) ? data[1] : data
showData = getFormated(itemData, type, digit)
tpl.push(marker)
tpl.push(`${seriesName}: ${showData}`)
tpl.push('<br>')
})
return tpl.join('')
}
}
}
function getLegend (args) {
const { metrics, legendName, labelMap } = args
if (!legendName && !labelMap) return { data: metrics }
const data = labelMap
? metrics.map(item => (labelMap[item] == null ? item : labelMap[item]))
: metrics
return {
data,
formatter (name) {
return legendName[name] != null ? legendName[name] : name
}
}
}
export const line = (columns, rows, settings, extra) => {
rows = isArray(rows) ? rows : []
columns = isArray(columns) ? columns : []
const {
axisSite = {},
yAxisType = ['normal', 'normal'],
xAxisType = 'category',
yAxisName = [],
dimension = [columns[0]],
xAxisName = [],
axisVisible = true,
area,
stack,
scale = [false, false],
min = [null, null],
max = [null, null],
nullAddZero = false,
digit = 2,
legendName = {},
labelMap = {},
label,
itemStyle,
lineStyle,
areaStyle
} = settings
const { tooltipVisible, legendVisible, tooltipFormatter } = extra
let metrics = columns.slice()
if (axisSite.left && axisSite.right) {
metrics = axisSite.left.concat(axisSite.right)
} else if (axisSite.left && !axisSite.right) {
metrics = axisSite.left
} else if (settings.metrics) {
metrics = settings.metrics
} else {
metrics.splice(columns.indexOf(dimension[0]), 1)
}
const legend = legendVisible && getLegend({ metrics, legendName, labelMap })
const tooltip = tooltipVisible && getLineTooltip({
axisSite,
yAxisType,
digit,
labelMap,
xAxisType,
tooltipFormatter
})
const xAxis = getLineXAxis({
dimension,
rows,
xAxisName,
axisVisible,
xAxisType
})
const yAxis = getLineYAxis({
yAxisName,
yAxisType,
axisVisible,
scale,
min,
max,
digit
})
const series = getLineSeries({
rows,
axisSite,
metrics,
area,
stack,
nullAddZero,
labelMap,
label,
itemStyle,
lineStyle,
areaStyle,
xAxisType,
dimension
})
let options = { legend, xAxis, series, yAxis, tooltip }
return options
}