@@ -18,6 +18,7 @@ import {
18
18
LinkThicknessFunction ,
19
19
LinkMouseEventHandler ,
20
20
LinkTooltip ,
21
+ IntermediateComputedNode ,
21
22
} from './types'
22
23
import { commonDefaultProps } from './defaults'
23
24
@@ -85,23 +86,31 @@ const useCartesianScales = ({
85
86
}
86
87
} , [ width , height , layout ] )
87
88
89
+ const useNodeColor = < Datum extends object > (
90
+ color : Exclude < CommonProps < Datum > [ 'nodeColor' ] , undefined >
91
+ ) =>
92
+ useMemo ( ( ) => {
93
+ if ( typeof color === 'function' ) return color
94
+ return ( ) => color
95
+ } , [ color ] )
96
+
88
97
const useNodes = < Datum extends object > ( {
89
98
root,
90
99
xScale,
91
100
yScale,
92
101
layout,
93
102
getIdentity,
103
+ nodeColor,
94
104
} : {
95
105
root : HierarchyDendogramNode < Datum >
96
106
xScale : ScaleLinear < number , number >
97
107
yScale : ScaleLinear < number , number >
98
108
layout : Layout
99
109
getIdentity : ( node : Datum ) => string
100
- } ) =>
101
- useMemo ( ( ) => {
102
- const nodeByUid : Record < string , ComputedNode < Datum > > = { }
103
-
104
- const nodes : ComputedNode < Datum > [ ] = root . descendants ( ) . map ( node => {
110
+ nodeColor : Exclude < CommonProps < Datum > [ 'nodeColor' ] , undefined >
111
+ } ) => {
112
+ const intermediateNodes = useMemo < IntermediateComputedNode < Datum > [ ] > ( ( ) => {
113
+ return root . descendants ( ) . map ( node => {
105
114
const { pathComponents } = computeNodePath < Datum > ( node , getIdentity )
106
115
107
116
let x : number
@@ -114,7 +123,7 @@ const useNodes = <Datum extends object>({
114
123
y = yScale ( node . x ! )
115
124
}
116
125
117
- const computedNode : ComputedNode < Datum > = {
126
+ return {
118
127
uid : node . uid ! ,
119
128
id : getIdentity ( node . data ) ,
120
129
data : node . data ,
@@ -124,6 +133,20 @@ const useNodes = <Datum extends object>({
124
133
x,
125
134
y,
126
135
}
136
+ } )
137
+ } , [ root , getIdentity , layout , xScale , yScale ] )
138
+
139
+ const getNodeColor = useNodeColor < Datum > ( nodeColor )
140
+
141
+ return useMemo ( ( ) => {
142
+ const nodeByUid : Record < string , ComputedNode < Datum > > = { }
143
+
144
+ const nodes : ComputedNode < Datum > [ ] = intermediateNodes . map ( intermediateNode => {
145
+ const computedNode : ComputedNode < Datum > = {
146
+ ...intermediateNode ,
147
+ color : getNodeColor ( intermediateNode ) ,
148
+ }
149
+
127
150
nodeByUid [ computedNode . uid ] = computedNode
128
151
129
152
return computedNode
@@ -133,16 +156,27 @@ const useNodes = <Datum extends object>({
133
156
nodes,
134
157
nodeByUid,
135
158
}
136
- } , [ root , getIdentity , layout , xScale , yScale ] )
159
+ } , [ intermediateNodes , getNodeColor ] )
160
+ }
161
+
162
+ const useLinkColor = < Datum extends object > (
163
+ color : Exclude < CommonProps < Datum > [ 'linkColor' ] , undefined >
164
+ ) =>
165
+ useMemo ( ( ) => {
166
+ if ( typeof color === 'function' ) return color
167
+ return ( ) => color
168
+ } , [ color ] )
137
169
138
170
const useLinks = < Datum extends object > ( {
139
171
root,
140
172
nodeByUid,
141
173
linkThickness,
174
+ linkColor,
142
175
} : {
143
176
root : HierarchyDendogramNode < Datum >
144
177
nodeByUid : Record < string , ComputedNode < Datum > >
145
178
linkThickness : Exclude < CommonProps < Datum > [ 'linkThickness' ] , undefined >
179
+ linkColor : Exclude < CommonProps < Datum > [ 'linkColor' ] , undefined >
146
180
} ) : ComputedLink < Datum > [ ] => {
147
181
const intermediateLinks = useMemo < IntermediateComputedLink < Datum > [ ] > ( ( ) => {
148
182
return ( root . links ( ) as HierarchyDendogramLink < Datum > [ ] ) . map ( link => {
@@ -160,14 +194,17 @@ const useLinks = <Datum extends object>({
160
194
return ( ) => linkThickness
161
195
} , [ linkThickness ] )
162
196
197
+ const getLinkColor = useLinkColor < Datum > ( linkColor )
198
+
163
199
return useMemo ( ( ) => {
164
200
return intermediateLinks . map ( intermediateLink => {
165
201
return {
166
202
...intermediateLink ,
167
203
thickness : getLinkThickness ( intermediateLink ) ,
204
+ color : getLinkColor ( intermediateLink ) ,
168
205
}
169
206
} )
170
- } , [ intermediateLinks , getLinkThickness ] )
207
+ } , [ intermediateLinks , getLinkThickness , getLinkColor ] )
171
208
}
172
209
173
210
export const useDendogram = < Datum extends object = DefaultDatum > ( {
@@ -176,14 +213,18 @@ export const useDendogram = <Datum extends object = DefaultDatum>({
176
213
layout = commonDefaultProps . layout ,
177
214
width,
178
215
height,
216
+ nodeColor = commonDefaultProps . nodeColor ,
179
217
linkThickness = commonDefaultProps . linkThickness ,
218
+ linkColor = commonDefaultProps . linkColor ,
180
219
} : {
181
220
data : DendogramDataProps < Datum > [ 'data' ]
182
221
identity ?: CommonProps < Datum > [ 'identity' ]
183
222
layout ?: Layout
184
223
width : number
185
224
height : number
225
+ nodeColor ?: CommonProps < Datum > [ 'nodeColor' ]
186
226
linkThickness ?: CommonProps < Datum > [ 'linkThickness' ]
227
+ linkColor ?: CommonProps < Datum > [ 'linkColor' ]
187
228
} ) => {
188
229
const getIdentity = usePropertyAccessor ( identity )
189
230
@@ -205,9 +246,15 @@ export const useDendogram = <Datum extends object = DefaultDatum>({
205
246
yScale,
206
247
layout,
207
248
getIdentity,
249
+ nodeColor,
208
250
} )
209
251
210
- const links = useLinks < Datum > ( { root, nodeByUid, linkThickness } )
252
+ const links = useLinks < Datum > ( { root, nodeByUid, linkThickness, linkColor } )
253
+
254
+ console . log ( {
255
+ nodes,
256
+ links,
257
+ } )
211
258
212
259
return {
213
260
nodes,
0 commit comments