1
- import React , { useRef , useEffect , useContext } from "react" ;
1
+ import React , { useRef , useEffect } from "react" ;
2
2
import { select , hierarchy , tree , linkHorizontal } from "d3" ;
3
- import { stateContext } from '../context/context' ;
4
-
5
- // i don't think this does what i need it to do
6
3
import useResizeObserver from "./useResizeObserver" ;
7
4
8
5
function usePrevious ( value ) {
@@ -14,14 +11,14 @@ function usePrevious(value) {
14
11
}
15
12
16
13
function TreeChart ( { data } ) {
17
- const [ state , dispatch ] = useContext ( stateContext ) ;
18
14
const svgRef = useRef ( ) ;
19
15
const wrapperRef = useRef ( ) ;
16
+
17
+ const xPosition = 50 ;
18
+ const textAndBorderColor = 'rgb(51, 235, 145)' ;
20
19
21
- // this needs to be modified
22
20
const dimensions = useResizeObserver ( wrapperRef ) ;
23
- console . log ( 'dimensions' , dimensions ) ;
24
-
21
+
25
22
// we save data to see if it changed
26
23
const previouslyRenderedData = usePrevious ( data ) ;
27
24
@@ -34,14 +31,10 @@ function TreeChart({ data }) {
34
31
// (dimensions are null for the first render)
35
32
const { width, height } =
36
33
dimensions || wrapperRef . current . getBoundingClientRect ( ) ;
37
-
38
- console . log ( 'width' , width ) ;
39
- console . log ( 'height' , height ) ;
40
-
41
34
// transform hierarchical data
42
35
const root = hierarchy ( data [ 0 ] ) ;
43
- const treeLayout = tree ( ) . size ( [ height , width ] ) ;
44
-
36
+ const treeLayout = tree ( ) . size ( [ height , width - 125 ] ) ;
37
+
45
38
// Returns a new link generator with horizontal display.
46
39
// To visualize links in a tree diagram rooted on the left edge of the display
47
40
const linkGenerator = linkHorizontal ( )
@@ -62,10 +55,13 @@ function TreeChart({ data }) {
62
55
cx: The position of the centre of the element in the x axis measured from the left side of the screen.
63
56
cy: The position of the centre of the element in the y axis measured from the top of the screen.
64
57
*/
65
- . attr ( "cx" , node => node . y )
66
- . attr ( "cy" , node => node . x )
67
- . attr ( "r" , 4 ) // radius of circle
68
- . attr ( "opacity" , 1 ) ;
58
+ // translate (x, y)
59
+ . attr ( "cx" , node => node . y )
60
+ . attr ( "cy" , node => node . x )
61
+ . attr ( "r" , 4 ) // radius of circle
62
+ . attr ( "opacity" , 1 )
63
+ . style ( 'fill' , 'white' )
64
+ . attr ( "transform" , `translate(${ xPosition } , 0)` ) ;
69
65
70
66
// link - lines that connect the nodes
71
67
const enteringAndUpdatingLinks = svg
@@ -74,14 +70,11 @@ function TreeChart({ data }) {
74
70
. join ( "path" )
75
71
. attr ( "class" , "link" )
76
72
. attr ( "d" , linkGenerator )
77
- . attr ( "stroke-dasharray" , function ( ) {
78
- const length = this . getTotalLength ( ) ;
79
- return `${ length } ${ length } ` ;
80
- } )
81
- . attr ( "stroke" , "black" )
73
+ . attr ( "stroke" , "white" )
82
74
. attr ( "fill" , "none" )
83
- . attr ( "opacity" , 1 ) ;
84
-
75
+ . attr ( "opacity" , 1 )
76
+ . attr ( "transform" , `translate(${ xPosition } , 0)` ) ;
77
+
85
78
if ( data !== previouslyRenderedData ) {
86
79
enteringAndUpdatingLinks
87
80
. attr ( "stroke-dashoffset" , function ( ) {
@@ -93,20 +86,37 @@ function TreeChart({ data }) {
93
86
// label - the names of each html element (node)
94
87
svg
95
88
. selectAll ( ".label" )
96
- . data ( root . descendants ( ) )
89
+ . data ( root . descendants ( ) )
97
90
. join ( enter => enter . append ( "text" ) . attr ( "opacity" , 0 ) )
98
91
. attr ( "class" , "label" )
99
92
. attr ( "x" , node => node . y )
100
93
. attr ( "y" , node => node . x - 12 )
101
94
. attr ( "text-anchor" , "middle" )
102
- . attr ( "font-size" , 24 )
95
+ . attr ( "font-size" , 18 )
96
+ . style ( 'fill' , textAndBorderColor )
103
97
. text ( node => node . data . name )
104
- . attr ( "opacity" , 1 ) ;
98
+ . attr ( "opacity" , 1 )
99
+ . attr ( "transform" , `translate(${ xPosition } , 0)` ) ;
105
100
} , [ data , dimensions , previouslyRenderedData ] ) ;
106
101
102
+ const treeStyles = {
103
+ height : '100%' ,
104
+ width : `100%` ,
105
+ margin : '10px 10px 10px 10px' ,
106
+ overflow : 'auto'
107
+ }
108
+
109
+ const wrapperStyles = {
110
+ border : `2px solid ${ textAndBorderColor } ` ,
111
+ borderRadius : '8px' ,
112
+ width : '100%' ,
113
+ height : '90%' ,
114
+ display : 'flex' ,
115
+ justifyContent : 'center' ,
116
+ }
107
117
return (
108
- < div ref = { wrapperRef } style = { { marginBottom : "2rem" } } >
109
- < svg ref = { svgRef } > </ svg >
118
+ < div ref = { wrapperRef } style = { wrapperStyles } >
119
+ < svg ref = { svgRef } style = { treeStyles } > </ svg >
110
120
</ div >
111
121
) ;
112
122
}
0 commit comments