Skip to content

Commit 42231b4

Browse files
authored
Merge pull request #17 from wwlib/feature/andrewrapo/d3-force-improvement
Updated d3-force so that it doesn't render until force solution is complete
2 parents 469b6ed + 4fce876 commit 42231b4

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

src/components/graphDiagram/GraphEditor.tsx

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -334,22 +334,13 @@ export default class GraphEditor extends React.Component < GraphEditorProps, Gra
334334
let height: number = svgElement ? svgElement.clientHeight / 2 : this.props.appModel.appDimensions.height / 2;
335335

336336
// https://bl.ocks.org/wnghdcjfe/c2b04ee8430afa32ce76596daa4d8123
337-
simulation = d3Force.forceSimulation()
338-
.force("link", d3Force.forceLink().id(function(d: any) { return d.index })) //.distance((d:any) => {return d.source.r + d.target.r + 45}).strength(1))
339-
.force("collide",d3Force.forceCollide( function(d: any){return d.r + 25 }))
340-
.force("charge", d3Force.forceManyBody()) //.strength(-5000).distanceMin(500).distanceMax(2000))
341-
.force("center", d3Force.forceCenter(width, height))
342-
.force("y", d3Force.forceY(0.001))
343-
.force("x", d3Force.forceX(0.001))
344-
345-
// simulation = d3Force.forceSimulation(simData.nodes)
346-
// .force("link", d3Force.forceLink(simData.links).id(function(d: any) { return d.index })) //.distance((d:any) => {return d.source.r + d.target.r + 45}).strength(1))
347-
// .force("charge", d3Force.forceManyBody().strength(-1500))
348-
// .force('center', d3Force.forceCenter(width, height))
349-
// .force("x", d3Force.forceX())
350-
// .force("y", d3Force.forceY())
351-
// .stop()
352-
// .tick(300)
337+
// simulation = d3Force.forceSimulation()
338+
// .force("link", d3Force.forceLink().id(function(d: any) { return d.index })) //.distance((d:any) => {return d.source.r + d.target.r + 45}).strength(1))
339+
// .force("collide",d3Force.forceCollide( function(d: any){return d.r + 25 }))
340+
// .force("charge", d3Force.forceManyBody()) //.strength(-5000).distanceMin(500).distanceMax(2000))
341+
// .force("center", d3Force.forceCenter(width, height))
342+
// .force("y", d3Force.forceY(0.001))
343+
// .force("x", d3Force.forceX(0.001))
353344

354345
// from neo4j-browser
355346
// linkDistance = 45
@@ -376,23 +367,24 @@ export default class GraphEditor extends React.Component < GraphEditorProps, Gra
376367
.attr( "display", "none");
377368
}
378369

379-
// simLinks = svg_g.select( "g.layer.nodes" ).append("g")
380-
// .attr("class", "links")
381-
// .selectAll("line")
382-
// .data(simData.links)
383-
// .enter().append("line")
384-
// .attr("stroke-width", function(d: any) { return 1; });
370+
simulation = d3Force.forceSimulation(simData.nodes)
371+
.force("link", d3Force.forceLink(simData.links).id(function(d: any) { return d.index })) //.distance((d:any) => {return d.source.r + d.target.r + 45}).strength(1))
372+
.force("charge", d3Force.forceManyBody().strength(-1500))
373+
.force('center', d3Force.forceCenter(width, height))
374+
.force("x", d3Force.forceX())
375+
.force("y", d3Force.forceY())
376+
.stop()
377+
.tick(300)
385378

386-
simulation
387-
.nodes(simData.nodes)
388-
.on("tick", thiz.ticked)
389-
.on("end", thiz.ended);
379+
// simulation
380+
// .nodes(simData.nodes)
381+
// .on("tick", thiz.ticked)
382+
// .on("end", thiz.ended);
390383

391-
simulation.force("link")
392-
.links(simData.links);
384+
// simulation.force("link")
385+
// .links(simData.links);
393386

394-
// thiz.ticked();
395-
// thiz.ended();
387+
this.updateAndRedrawNodes();
396388
}
397389

398390
ticked() {
@@ -403,9 +395,9 @@ export default class GraphEditor extends React.Component < GraphEditorProps, Gra
403395
// .attr("x2", function(d: any) { return d.target.x; })
404396
// .attr("y2", function(d: any) { return d.target.y; });
405397

406-
simNodes
407-
.attr("cx", function(d: any) { return d.x; })
408-
.attr("cy", function(d: any) { return d.y; });
398+
// simNodes
399+
// .attr("cx", function(d: any) { return d.x; })
400+
// .attr("cy", function(d: any) { return d.y; });
409401

410402
simulationTickCount++
411403
if (simulationTickCount >= simulationMaxTicks) {
@@ -415,6 +407,10 @@ export default class GraphEditor extends React.Component < GraphEditorProps, Gra
415407

416408
ended() {
417409
simulation.stop();
410+
thiz.updateAndRedrawNodes();
411+
}
412+
413+
updateAndRedrawNodes() {
418414
simData.nodes.forEach((node: any) => {
419415
node.layoutNode.x = node.x;
420416
node.layoutNode.y = node.y;
@@ -431,7 +427,7 @@ export default class GraphEditor extends React.Component < GraphEditorProps, Gra
431427
svg_g.select( "g.layer.nodes" ).selectAll( "g.caption")
432428
.attr( "display", "block");
433429
}
434-
thiz.draw();
430+
this.draw();
435431
}
436432

437433
draw()

0 commit comments

Comments
 (0)