@@ -25,26 +25,36 @@ function SunburstChart({
25
25
const clickHandler = useRef ( onClick )
26
26
const hoverHandler = useRef ( onHover )
27
27
28
+ // this state stores the root node of the sunburst chart
28
29
const [ root ] = useState ( ( ) => {
30
+ // go through the data and add `value` to each node
29
31
const stack = [ data ]
30
- const result = { ...data , value : selectorHandler . current ( data ) }
31
32
const nodeMap = new Map ( )
33
+
34
+ // create a new root node with the value of the root node
35
+ const result = { ...data , value : selectorHandler . current ( data ) }
36
+ // add the root node to the node map
32
37
nodeMap . set ( data , result )
33
38
39
+ // while there are nodes to process, pop the last node from the stack
34
40
while ( stack . length > 0 ) {
35
41
const node = stack . pop ( )
36
42
const currentNode = nodeMap . get ( node )
37
43
44
+ // if the node has children, process them
38
45
if ( Array . isArray ( node . children ) ) {
39
46
currentNode . children = node . children . map ( ( child ) => {
40
- const newChild = { ...child , value : selectorHandler . current ( child ) }
47
+ const newChild = structuredClone ( child )
48
+ Object . assign ( newChild , { value : selectorHandler . current ( child ) } )
49
+
41
50
nodeMap . set ( child , newChild )
42
51
stack . push ( child )
43
52
return newChild
44
53
} )
45
54
}
46
55
}
47
56
57
+ // partition the data and add the `current` property to each node
48
58
return partitionFn ( result ) . each ( ( d ) => ( d . current = d ) )
49
59
} )
50
60
0 commit comments