@@ -68,6 +68,7 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
68
68
. sort ( function ( a , b ) { return a . order - b . order ; } )
69
69
( data ) ;
70
70
71
+ // Remove the fake root node
71
72
nodes = nodes . filter ( function ( d ) { return d . id !== null ; } ) ;
72
73
73
74
var x = d3 . scale . linear ( )
@@ -79,6 +80,7 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
79
80
. ___domain ( [ 0 , nodes . length ] )
80
81
. range ( [ 0 , height ] ) ;
81
82
83
+ // Pre-compute the target x, y coordinates for each bar
82
84
nodes . forEach ( function ( d , i ) {
83
85
d . x = x ( d . startMillis ) ;
84
86
d . y = y ( i ) ;
@@ -102,72 +104,52 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
102
104
. enter ( )
103
105
. append ( 'g' )
104
106
. classed ( 'trace' , true )
105
- . classed ( 'component' , function ( d ) { return d . parent . id !== null ; } )
106
107
. classed ( 'composite' , function ( d ) { return d . children || d . _children ; } )
107
108
. style ( 'opacity' , 1e-6 )
108
109
. on ( 'mouseover' , mouseover )
109
110
. on ( 'mouseout' , mouseout ) ;
110
111
111
- bars . classed ( 'collapsed' , function ( d ) { return d . _children ; } ) ;
112
-
113
- // Append hierarchy lines before we draw the bars
112
+ // Add expand / collapse icon and lines indicating parent / child relationships
114
113
barsEnter . each ( function ( d ) {
114
+ var elem = d3 . select ( this ) ;
115
115
if ( d . children || d . _children ) {
116
- d3 . select ( this ) . append ( 'path' )
117
- . attr ( 'd' , 'M10 5 L0 10 L0 0 Z' ) ;
116
+ elem . append ( 'path' ) . attr ( 'd' , 'M10 5 L0 10 L0 0 Z' ) ;
117
+ elem . append ( 'line' )
118
+ . classed ( 'vertical' , true )
119
+ . attr ( 'x1' , - 5 )
120
+ . attr ( 'y1' , BAR_HEIGHT / 2 )
121
+ . attr ( 'x2' , - 5 )
122
+ . attr ( 'y2' , BAR_HEIGHT / 2 ) ;
118
123
} else if ( d . parent . id !== null ) {
119
- d3 . select ( this ) . append ( 'circle' )
124
+ elem . append ( 'circle' )
120
125
. attr ( 'r' , 4 )
121
126
. attr ( 'cx' , - 5 )
122
127
. attr ( 'cy' , BAR_HEIGHT / 2 ) ;
123
128
}
124
- } ) ;
125
129
126
- barsEnter . append ( 'line' )
127
- . classed ( 'vertical' , true )
128
- . attr ( 'x1' , - 5 )
129
- . attr ( 'y1' , BAR_HEIGHT / 2 )
130
- . attr ( 'x2' , - 5 )
131
- . attr ( 'y2' , BAR_HEIGHT / 2 ) ;
130
+ if ( d . parent . id !== null ) {
131
+ elem . append ( 'line' )
132
+ . classed ( 'horizontal' , true )
133
+ . attr ( 'x1' , - 5 )
134
+ . attr ( 'y1' , BAR_HEIGHT / 2 )
135
+ . attr ( 'x2' , - 5 )
136
+ . attr ( 'y2' , BAR_HEIGHT / 2 ) ;
137
+ }
138
+ } ) ;
132
139
133
- barsEnter . append ( 'line' )
134
- . classed ( 'horizontal' , true )
135
- . attr ( 'x1' , - 5 )
136
- . attr ( 'y1' , BAR_HEIGHT / 2 )
137
- . attr ( 'x2' , - 5 )
138
- . attr ( 'y2' , BAR_HEIGHT / 2 ) ;
139
-
140
- barsEnter
141
- . attr ( 'transform' , 'translate(' + rootCoords . x + ',' + rootCoords . y + ')' )
142
- . append ( 'rect' )
143
- . attr ( 'rx' , 3 )
144
- . attr ( 'width' , function ( d ) { return Math . max ( x ( d . runMillis ) , MIN_BAR_WIDTH ) ; } )
145
- . attr ( 'height' , function ( ) { return BAR_HEIGHT ; } )
146
- . each ( function ( d ) { d3 . select ( this ) . classed ( d . resultType . toLowerCase ( ) , true ) ; } )
147
- . style ( 'stroke-opacity' , 0 ) ;
148
-
149
- barsEnter
150
- . attr ( 'transform' , 'translate(' + rootCoords . x + ',' + rootCoords . y + ')' )
151
- . append ( 'rect' )
152
- . attr ( 'rx' , 3 )
153
- . attr ( 'width' , function ( d ) { return Math . max ( x ( d . totalMillis ) , MIN_BAR_WIDTH ) ; } )
154
- . attr ( 'height' , function ( ) { return BAR_HEIGHT ; } )
155
- . each ( function ( d ) { d3 . select ( this ) . classed ( d . resultType . toLowerCase ( ) , true ) ; } )
156
- . style ( 'fill-opacity' , 0.3 ) ;
140
+ drawBars ( barsEnter , function ( d ) { return x ( d . runMillis ) ; } )
141
+ . style ( 'stroke-opacity' , 0 ) ;
142
+
143
+ drawBars ( barsEnter , function ( d ) { return x ( d . totalMillis ) ; } )
144
+ . style ( 'fill-opacity' , 0.3 ) ;
145
+
146
+ bars . on ( 'click' , toggleCollapse ) ;
157
147
158
148
var textEnter = barsEnter
159
149
. append ( 'text' )
160
150
. attr ( 'dy' , '1.3em' )
161
151
. attr ( 'dx' , '0.5em' ) ;
162
152
163
- textEnter
164
- . append ( 'tspan' )
165
- . classed ( 'collapse-toggle' , true )
166
- . style ( 'font-family' , 'monospace' ) ;
167
-
168
- bars
169
- . on ( 'click' , toggleCollapse ) ;
170
-
171
153
textEnter
172
154
. append ( 'tspan' )
173
155
. text ( function ( d ) { return d . name + ' (' + util . alignMillis ( d . totalMillis ) + ' ms)' ; } ) ;
@@ -182,51 +164,26 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
182
164
d . deleted = true ;
183
165
} ) ;
184
166
185
- createTransition ( bars )
167
+ createTransition ( bars , transitionTime )
186
168
. selectAll ( 'path' )
187
169
. attr ( 'transform' , function ( d ) {
188
170
return 'translate(' + ( d . children ? 0 : - 10 ) + ',' + ( BAR_HEIGHT / 2 - 5 ) + ') ' +
189
171
'rotate(' + ( d . children ? 90 : 0 ) + ')' ;
190
172
} ) ;
191
- createTransition ( bars . exit ( ) )
173
+
174
+ createTransition ( bars . exit ( ) , transitionTime )
192
175
. each ( 'end' , function ( ) {
193
176
d3 . select ( this . remove ( ) ) ;
194
177
} ) ;
195
178
196
- function createTransition ( selection ) {
197
- var transition = selection
198
- . style ( 'pointer-events' , 'none' )
199
- . transition ( )
200
- . duration ( transitionTime )
201
- . attr ( 'transform' , function ( d ) { return 'translate(' + d . x + ',' + d . y + ')' ; } )
202
- . style ( 'opacity' , alpha ) ;
203
-
204
- transition
205
- . each ( 'end' , function ( ) {
206
- d3 . select ( this ) . style ( 'opacity' , alpha ) ;
207
- d3 . select ( this ) . style ( 'pointer-events' , undefined ) ;
208
- } ) ;
209
-
210
- transition . selectAll ( 'line.horizontal' )
211
- . attr ( 'x2' , function ( d ) {
212
- var delta = 0 ;
213
- // Check for id is important since we have a fake node at the root
214
- if ( d . parent . id !== null ) {
215
- delta = d . parent . x - d . x ;
216
- }
217
- return delta - 4 ;
218
- } ) ;
219
-
220
- transition . selectAll ( 'line.vertical' )
221
- . attr ( 'y2' , function ( d ) {
222
- var delta = 0 ;
223
- if ( d . children ) {
224
- delta = Math . max . apply ( Math , d . children . map ( function ( c ) { return c . y ; } ) ) - d . y ;
225
- }
226
- return delta + BAR_HEIGHT / 2 ;
227
- } ) ;
228
-
229
- return transition ;
179
+ function drawBars ( selection , widthFunction ) {
180
+ return selection
181
+ . attr ( 'transform' , 'translate(' + rootCoords . x + ',' + rootCoords . y + ')' )
182
+ . append ( 'rect' )
183
+ . attr ( 'rx' , 3 )
184
+ . attr ( 'width' , function ( d ) { return Math . max ( widthFunction ( d ) , MIN_BAR_WIDTH ) ; } )
185
+ . attr ( 'height' , function ( ) { return BAR_HEIGHT ; } )
186
+ . each ( function ( d ) { d3 . select ( this ) . classed ( d . resultType . toLowerCase ( ) , true ) ; } ) ;
230
187
}
231
188
232
189
function mouseover ( d ) {
@@ -253,26 +210,62 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
253
210
}
254
211
redraw ( svg , vis , data , ancestors , TRANSITION_TIME , { x : x ( d . startMillis ) , y : y ( i ) } ) ;
255
212
}
213
+ }
256
214
257
- function recursiveCollapse ( d , collapse ) {
258
- collapseOne ( d , collapse ) ;
259
- var children = d . children || d . _children || [ ] ;
260
- children . forEach ( function ( child ) {
261
- recursiveCollapse ( child , collapse ) ;
215
+ function createTransition ( selection , transitionTime ) {
216
+ var transition = selection
217
+ . style ( 'pointer-events' , 'none' )
218
+ . transition ( )
219
+ . duration ( transitionTime )
220
+ . attr ( 'transform' , function ( d ) { return 'translate(' + d . x + ',' + d . y + ')' ; } )
221
+ . style ( 'opacity' , alpha ) ;
222
+
223
+ transition
224
+ . each ( 'end' , function ( ) {
225
+ d3 . select ( this ) . style ( 'opacity' , alpha ) ;
226
+ d3 . select ( this ) . style ( 'pointer-events' , undefined ) ;
262
227
} ) ;
263
- }
264
228
265
- function collapseOne ( d , collapse ) {
266
- if ( collapse ) {
267
- if ( d . children ) {
268
- d . _children = d . children ;
269
- delete d . children ;
229
+ transition . selectAll ( 'line.horizontal' )
230
+ . attr ( 'x2' , function ( d ) {
231
+ var delta = 0 ;
232
+ // Check for id is important since we have a fake node at the root
233
+ if ( d . parent . id !== null ) {
234
+ delta = d . parent . x - d . x ;
270
235
}
271
- } else {
272
- if ( d . _children ) {
273
- d . children = d . _children ;
274
- delete d . _children ;
236
+ return delta - 4 ;
237
+ } ) ;
238
+
239
+ transition . selectAll ( 'line.vertical' )
240
+ . attr ( 'y2' , function ( d ) {
241
+ var delta = 0 ;
242
+ if ( d . children ) {
243
+ delta = Math . max . apply ( Math , d . children . map ( function ( c ) { return c . y ; } ) ) - d . y ;
275
244
}
245
+ return delta + BAR_HEIGHT / 2 ;
246
+ } ) ;
247
+
248
+ return transition ;
249
+ }
250
+
251
+ function recursiveCollapse ( d , collapse ) {
252
+ collapseOne ( d , collapse ) ;
253
+ var children = d . children || d . _children || [ ] ;
254
+ children . forEach ( function ( child ) {
255
+ recursiveCollapse ( child , collapse ) ;
256
+ } ) ;
257
+ }
258
+
259
+ function collapseOne ( d , collapse ) {
260
+ if ( collapse ) {
261
+ if ( d . children ) {
262
+ d . _children = d . children ;
263
+ delete d . children ;
264
+ }
265
+ } else {
266
+ if ( d . _children ) {
267
+ d . children = d . _children ;
268
+ delete d . _children ;
276
269
}
277
270
}
278
271
}
0 commit comments