/
TreeMap.tsx
169 lines (159 loc) · 5.27 KB
/
TreeMap.tsx
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
import { createElement, Fragment, ReactNode } from 'react'
import {
SvgWrapper,
Container,
useDimensions,
// @ts-ignore
bindDefs,
} from '@nivo/core'
import { useTreeMap, useCustomLayerProps } from './hooks'
import { TreeMapNodes } from './TreeMapNodes'
import {
DefaultTreeMapDatum,
NodeComponent,
TreeMapCommonProps,
TreeMapSvgProps,
LayerId,
} from './types'
import { svgDefaultProps } from './defaults'
type InnerTreeMapProps<Datum extends object> = Omit<
TreeMapSvgProps<Datum>,
'animate' | 'motionConfig' | 'renderWrapper' | 'theme'
>
const InnerTreeMap = <Datum extends object>({
data,
identity = svgDefaultProps.identity as TreeMapCommonProps<Datum>['identity'],
value = svgDefaultProps.value as TreeMapCommonProps<Datum>['value'],
valueFormat,
tile = svgDefaultProps.tile,
nodeComponent = svgDefaultProps.nodeComponent as NodeComponent<Datum>,
innerPadding = svgDefaultProps.innerPadding,
outerPadding = svgDefaultProps.outerPadding,
leavesOnly = svgDefaultProps.leavesOnly,
width,
height,
margin: partialMargin,
layers = svgDefaultProps.layers as NonNullable<TreeMapSvgProps<Datum>['layers']>,
colors = svgDefaultProps.colors as TreeMapCommonProps<Datum>['colors'],
colorBy = svgDefaultProps.colorBy as TreeMapCommonProps<Datum>['colorBy'],
nodeOpacity = svgDefaultProps.nodeOpacity,
borderWidth = svgDefaultProps.borderWidth,
borderColor = svgDefaultProps.borderColor as TreeMapCommonProps<Datum>['borderColor'],
defs = svgDefaultProps.defs,
fill = svgDefaultProps.fill,
enableLabel = svgDefaultProps.enableLabel,
label = svgDefaultProps.label as TreeMapCommonProps<Datum>['label'],
labelTextColor = svgDefaultProps.labelTextColor as TreeMapCommonProps<Datum>['labelTextColor'],
orientLabel = svgDefaultProps.orientLabel,
labelSkipSize = svgDefaultProps.labelSkipSize,
enableParentLabel = svgDefaultProps.enableParentLabel,
parentLabel = svgDefaultProps.parentLabel as TreeMapCommonProps<Datum>['parentLabel'],
parentLabelSize = svgDefaultProps.parentLabelSize,
parentLabelPosition = svgDefaultProps.parentLabelPosition,
parentLabelPadding = svgDefaultProps.parentLabelPadding,
parentLabelTextColor = svgDefaultProps.parentLabelTextColor as TreeMapCommonProps<Datum>['parentLabelTextColor'],
isInteractive = svgDefaultProps.isInteractive,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
tooltip = svgDefaultProps.tooltip as TreeMapCommonProps<Datum>['tooltip'],
role,
ariaLabel,
ariaLabelledBy,
ariaDescribedBy,
}: InnerTreeMapProps<Datum>) => {
const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions(
width,
height,
partialMargin
)
const { nodes } = useTreeMap<Datum>({
data,
identity,
value,
valueFormat,
leavesOnly,
width: innerWidth,
height: innerHeight,
tile,
innerPadding,
outerPadding,
colors,
colorBy,
nodeOpacity,
borderColor,
label,
labelTextColor,
orientLabel,
enableParentLabel,
parentLabel,
parentLabelSize,
parentLabelPosition,
parentLabelPadding,
parentLabelTextColor,
})
const layerById: Record<LayerId, ReactNode> = {
nodes: null,
}
if (layers.includes('nodes')) {
layerById.nodes = (
<TreeMapNodes<Datum>
key="nodes"
nodes={nodes}
nodeComponent={nodeComponent}
borderWidth={borderWidth}
enableLabel={enableLabel}
labelSkipSize={labelSkipSize}
enableParentLabel={enableParentLabel}
isInteractive={isInteractive}
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
onClick={onClick}
tooltip={tooltip}
/>
)
}
const customLayerProps = useCustomLayerProps<Datum>({ nodes })
const boundDefs = bindDefs(defs, nodes, fill)
return (
<SvgWrapper
width={outerWidth}
height={outerHeight}
margin={margin}
defs={boundDefs}
role={role}
ariaLabel={ariaLabel}
ariaLabelledBy={ariaLabelledBy}
ariaDescribedBy={ariaDescribedBy}
>
{layers.map((layer, i) => {
if (typeof layer === 'function') {
return <Fragment key={i}>{createElement(layer, customLayerProps)}</Fragment>
}
return layerById?.[layer] ?? null
})}
</SvgWrapper>
)
}
export const TreeMap = <Datum extends object = DefaultTreeMapDatum>({
isInteractive = svgDefaultProps.isInteractive,
animate = svgDefaultProps.animate,
motionConfig = svgDefaultProps.motionConfig,
theme,
renderWrapper,
...otherProps
}: TreeMapSvgProps<Datum>) => (
<Container
{...{
animate,
isInteractive,
motionConfig,
renderWrapper,
theme,
}}
>
<InnerTreeMap<Datum> isInteractive={isInteractive} {...otherProps} />
</Container>
)