main.js
3.82 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
import { getFormated } from '../../utils'
function getWaterfallTooltip (dataType, digit) {
return {
trigger: 'axis',
axisPointer: { type: 'shadow' },
formatter (items) {
const item = items[1]
return [
`${item.name}<br/>${item.seriesName} :`,
`${getFormated(item.value, dataType, digit)}`
].join('')
}
}
}
function getWaterfallXAxis (args) {
const {
dimension,
rows,
remainStatus,
totalName,
remainName,
labelMap,
xAxisName,
axisVisible
} = args
let xAxisData = [totalName].concat(rows.map(row => row[dimension]))
if (remainStatus === 'have-remain') {
xAxisData = xAxisData.concat([remainName])
}
return {
type: 'category',
name: labelMap && labelMap[xAxisName] || xAxisName,
splitLine: { show: false },
data: xAxisData,
show: axisVisible
}
}
function getWaterfallYAxis (args) {
const { dataType, yAxisName, axisVisible, digit, labelMap } = args
return {
type: 'value',
name: labelMap[yAxisName] != null ? labelMap[yAxisName] : yAxisName,
axisTick: { show: false },
axisLabel: {
formatter (val) {
return getFormated(val, dataType, digit)
}
},
show: axisVisible
}
}
function getWaterfallSeries (args) {
const {
dataType,
rows,
metrics,
totalNum,
remainStatus,
dataSum,
digit
} = args
const seriesBase = { type: 'bar', stack: '总量' }
let dataSumTemp = dataSum
let totalNumTemp = totalNum
let assistData
let mainData
const rowData = rows.map(row => row[metrics])
if (remainStatus === 'have-remain') {
assistData = [0].concat(rows.map(row => {
totalNumTemp -= row[metrics]
return totalNumTemp
})).concat([0])
mainData = [totalNum].concat(rowData).concat([totalNum - dataSum])
} else {
assistData = [0].concat(rows.map(row => {
dataSumTemp -= row[metrics]
return dataSumTemp
}))
mainData = [dataSum].concat(rowData)
}
const series = []
series.push(Object.assign({
name: '辅助',
itemStyle: {
normal: { opacity: 0 },
emphasis: { opacity: 0 }
},
data: assistData
}, seriesBase))
series.push(Object.assign({
name: '数值',
label: {
normal: {
show: true,
position: 'top',
formatter (item) {
return getFormated(item.value, dataType, digit)
}
}
},
data: mainData
}, seriesBase))
return series
}
function getWaterfallRemainStatus (dataSum, totalNum) {
if (!totalNum) return 'not-total'
return totalNum > dataSum ? 'have-remain' : 'none-remain'
}
export const waterfall = (columns, rows, settings, extra) => {
const {
dataType = 'normal',
dimension = columns[0],
totalName = '总计',
totalNum,
remainName = '其他',
xAxisName = dimension,
labelMap = {},
axisVisible = true,
digit = 2
} = settings
const { tooltipVisible } = extra
let metricsTemp = columns.slice()
metricsTemp.splice(metricsTemp.indexOf(dimension), 1)
const metrics = metricsTemp[0]
const yAxisName = metrics
const tooltip = tooltipVisible && getWaterfallTooltip(dataType, digit)
const dataSum = parseFloat(rows.reduce((pre, cur) => {
return pre + Number(cur[metrics])
}, 0).toFixed(digit))
const remainStatus = getWaterfallRemainStatus(dataSum, totalNum)
const xAxisParams = {
dimension,
rows,
remainStatus,
totalName,
remainName,
xAxisName,
labelMap,
axisVisible
}
const xAxis = getWaterfallXAxis(xAxisParams)
const yAxis = getWaterfallYAxis({ dataType, yAxisName, axisVisible, digit, labelMap })
const seriesParams = {
dataType,
rows,
dimension,
metrics,
totalNum,
remainStatus,
dataSum,
digit
}
const series = getWaterfallSeries(seriesParams)
const options = { tooltip, xAxis, yAxis, series }
return options
}