-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
enhance.js
145 lines (128 loc) · 4.52 KB
/
enhance.js
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
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { treemap as d3Treemap } from 'd3-hierarchy'
import cloneDeep from 'lodash/cloneDeep'
import compose from 'recompose/compose'
import defaultProps from 'recompose/defaultProps'
import withPropsOnChange from 'recompose/withPropsOnChange'
import pure from 'recompose/pure'
import {
withHierarchy,
withDimensions,
withTheme,
withMotion,
getAccessorFor,
getLabelGenerator,
treeMapTileFromProp,
bindDefs,
} from '@nivo/core'
import { getOrdinalColorScale, getInheritedColorGenerator } from '@nivo/colors'
import * as props from './props'
const computeNodePath = (node, getIdentity) =>
node
.ancestors()
.map(ancestor => getIdentity(ancestor.data))
.join('.')
const commonEnhancers = [
withHierarchy(),
withDimensions(),
withTheme(),
withMotion(),
withPropsOnChange(['colors', 'colorBy'], ({ colors, colorBy }) => ({
getColor: getOrdinalColorScale(colors, colorBy),
})),
withPropsOnChange(['identity'], ({ identity }) => ({
getIdentity: getAccessorFor(identity),
})),
withPropsOnChange(['borderColor', 'theme'], ({ borderColor, theme }) => ({
getBorderColor: getInheritedColorGenerator(borderColor, theme),
})),
withPropsOnChange(['label', 'labelFormat'], ({ label, labelFormat }) => ({
getLabel: getLabelGenerator(label, labelFormat),
})),
withPropsOnChange(['labelTextColor', 'theme'], ({ labelTextColor, theme }) => ({
getLabelTextColor: getInheritedColorGenerator(labelTextColor, theme),
})),
withPropsOnChange(
['width', 'height', 'tile', 'innerPadding', 'outerPadding'],
({ width, height, tile, innerPadding, outerPadding }) => ({
treemap: d3Treemap()
.size([width, height])
.tile(treeMapTileFromProp(tile))
.round(true)
.paddingInner(innerPadding)
.paddingOuter(outerPadding),
})
),
withPropsOnChange(
['root', 'treemap', 'leavesOnly', 'getIdentity', 'getColor'],
({ root: _root, treemap, leavesOnly, getIdentity, getColor }) => {
const root = cloneDeep(_root)
treemap(root)
let nodes = leavesOnly ? root.leaves() : root.descendants()
nodes = nodes.map(d => {
d.path = computeNodePath(d, getIdentity)
d.nodeHeight = d.height
d.x = d.x0
d.y = d.y0
d.width = d.x1 - d.x0
d.height = d.y1 - d.y0
d.data.color = d.color = getColor({ ...d.data, depth: d.depth })
d.data.id = d.id = getIdentity(d.data)
d.data.value = d.value
return d
})
return { nodes }
}
),
withPropsOnChange(
['enableLabel', 'nodes', 'getLabel', 'labelSkipSize'],
({ enableLabel, nodes, getLabel, labelSkipSize }) => {
if (!enableLabel) return
const nodesWithLabel = nodes.map(node => {
if (
node.nodeHeight > 0 ||
(labelSkipSize !== 0 && Math.min(node.width, node.height) <= labelSkipSize)
)
return node
return { ...node, label: getLabel(node.data) }
})
return { nodes: nodesWithLabel }
}
),
]
const svgEnhancers = [
withPropsOnChange(['nodes', 'defs', 'fill'], ({ nodes, defs, fill }) => {
return {
defs: bindDefs(defs, nodes, fill, { targetKey: 'fill' }),
}
}),
]
export default Component => {
const implDefaultProps = props[`${Component.displayName}DefaultProps`]
switch (Component.displayName) {
case 'TreeMap':
return compose(
...[
defaultProps(implDefaultProps),
...commonEnhancers,
...svgEnhancers,
withMotion(),
pure,
]
)(Component)
case 'TreeMapHtml':
return compose(
...[defaultProps(implDefaultProps), ...commonEnhancers, withMotion(), pure]
)(Component)
case 'TreeMapCanvas':
return compose(...[defaultProps(implDefaultProps), ...commonEnhancers, pure])(Component)
}
return Component
}