I'm trying to render a d3js force simulation but I'd like to ensure my nodes don't relay false information.
With the following code used to display the nodes but due to the dynamic nature of force layouts, it occasionally pushes some nodes out of its appropriate x-coordinate location.
inOrder(){
this.simulation
.force("x", d3.forceX(d => this.xScale(d.value)))
.force("y", d3.forceY(this.height / 2))
.alpha(1).restart();
},
Here is an egregious example of this happening:
The numbers should be in order from left to right.
I made an attempt to use the fx property on a node to lock the position in place:
inOrder(){
this.releases.forEach(x => {
x.fx = this.xScale(x.value)
})
this.simulation
.force("x", d3.forceX(d => this.xScale(d.value)))
.force("y", d3.forceY(this.height / 2))
.alpha(1).restart();
},
This works as intended for preserving the x position but when the inOrder
method is called, the nodes instantly jump to their final x position. This ruins the fluid and dynamic nature of the force simulation.
Is there a way to get the best of both worlds? Perhaps by using the .on("end", () => {})
or the .on("tick", () => {})
? event handlers?
Mike Bostock (https://stackoverflow.com/users/365814/mbostock) and Shan Carter created some of the work that serves as the inspiration to what I'm trying to do here:
Click between the Changes and Department totals tabs https://archive.nytimes.com/www.nytimes.com/interactive/2012/02/13/us/politics/2013-budget-proposal-graphic.html?hp
Click betweeen The Overall Picture and the View By Industry tabs https://archive.nytimes.com/www.nytimes.com/interactive/2013/05/25/sunday-review/corporate-taxes.html