5
5
import React , { Component } from 'react' ;
6
6
import PropTypes from 'prop-types' ;
7
7
import * as d3 from 'd3' ;
8
+ import d3Tip from "d3-tip" ;
8
9
9
10
let root = { } ;
10
11
class Chart extends Component {
@@ -35,17 +36,31 @@ class Chart extends Component {
35
36
36
37
maked3Tree ( ) {
37
38
this . removed3Tree ( ) ;
38
- let width = 900 ;
39
- let height = 1000 ;
39
+ let width = 600 ;
40
+ // let height = 1060;
41
+ const margin = {
42
+ top : 0 ,
43
+ right : 60 ,
44
+ bottom : 80 ,
45
+ left : 120 ,
46
+ } ;
47
+ // const width = 600 - margin.right - margin.left;
48
+ const height = 700 - margin . top - margin . bottom ;
49
+
40
50
let chartContainer = d3 . select ( this . chartRef . current )
41
51
. append ( 'svg' ) // chartContainer is now pointing to svg
42
52
. attr ( 'width' , width )
43
53
. attr ( 'height' , height ) ;
44
-
45
- let g = chartContainer
54
+
55
+ chartContainer . call ( d3 . zoom ( )
56
+ . on ( "zoom" , function ( ) {
57
+ chartContainer . attr ( "transform" , d3 . event . transform )
58
+ } ) )
46
59
. append ( "g" )
60
+
61
+ let g = chartContainer . append ( "g" )
47
62
// this is changing where the graph is located physically
48
- . attr ( "transform" , `translate(${ width / 2.5 } , ${ height / 2 + 90 } )` ) ;
63
+ . attr ( "transform" , `translate(${ width / 2 + 4 } , ${ height / 2 + 2 } )` ) ;
49
64
50
65
// if we consider the container for our radial node graph as a box encapsulating, half of this container width is essentially the radius of our radial node graph
51
66
let radius = width / 2 ;
@@ -55,7 +70,8 @@ class Chart extends Component {
55
70
56
71
let tree = d3 . tree ( )
57
72
// this assigns width of tree to be 2pi
58
- . size ( [ 2 * Math . PI , radius ] )
73
+ . size ( [ 2 * Math . PI , radius / 1.5 ] )
74
+ . separation ( function ( a , b ) { return ( a . parent == b . parent ? 1 : 2 ) / a . depth } ) ;
59
75
60
76
let d3root = tree ( hierarchy ) ;
61
77
@@ -74,6 +90,7 @@ class Chart extends Component {
74
90
. data ( d3root . descendants ( ) )
75
91
. enter ( )
76
92
. append ( "g" )
93
+ // assigning class to the node based on whether node has children or not
77
94
. attr ( "class" , function ( d ) {
78
95
return "node" + ( d . children ? " node--internal" : " node--leaf" )
79
96
} )
@@ -82,21 +99,41 @@ class Chart extends Component {
82
99
} ) ;
83
100
84
101
node . append ( "circle" )
85
- . attr ( "r" , 5.5 )
102
+ . attr ( "r" , 12 )
103
+
104
+ //creating a d3.tip method where the html has a function that returns the data we passed into tip.show from line 120
105
+ let tip = d3Tip ( )
106
+ . attr ( "class" , "d3-tip" )
107
+ . html ( function ( d ) { return "State: " + d ; } )
108
+
109
+ //invoking tooltip for nodes
110
+ node . call ( tip )
86
111
87
112
node
88
113
. append ( "text" )
89
- . attr ( "dy" , "0.1em" )
114
+ // adjusts the y coordinates for the node text
115
+ . attr ( "dy" , "0.35em" )
90
116
. attr ( "x" , function ( d ) {
91
117
// this positions how far the text is from leaf nodes (ones without children)
92
- return d . x < Math . PI === ! d . children ? 10 : - 10 ;
118
+ // negative number before the colon moves the text of rightside nodes, positive number moves the text for the leftside nodes
119
+ return d . x < Math . PI === ! d . children ? - 4 : 5 ;
93
120
} )
94
121
. attr ( "text-anchor" , function ( d ) { return d . x < Math . PI === ! d . children ? "start" : "end" ; } )
95
122
// this arranges the angle of the text
96
- . attr ( "transform" , function ( d ) { return "rotate(" + ( d . x < Math . PI ? d . x - Math . PI / 2 : d . x + Math . PI / 2 ) * 180 / Math . PI + ")" ; } )
123
+ . attr ( "transform" , function ( d ) { return "rotate(" + ( d . x < Math . PI ? d . x - Math . PI / 2 : d . x + Math . PI / 2 ) * 1 / Math . PI + ")" ; } )
97
124
. text ( function ( d ) {
98
- return "state" + d . data . index ;
125
+ return d . data . index ;
99
126
} ) ;
127
+
128
+ //applying tooltip on mouseover and removes it when mouse cursor moves away
129
+ node
130
+ . on ( 'mouseover' , function ( d ) {
131
+ // without JSON.stringify, data will display as object Object
132
+ // console.log('d.data --> ', JSON.stringify(d.data))
133
+ let displayedState ;
134
+ tip . show ( JSON . stringify ( d . data . stateSnapshot . children [ 0 ] . state ) , this )
135
+ } )
136
+ . on ( 'mouseout' , tip . hide )
100
137
101
138
function reinfeldTidierAlgo ( x , y ) {
102
139
return [ ( y = + y ) * Math . cos ( x -= Math . PI / 2 ) , y * Math . sin ( x ) ] ;
0 commit comments