Skip to content

Commit 7d637a2

Browse files
author
Robert Jackson
committed
Enable expand/collapse (and stuff).
1 parent cf23e39 commit 7d637a2

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

app/components/basic-tree.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Ember from 'ember';
22

3-
import fetch from "ember-network/fetch";
4-
53
// Import the D3 packages we want to use
64
import { select, event } from 'd3-selection';
75
import { tree, cluster, hierarchy } from 'd3-hierarchy';
86
import { zoom, zoomIdentity } from 'd3-zoom';
97

10-
const { $, run, get } = Ember;
8+
const { run, get } = Ember;
9+
10+
const DURATION = 500;
1111

1212
// copied these functions temporarily from `broccoli-viz` here:
1313
// https://github.com/ember-cli/broccoli-viz/blob/master/lib/node-by-id.js
@@ -30,23 +30,10 @@ export default Ember.Component.extend({
3030
},
3131

3232
didReceiveAttrs() {
33-
let graphPath = get(this, 'graphPath');
3433
let graphData = get(this, 'graphData');
3534

36-
if (this._lastGraphPath !== graphPath && graphPath) {
37-
fetch(graphPath)
38-
.then((response) => {
39-
return response.json();
40-
})
41-
.then((response) => this.processRawData(response));
42-
43-
this._lastGraphPath = graphPath;
44-
}
45-
4635
if (this._lastGraphData !== graphData && graphData) {
47-
let response = JSON.parse(graphData);
48-
49-
this.processRawData(response);
36+
this.processRawData(graphData);
5037

5138
this._lastGraphData = graphData;
5239
}
@@ -78,39 +65,31 @@ export default Ember.Component.extend({
7865
return node.children
7966
.map((childId) => data.nodesById[childId])
8067
.filter(this.nodeFilter);
81-
}).sum(d => d.stats.time.self);
68+
})
69+
.sum(d => d.stats.time.self);
70+
71+
// make debugging easier, this is dumb though
72+
self.root = root;
8273

83-
let graph = tree()
74+
let graph = cluster()
8475
.separation((a,b) => {
8576
return a.parent == b.parent ? 4 : 8;
8677
})
8778
.nodeSize([6, 180]);
8879
graph(root);
8980

9081
function update(source) {
91-
let link = g.selectAll(".link")
92-
.data(root.descendants().slice(1));
93-
94-
link.enter()
95-
.append("path")
96-
.attr("class", "link")
97-
.attr("d", function(d) {
98-
return "M" + d.y + "," + d.x
99-
+ "C" + (d.parent.y + 50) + "," + d.x
100-
+ " " + (d.parent.y + 50) + "," + d.parent.x
101-
+ " " + d.parent.y + "," + d.parent.x;
102-
});
103-
104-
let node = g.selectAll(".node")
105-
.data(root.descendants());
82+
let nodes = root.descendants();
83+
let links = root.links();
84+
let node = g
85+
.selectAll(".node")
86+
.data(nodes, d => d.data._id);
10687

10788
let nodeEnter = node
10889
.enter()
10990
.append("g")
11091
.attr("class", 'node')
111-
.attr("transform", function(d) {
112-
return "translate(" + d.y + "," + d.x + ")";
113-
})
92+
.attr("transform", d => `translate(${d.y},${d.x})`)
11493
.on('click', (d) => {
11594
// Toggle children on click.
11695
if (d.children) {
@@ -135,44 +114,65 @@ export default Ember.Component.extend({
135114
// .attr('stroke-width', 1)
136115
// .style('fill', "#fff");
137116

138-
nodeEnter.append('text')
117+
nodeEnter
118+
.append('text')
139119
.attr('dy', '-1.1em')
140120
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
141121
.text(d => d.data._id);
142122

143-
nodeEnter.append("text")
123+
nodeEnter
124+
.append("text")
144125
.attr("dy", '0.1em')
145126
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
146127
.text(function(d) {
147128
return d.data.id.name;
148129
});
149130

150-
nodeEnter.append("text")
131+
nodeEnter
132+
.append("text")
151133
.attr("dy", '1.1em')
152134
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
153135
.text(function(d) {
154136
return `total: ${(d.value / 1000000).toFixed(2)}`;
155137
});
156138

157-
nodeEnter.append("text")
139+
nodeEnter
140+
.append("text")
158141
.attr("dy", '2.1em')
159142
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
160143
.text(function(d) {
161144
return `self: ${(d.data.stats.time.self / 1000000).toFixed(2)}`;
162145
});
163146

164147
// Transition exiting nodes to the parent's new position.
165-
node.exit()
148+
node
149+
.exit()
166150
.transition()
167-
.duration(50)
151+
.duration(DURATION)
168152
.attr("transform", function () {
169153
return "translate(" + source.x + "," + source.y + ")";
170154
})
171155
.remove();
172156

173-
link.exit()
157+
let link = g
158+
.selectAll(".link")
159+
.data(links, d => d.target.data._id);
160+
161+
link
162+
.enter()
163+
.append("path")
164+
.attr("class", "link")
165+
.attr("d", function(d) {
166+
return "M" + d.target.y + "," + d.target.x
167+
+ "C" + (d.source.y + 50) + "," + d.target.x
168+
+ " " + (d.source.y + 50) + "," + d.source.x
169+
+ " " + d.source.y + "," + d.source.x;
170+
});
171+
172+
link
173+
.exit()
174174
.transition()
175-
.duration(50)
175+
.duration(DURATION / 2)
176176
.attr("transform", function () {
177177
return "translate(" + source.x + "," + source.y + ")";
178178
})

0 commit comments

Comments
 (0)