Skip to content

Commit 518635a

Browse files
committed
start separating graph g elements...
1 parent 6c41b5c commit 518635a

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

js/GraphLayout.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const GraphLayout = (function() {
7676
d3.select(this.div).append('button')
7777
.text('⤆ reset ⤇')
7878
.on('click', () => {
79-
this.dx = this.dy = 0;
79+
this.dx = 0;
8080
this.adjustMargins();
8181
});
8282

@@ -85,14 +85,9 @@ const GraphLayout = (function() {
8585
.attr('width', this.bounds.width)
8686
.attr('height', this.bounds.height);
8787
this.g = this.svg.append('g');
88-
this.links = this.g.append('g')
89-
.attr('class','links');
90-
this.nodes = this.g.append('g')
91-
.attr('class','nodes');
9288

9389
// margins and drag event for positioning svg
9490
this.dx = 0;
95-
this.dy = 0;
9691
this.svg
9792
.call(d3.drag()
9893
.on('drag', () => {
@@ -117,17 +112,24 @@ const GraphLayout = (function() {
117112
}
118113
adjustMargins() {
119114
let bounds = this.div.getBoundingClientRect();
120-
let bbox = this.g.node().getBBox();
121115

122-
let x = 20 - bbox.x + this.dx;
123-
let y = bounds.height/2 - (bbox.height/2 + bbox.y) + this.dy;
116+
d3.selectAll('.group')
117+
.attr('transform', (d, i, el) => {
124118

125-
this.g.attr('transform', 'translate(' + [x, y] + ')');
119+
let bbox = el[i].getBBox();
120+
let y = -bbox.height / 2 - bbox.y;
121+
122+
return 'translate(' + [0, y] + ')';
123+
});
124+
125+
let bbox2 = this.g.node().getBBox();
126+
let x = 20 - bbox2.x + this.dx;
127+
128+
this.g.attr('transform', 'translate(' + [x, bounds.height / 2] + ')');
126129
}
127130
clear() {
128131
this.words = [];
129-
this.nodes.selectAll('*').remove();
130-
this.links.selectAll('*').remove();
132+
this.g.selectAll('*').remove();
131133
}
132134

133135
/**
@@ -204,14 +206,23 @@ const GraphLayout = (function() {
204206
}
205207

206208
updateGraph() {
207-
let links = this.links.selectAll('.linkGroup')
209+
let group = this.g.selectAll('.group')
208210
.data(this.data);
209211

210-
links.exit().remove();
211-
links.enter().append('g')
212-
.attr('class','linkGroup')
213-
.merge(links)
214-
.each((d, i, el) => {
212+
group.exit().remove();
213+
214+
let groupEnter = group.enter().append('g')
215+
.attr('class','group')
216+
217+
groupEnter.append('g')
218+
.attr('class', 'linkGroup');
219+
groupEnter.append('g')
220+
.attr('class', 'nodeGroup');
221+
222+
let groupMerge = groupEnter.merge(group);
223+
224+
groupMerge.select('.linkGroup')
225+
.datum((d, i, el) => {
215226
el = d3.select(el[i])
216227
.attr('transform', 'translate(' + d.offset + ', 0)');
217228
this.drawLinks(d.tree, el);
@@ -230,14 +241,8 @@ const GraphLayout = (function() {
230241
}
231242
});
232243

233-
let nodes = this.nodes.selectAll('.nodeGroup')
234-
.data(this.data);
235-
236-
nodes.exit().remove();
237-
nodes.enter().append('g')
238-
.attr('class','nodeGroup')
239-
.merge(nodes)
240-
.each((d, i, el) => {
244+
groupMerge.select('.nodeGroup')
245+
.datum((d, i, el) => {
241246
el = d3.select(el[i])
242247
.attr('transform', 'translate(' + d.offset + ', 0)');
243248

0 commit comments

Comments
 (0)