@@ -8,6 +8,7 @@ import React, { useRef, useState, useEffect } from 'react';
8
8
import * as d3 from 'd3' ;
9
9
10
10
const Map = ( props ) => {
11
+ // Current snapshot
11
12
const { snapshot } = props ;
12
13
console . log ( 'MAP SNAPSHOT' , snapshot ) ;
13
14
@@ -17,65 +18,47 @@ const Map = (props) => {
17
18
18
19
// this state allows the canvas to stay at the zoom level on multiple re-renders
19
20
const [ { x, y, k } , setZoomState ] : any = useState ( { x : 0 , y : 0 , k : 0 } ) ;
20
-
21
21
useEffect ( ( ) => {
22
22
setZoomState ( d3 . zoomTransform ( d3 . select ( '#canvas' ) . node ( ) ) ) ;
23
23
} , [ snapshot . children ] ) ;
24
24
25
- // this only clears the canvas if Visualizer is already rendered on the extension
25
+ // Create D3 Tree Diagram
26
26
useEffect ( ( ) => {
27
+
28
+ const appState : any = {
29
+ name : ' Root' ,
30
+ children : snapshot . children ,
31
+ } ;
32
+ console . log ( 'STATE' , appState ) ;
33
+
27
34
document . getElementById ( 'canvas' ) . innerHTML = '' ;
28
35
29
36
// creating the main svg container for d3 elements
30
37
const svgContainer : any = d3
31
38
. select ( '#canvas' )
32
39
. attr ( 'width' , width )
33
40
. attr ( 'height' , height ) ;
41
+
34
42
// creating a pseudo-class for reusability
35
43
const g : any = svgContainer
36
-
37
44
. append ( 'g' )
38
45
. attr ( 'transform' , `translate(${ x } , ${ y } ), scale(${ k } )` ) ; // sets the canvas to the saved zoomState
39
46
40
- //RE-WRITE ALGORITHIM For All Possible Snapshot Formats
41
-
42
- // appState is the object that is passed into d3.hierarchy
43
- // const childrenArr = [];
44
- // if (snapshot.children[0].state.hooksState) {
45
- // snapshot.children[0].state.hooksState.forEach((el) =>
46
- // childrenArr.push(el)
47
- // );
48
- // }
49
-
50
- // console.log('CHILDREN', childrenArr);
51
-
52
- const appState : any = {
53
- name : ' Root' ,
54
- // pass in parsed data here
55
- children : snapshot . children ,
56
- } ;
57
-
58
- console . log ( 'STATE' , appState ) ;
47
+
59
48
// creating the tree map
60
49
const treeMap : any = d3 . tree ( ) . nodeSize ( [ width , height ] ) ;
61
50
62
51
// creating the nodes of the tree
63
- // pass
64
52
const hierarchyNodes : any = d3 . hierarchy ( appState ) ;
65
53
66
- console . log ( 'Hierarchy NODES' , hierarchyNodes ) ;
67
-
68
54
// calling the tree function with nodes created from data
69
55
const finalMap : any = treeMap ( hierarchyNodes ) ;
70
56
71
- console . log ( 'FINAL MAP' , finalMap ) ;
72
-
73
57
// renders a flat array of objects containing all parent-child links
74
58
// renders the paths onto the component
75
59
let paths : any = finalMap . links ( ) ;
76
- console . log ( 'PATHS' , paths ) ;
77
60
78
- // this creates the paths to each atom and its contents in the tree
61
+ // this creates the paths to each node and its contents in the tree
79
62
g . append ( 'g' )
80
63
. attr ( 'fill' , 'none' )
81
64
. attr ( 'stroke' , '#646464' )
@@ -93,11 +76,8 @@ const Map = (props) => {
93
76
) ;
94
77
95
78
// returns a flat array of objects containing all the nodes and their information
96
- // renders nodes onto the canvas
97
79
let nodes : any = hierarchyNodes . descendants ( ) ;
98
- console . log ( "NODES" , nodes )
99
80
100
- // const node is used to create all the nodes
101
81
// this segment places all the nodes on the canvas
102
82
const node : any = g
103
83
. append ( 'g' )
@@ -130,8 +110,7 @@ const Map = (props) => {
130
110
. attr ( 'stroke' , '#646464' )
131
111
. attr ( 'stroke-width' , 2 ) ;
132
112
133
- // adding a mouseOver event handler to each node
134
- // only add popup text on nodes with no children
113
+
135
114
// display the data in the node on hover
136
115
node . on ( 'mouseover' , function ( d : any , i : number ) : any {
137
116
if ( ! d . children ) {
0 commit comments