Skip to content

Commit 3413229

Browse files
Some refactoring / cleanup for tracevis waterfall view
1 parent 21a69a0 commit 3413229

File tree

2 files changed

+90
-109
lines changed

2 files changed

+90
-109
lines changed

tools/tracevis/css/waterfall.css

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,18 @@
66
.waterfallview .trace rect.early_finish { fill: #FFFD76; }
77

88
.waterfallview .trace circle {
9-
opacity: 0;
109
fill: #FFFFFF;
1110
stroke: #333;
1211
stroke-width: 1px;
1312
}
1413

15-
.waterfallview .trace.composite circle,
16-
.waterfallview .trace.component circle {
17-
opacity: 1;
18-
}
19-
2014
.waterfallview .trace line {
21-
opacity: 0;
2215
stroke: #333;
2316
stroke-width: 1px;
2417
stroke-linecap: round;
2518
stroke-dasharray: 3;
2619
}
2720

28-
.waterfallview .trace.composite line,
29-
.waterfallview .trace.component line {
30-
opacity: 1;
31-
}
32-
3321
.waterfallview .trace text,
3422
.waterfallview .trace text tspan {
3523
fill: #333;

tools/tracevis/lib/render/waterfall.js

Lines changed: 90 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
6868
.sort(function(a, b) { return a.order - b.order; })
6969
(data);
7070

71+
// Remove the fake root node
7172
nodes = nodes.filter(function(d) { return d.id !== null; });
7273

7374
var x = d3.scale.linear()
@@ -79,6 +80,7 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
7980
.___domain([0, nodes.length])
8081
.range([0, height]);
8182

83+
// Pre-compute the target x, y coordinates for each bar
8284
nodes.forEach(function(d, i) {
8385
d.x = x(d.startMillis);
8486
d.y = y(i);
@@ -102,72 +104,52 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
102104
.enter()
103105
.append('g')
104106
.classed('trace', true)
105-
.classed('component', function(d) { return d.parent.id !== null; })
106107
.classed('composite', function(d) { return d.children || d._children; })
107108
.style('opacity', 1e-6)
108109
.on('mouseover', mouseover)
109110
.on('mouseout', mouseout);
110111

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
114113
barsEnter.each(function(d) {
114+
var elem = d3.select(this);
115115
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);
118123
} else if (d.parent.id !== null) {
119-
d3.select(this).append('circle')
124+
elem.append('circle')
120125
.attr('r', 4)
121126
.attr('cx', -5)
122127
.attr('cy', BAR_HEIGHT / 2);
123128
}
124-
});
125129

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+
});
132139

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);
157147

158148
var textEnter = barsEnter
159149
.append('text')
160150
.attr('dy', '1.3em')
161151
.attr('dx', '0.5em');
162152

163-
textEnter
164-
.append('tspan')
165-
.classed('collapse-toggle', true)
166-
.style('font-family', 'monospace');
167-
168-
bars
169-
.on('click', toggleCollapse);
170-
171153
textEnter
172154
.append('tspan')
173155
.text(function(d) { return d.name + ' (' + util.alignMillis(d.totalMillis) + ' ms)'; });
@@ -182,51 +164,26 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
182164
d.deleted = true;
183165
});
184166

185-
createTransition(bars)
167+
createTransition(bars, transitionTime)
186168
.selectAll('path')
187169
.attr('transform', function(d) {
188170
return 'translate(' + (d.children ? 0 : -10) + ',' + (BAR_HEIGHT / 2 - 5) + ') ' +
189171
'rotate(' + (d.children ? 90 : 0) + ')';
190172
});
191-
createTransition(bars.exit())
173+
174+
createTransition(bars.exit(), transitionTime)
192175
.each('end', function() {
193176
d3.select(this.remove());
194177
});
195178

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); });
230187
}
231188

232189
function mouseover(d) {
@@ -253,26 +210,62 @@ function redraw(svg, vis, data, ancestors, transitionTime, rootCoords) {
253210
}
254211
redraw(svg, vis, data, ancestors, TRANSITION_TIME, { x: x(d.startMillis), y: y(i) });
255212
}
213+
}
256214

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);
262227
});
263-
}
264228

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;
270235
}
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;
275244
}
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;
276269
}
277270
}
278271
}

0 commit comments

Comments
 (0)