main.js
2.22 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
import { getFormated } from '../../utils'
import { itemPoint } from '../../constants'
function getTooltip (args) {
const {
itemDataType,
linksDataType,
digit
} = args
return {
trigger: 'item',
formatter (item) {
const tpl = []
const { name, data, value, color } = item
tpl.push(itemPoint(color))
tpl.push(`${name} : `)
if (data && data.source) {
tpl.push(`${getFormated(value, linksDataType, digit)}<br />`)
} else {
tpl.push(`${getFormated(value, itemDataType, digit)}<br />`)
}
return tpl.join('')
}
}
}
function getSeries (args) {
const {
rows,
dimension,
metrics,
links,
valueFull,
useDataValue,
label,
itemStyle,
lineStyle
} = args
const dataMap = {}
const seriesData = rows.map(row => {
dataMap[row[dimension]] = row[metrics]
return { name: row[dimension], value: row[metrics] }
})
let innerLinks = null
if (useDataValue) {
innerLinks = links.map(link => {
return Object.assign({}, link, { value: dataMap[link.target] })
})
} else if (!valueFull) {
innerLinks = links.map(link => {
return link.value == null
? Object.assign({}, link, { value: dataMap[link.target] })
: link
})
} else {
innerLinks = links
}
const result = {
type: 'sankey',
data: seriesData,
links: innerLinks
}
if (label) result.label = label
if (itemStyle) result.itemStyle = itemStyle
if (lineStyle) result.lineStyle = lineStyle
return [result]
}
export const sankey = (columns, rows, settings, extra) => {
const {
links,
dimension = columns[0],
metrics = columns[1],
dataType = ['normal', 'normal'],
digit = 2,
valueFull = false,
useDataValue = false,
label,
itemStyle,
lineStyle
} = settings
if (!links) {
console.warn('links is needed in settings!')
return
}
const itemDataType = dataType[0]
const linksDataType = dataType[1]
const tooltip = getTooltip({
itemDataType,
linksDataType,
digit
})
const series = getSeries({
rows,
dimension,
metrics,
links,
valueFull,
useDataValue,
label,
itemStyle,
lineStyle
})
return { tooltip, series }
}