Is there a proper way to position nodes at specific coordinates using simulation force in D3.js?
I am trying to place nodes at specific positions with forceX/Y simulation in D3.js. My understanding is that setting the strength to 0 should result in no force being applied to the X and Y coordinates, allowing the nodes to be placed at the defined positions. What would be the correct approach to achieve this?
drag_nodes()
function add_grids(g,width,height) {
var m = 10
var n = 10
var xScale = d3.scaleLinear()
.domain([0,m])
.range([0, width])
var yScale = d3.scaleLinear()
.domain([0,n])
.range([0,height])
var xaxis = g.append('g')
.attr('class','xaxis')
.call(d3.axisTop(xScale)
.tickFormat((d,i) => null) //i==0 ? null:d)
.tickSizeOuter(0)
.tickSize(0)
//.tickValues([])
)
.call(g => g.select(".domain").remove())
var yaxis = g.append('g')
.attr('class','yaxis')
.call(d3.axisLeft(yScale)
.tickFormat((d,i) => null)//i==0 ? null:d)
.tickSizeOuter(0)
.tickSize(0)
//.tickValues([])
)
.call(g => g.select(".domain").remove())
g.selectAll("g.yaxis g.tick")
.append("line")
.attr("class", "gridline")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", width)
.attr("y2", 0)
.attr("stroke", "#9ca5aecf") // line color
//.attr("stroke-dasharray","4") // make it dashed;;
g.selectAll("g.xaxis g.tick")
.append("line")
.attr("class", "gridline")
.attr("x1", 0)
.attr("y1", height)
.attr("x2", 0)
.attr("y2", 0)
.attr("stroke", "#9ca5aecf") // line color
}
function drag_nodes() {
var data = {
nodes: [{
name: "A",
x: 100,
y: 100
}, {
name: "B",
x: 100,
y: 400
}, {
name: "C",
x: 400,
y: 400
}, {
name: "D",
x: 400,
y: 100
}],
links: [{
source: 'A',
target: 'B'
}, {
source: 'B',
target: 'C'
}, {
source: 'C',
target: 'D'
}, ]
};
var width = 500
var height = 500
var colors = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.style('border','2px solid red')
add_grids(svg,width,height)
var drag = d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
function dragstarted(event,d) {
if (!event.active) simulation.alphaTarget(.1).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event,d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event,d) {
if (!event.active) simulation.alphaTarget(.1);
d.fx = null;
d.fy = null;
}
var link = svg.selectAll(".link")
.data(data.links)
.enter()
.append("line")
.attr("class", "link")
.attr("fill", "none")
.attr("stroke", "black");
var node = svg.selectAll(".node")
.data(data.nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) {
return d.x
})
.attr("cy", function(d) {
return d.y
})
.attr("r", 15)
.attr("fill", function(d, i) {
return colors(i);
})
.call(drag);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.name; }))
.force('x', d3.forceX(d => d.x).strength(0))
.force('y', d3.forceY(d => d.y).strength(0))//.strength(1))
.alpha(1).restart();
simulation.nodes(data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(data.links);
function ticked() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
}
}
<script src="https://d3js.org/d3.v7.min.js"></script>