12
12
import React , { Component } from 'react' ;
13
13
// import PropTypes from 'prop-types';
14
14
import * as d3 from 'd3' ;
15
- import d3Tip from 'd3-tip' ;
15
+ // import d3Tip from 'd3-tip';
16
16
17
17
let root = { } ;
18
18
class Chart extends Component {
@@ -78,10 +78,10 @@ class Chart extends Component {
78
78
const d3root = tree ( hierarchy ) ;
79
79
80
80
g . selectAll ( '.link' )
81
- // root.links() gets an array of all the links,
82
- // where each element is an object containing a
83
- // source property, which represents the link's source node,
84
- // and a target property, which represents the link's target node.
81
+ // root.links() gets an array of all the links,
82
+ // where each element is an object containing a
83
+ // source property, which represents the link's source node,
84
+ // and a target property, which represents the link's target node.
85
85
. data ( d3root . links ( ) )
86
86
. enter ( )
87
87
. append ( 'path' )
@@ -104,21 +104,36 @@ class Chart extends Component {
104
104
} ) ;
105
105
106
106
node . append ( 'circle' )
107
- . attr ( 'r' , 10 ) ;
108
-
109
- // creating a d3.tip method where the html has a function that returns
110
- // the data we passed into tip.show from line 120
111
- const tip = d3Tip ( )
112
- . attr ( 'class' , 'd3-tip' )
113
- . html ( function ( d ) { return 'State: ' + d ; } ) ;
114
-
115
- // invoking tooltip for nodes
116
- node . call ( tip ) ;
117
-
107
+ . attr ( 'r' , 10 )
108
+ . on ( 'mouseover' , function ( d ) {
109
+ d3 . select ( this )
110
+ . transition ( 100 )
111
+ . duration ( 20 )
112
+ . attr ( 'r' , 20 ) ;
113
+
114
+ tooltipDiv . transition ( )
115
+ . duration ( 200 )
116
+ . style ( 'opacity' , 0.9 ) ;
117
+
118
+ tooltipDiv . html ( JSON . stringify ( d . data . stateSnapshot . children [ 0 ] . state ) , this )
119
+ . style ( 'left' , ( d3 . event . pageX ) + 'px' )
120
+ . style ( 'top' , ( d3 . event . pageY - 28 ) + 'px' ) ;
121
+ } )
122
+ // eslint-disable-next-line no-unused-vars
123
+ . on ( 'mouseout' , function ( d ) {
124
+ d3 . select ( this )
125
+ . transition ( )
126
+ . duration ( 200 )
127
+ . attr ( 'r' , 12 ) ;
128
+
129
+ tooltipDiv . transition ( )
130
+ . duration ( 500 )
131
+ . style ( 'opacity' , 0 ) ;
132
+ } ) ;
118
133
node
119
134
. append ( 'text' )
120
135
// adjusts the y coordinates for the node text
121
- . attr ( 'dy' , '0.35em ' )
136
+ . attr ( 'dy' , '-1.5em ' )
122
137
. attr ( 'x' , function ( d ) {
123
138
// this positions how far the text is from leaf nodes (ones without children)
124
139
// negative number before the colon moves the text of rightside nodes,
@@ -160,14 +175,12 @@ class Chart extends Component {
160
175
g . attr ( 'transform' , d3 . event . transform ) ;
161
176
}
162
177
178
+ // define the div for the tooltip
179
+ const tooltipDiv = d3 . select ( 'body' ) . append ( 'div' )
180
+ . attr ( 'class' , 'tooltip' )
181
+ . style ( 'opacity' , 0 ) ;
182
+
163
183
// applying tooltip on mouseover and removes it when mouse cursor moves away
164
- node
165
- . on ( 'mouseover' , function ( d ) {
166
- // without JSON.stringify, data will display as object Object
167
- // console.log('d.data --> ', JSON.stringify(d.data))
168
- tip . show ( JSON . stringify ( d . data . stateSnapshot . children [ 0 ] . state ) , this ) ;
169
- } )
170
- . on ( 'mouseout' , tip . hide ) ;
171
184
172
185
function reinfeldTidierAlgo ( x , y ) {
173
186
return [ ( y = + y ) * Math . cos ( x -= Math . PI / 2 ) , y * Math . sin ( x ) ] ;
0 commit comments