@@ -3,6 +3,35 @@ import '../styles/components/_d3Tree.scss';
3
3
import * as d3 from 'd3' ;
4
4
5
5
var root = { } ;
6
+ let duration = 750 ;
7
+
8
+ // var test={
9
+ // name: 'root',
10
+ // state: {
11
+ // "kids": 0,
12
+ // "kids1": 0,
13
+ // "kids2": 0,
14
+ // "kids3": 0,
15
+ // "kids4": 0,
16
+ // "kids5": {
17
+ // subkid: 3,
18
+ // subkid3: 5,
19
+ // sukid5:{
20
+ // kid:1,
21
+ // kid:3
22
+ // }
23
+ // },
24
+ // "kids6": 0,
25
+ // "kids7": 0,
26
+ // "kids8": 0,
27
+ // "kids9": 0,
28
+ // "kids10": 0,
29
+ // "kids11": 0,
30
+ // "kids12": 0,
31
+ // "kids13": 0,
32
+ // },
33
+ // children: [{name: 'hello',state:{name: "codesmith"}},{name: 'bye'}]
34
+ // }
6
35
7
36
class Chart extends Component {
8
37
constructor ( props ) {
@@ -31,15 +60,17 @@ class Chart extends Component {
31
60
maked3Tree ( ) {
32
61
this . removed3Tree ( ) ;
33
62
63
+ duration = 0 ;
64
+ // root = test;
65
+
34
66
var margin = { top : 20 , right : 120 , bottom : 20 , left : 120 } ,
35
67
width = 960 - margin . right - margin . left ,
36
68
height = 800 - margin . top - margin . bottom ;
37
69
38
- var i = 0 ,
39
- duration = 750 ;
70
+ var i = 0 ;
40
71
41
72
var tree = d3 . layout . tree ( )
42
- . size ( [ height , width ] ) ;
73
+ . size ( [ 400 , 400 ] ) ;
43
74
44
75
var diagonal = d3 . svg . diagonal ( )
45
76
. projection ( function ( d ) { return [ d . y , d . x ] ; } ) ;
@@ -50,6 +81,14 @@ class Chart extends Component {
50
81
. append ( "g" )
51
82
. attr ( "transform" , "translate(" + margin . left + "," + margin . top + ")" ) ;
52
83
84
+ // Add tooltip div
85
+ var div = d3 . select ( "body" ) . append ( "div" )
86
+ . attr ( "class" , "tooltip" )
87
+ . style ( "opacity" , 1e-6 )
88
+ . on ( "mouseover" , tipMouseover )
89
+ . on ( "mouseout" , tipMouseout )
90
+
91
+
53
92
54
93
root . x0 = height / 2 ;
55
94
root . y0 = 0 ;
@@ -74,7 +113,9 @@ class Chart extends Component {
74
113
. attr ( "transform" , function ( d ) { return "translate(" + source . y0 + "," + source . x0 + ")" ; } )
75
114
. on ( "click" , click )
76
115
. on ( "mouseover" , mouseover )
77
- . on ( "mouseout" , mouseout ) ;
116
+ . on ( "mouseout" , mouseout )
117
+ . on ( "mousemove" , function ( d ) { mousemove ( d ) ; } ) ;
118
+
78
119
79
120
nodeEnter . append ( "circle" )
80
121
. attr ( "r" , 1e-6 )
@@ -85,6 +126,7 @@ class Chart extends Component {
85
126
. attr ( "dy" , ".35em" )
86
127
. attr ( "text-anchor" , function ( d ) { return d . children || d . _children ? "end" : "start" ; } )
87
128
. text ( function ( d ) { return d . name ; } )
129
+ . style ( 'fill' , '#6FB3D2' )
88
130
. style ( "fill-opacity" , 1e-6 ) ;
89
131
90
132
// Transition nodes to their new position.
@@ -93,8 +135,8 @@ class Chart extends Component {
93
135
. attr ( "transform" , function ( d ) { return "translate(" + d . y + "," + d . x + ")" ; } ) ;
94
136
95
137
nodeUpdate . select ( "circle" )
96
- . attr ( "r" , 4.5 )
97
- . style ( "fill" , function ( d ) { return d . _children ? "lightsteelblue " : "#fff " ; } ) ;
138
+ . attr ( "r" , 7 )
139
+ . style ( "fill" , function ( d ) { return d . _children ? "#A1C658 " : "#D381C3 " ; } ) ;
98
140
99
141
nodeUpdate . select ( "text" )
100
142
. style ( "fill-opacity" , 1 ) ;
@@ -158,17 +200,49 @@ class Chart extends Component {
158
200
159
201
// Show state on mouse over
160
202
function mouseover ( d ) {
161
- d3 . select ( this ) . append ( "text" )
162
- . attr ( "class" , "hover" )
163
- . attr ( 'transform' , function ( d ) {
164
- return 'translate(5, -10)' ;
165
- } )
166
- . text ( d . state === undefined ? '' : JSON . stringify ( d . state ) ) ;
203
+ // d3.select(this).append("text")
204
+ // .attr("class", "hover")
205
+ // .attr('transform', function(d){
206
+ // return 'translate(5, -10)';
207
+ // })
208
+ // .text(d.state === undefined ? '' : JSON.stringify(d.state));
209
+
210
+ div . transition ( )
211
+ . duration ( 300 )
212
+ . style ( "display" , "block" )
213
+ . style ( "opacity" , 1 )
167
214
}
168
215
169
- // Toggle children on click.
170
216
function mouseout ( d ) {
171
- d3 . select ( this ) . select ( "text.hover" ) . remove ( ) ;
217
+ // d3.select(this).select("text.hover").remove();
218
+
219
+ div . transition ( )
220
+ . duration ( 3000 )
221
+ . style ( "opacity" , 1e-6 )
222
+ . style ( "display" , "none" ) ;
223
+ }
224
+
225
+ function tipMouseover ( d ) {
226
+ div . transition ( )
227
+ . duration ( 300 )
228
+ . style ( "opacity" , 1 ) ;
229
+ }
230
+
231
+ function tipMouseout ( d ) {
232
+ // d3.select(this).select("text.hover").remove();
233
+
234
+ div . transition ( )
235
+ . duration ( 3000 )
236
+ . style ( "opacity" , 1e-6 )
237
+ . style ( "display" , "none" ) ;
238
+ }
239
+
240
+
241
+ function mousemove ( d ) {
242
+ div
243
+ . text ( d . state == undefined ? 'No state found' : JSON . stringify ( d . state , null , 4 ) )
244
+ . style ( "left" , ( d3 . event . pageX ) + "px" )
245
+ . style ( "top" , ( d3 . event . pageY ) + "px" ) ;
172
246
}
173
247
174
248
function collapse ( d ) {
@@ -181,16 +255,14 @@ class Chart extends Component {
181
255
182
256
// root.children.forEach(collapse);
183
257
update ( root ) ;
258
+ duration = 750 ;
184
259
185
260
// d3.select(self.frameElement).style("height", "800px");
186
261
187
262
}
188
263
189
264
render ( ) {
190
- return (
191
- < div ref = "anchor" className = { 'd3Container' } width = { '100%' } >
192
- </ div >
193
- )
265
+ return < div ref = "anchor" className = { 'd3Container' } width = { '100%' } > </ div >
194
266
}
195
267
}
196
268
0 commit comments