@@ -8,12 +8,12 @@ import React, { useState, useEffect } from 'react';
8
8
import * as d3 from 'd3' ;
9
9
10
10
const Map = ( props ) => {
11
- // Current snapshot
12
- const { snapshot } = props ;
13
- console . log ( 'MAP SNAPSHOT' , snapshot ) ;
11
+
12
+ const { snapshot, snapshots } = props ;
13
+ const lastSnap = snapshots . length - 1
14
14
15
15
// set the heights and width of the tree to be passed into treeMap function
16
- const width : number = 1100 ;
16
+ const width : number = 900 ;
17
17
const height : number = 600 ;
18
18
19
19
// this state allows the canvas to stay at the zoom level on multiple re-renders
@@ -22,15 +22,9 @@ const Map = (props) => {
22
22
setZoomState ( d3 . zoomTransform ( d3 . select ( '#canvas' ) . node ( ) ) ) ;
23
23
} , [ snapshot . children ] ) ;
24
24
25
- // Create D3 Tree Diagram
25
+ // Create D3 Tree Diagram
26
26
useEffect ( ( ) => {
27
27
28
- const appState : any = {
29
- name : ' Root' ,
30
- children : snapshot . children ,
31
- } ;
32
- console . log ( 'STATE' , appState ) ;
33
-
34
28
document . getElementById ( 'canvas' ) . innerHTML = '' ;
35
29
36
30
// creating the main svg container for d3 elements
@@ -44,12 +38,11 @@ const Map = (props) => {
44
38
. append ( 'g' )
45
39
. attr ( 'transform' , `translate(${ x } , ${ y } ), scale(${ k } )` ) ; // sets the canvas to the saved zoomState
46
40
47
-
48
41
// creating the tree map
49
42
const treeMap : any = d3 . tree ( ) . nodeSize ( [ width , height ] ) ;
50
43
51
44
// creating the nodes of the tree
52
- const hierarchyNodes : any = d3 . hierarchy ( appState ) ;
45
+ const hierarchyNodes : any = d3 . hierarchy ( snapshots [ lastSnap ] ) ;
53
46
54
47
// calling the tree function with nodes created from data
55
48
const finalMap : any = treeMap ( hierarchyNodes ) ;
@@ -110,7 +103,6 @@ const Map = (props) => {
110
103
. attr ( 'stroke' , '#646464' )
111
104
. attr ( 'stroke-width' , 2 ) ;
112
105
113
-
114
106
// display the data in the node on hover
115
107
node . on ( 'mouseover' , function ( d : any , i : number ) : any {
116
108
if ( ! d . children ) {
@@ -142,24 +134,24 @@ const Map = (props) => {
142
134
) ;
143
135
144
136
// allows the canvas to be zoom-able
145
- // d3 zoom functionality
146
- let zoom = d3 . zoom ( ) . on ( 'zoom' , zoomed ) ;
147
- svgContainer . call (
148
- zoom . transform ,
149
- // Changes the initial view, (left, top)
150
- d3 . zoomIdentity . translate ( 150 , 250 ) . scale ( 0.2 ) ,
151
- ) ;
152
- // allows the canvas to be zoom-able
153
- svgContainer . call (
154
- d3
155
- . zoom ( )
156
- . scaleExtent ( [ 0.05 , 0.9 ] ) // [zoomOut, zoomIn]
157
- . on ( 'zoom' , zoomed ) ,
158
- ) ;
159
- // helper function that allows for zooming
160
- function zoomed ( d : any ) {
161
- g . attr ( 'transform' , d3 . event . transform ) ;
162
- }
137
+ // d3 zoom functionality
138
+ let zoom = d3 . zoom ( ) . on ( 'zoom' , zoomed ) ;
139
+ svgContainer . call (
140
+ zoom . transform ,
141
+ // Changes the initial view, (left, top)
142
+ d3 . zoomIdentity . translate ( 150 , 250 ) . scale ( 0.2 )
143
+ ) ;
144
+ // allows the canvas to be zoom-able
145
+ svgContainer . call (
146
+ d3
147
+ . zoom ( )
148
+ . scaleExtent ( [ 0.05 , 0.9 ] ) // [zoomOut, zoomIn]
149
+ . on ( 'zoom' , zoomed )
150
+ ) ;
151
+ // helper function that allows for zooming
152
+ function zoomed ( d : any ) {
153
+ g . attr ( 'transform' , d3 . event . transform ) ;
154
+ }
163
155
164
156
// helper functions that help with dragging functionality
165
157
function dragStarted ( ) : any {
@@ -176,8 +168,6 @@ const Map = (props) => {
176
168
function dragEnded ( ) : any {
177
169
g . attr ( 'cursor' , 'grab' ) ;
178
170
}
179
-
180
-
181
171
} ) ;
182
172
183
173
return (
0 commit comments