Reputation: 91
I am very new to D3js and I am very lost. I was assigned to do a task and I am trying a lot of things but nothings seems to work. I am using this module https://github.com/d3/d3-force#forces
The task is: if there is a negative weight between two nodes they should be that weight distance apart. For example nodes u and v are close if their weight is positive, and they are far apart if the weight is negative. Which means the bigger the weight the closer the nodes, and the smaller the weight the further they are.
Some parts of the code are already ready and I think this is the part where I need to fix it.
// extract static links
var links = [];
var links_by_id = {};
temporal_net.links.forEach(function(link){
id = String(link.source + '-' + link.target);
edgesbytime[link.time].push(id);
l = {
'source': link.source,
'target': link.target,
'id': id,
'strength': 0,
'weight': [-2000, 2000] // changed here
};
if (!contains(links, l)){
links.push(l);
links_by_id[l.id] = l;
}
});
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody().strength(-30).distanceMax(200))
.force("repelForce", d3.forceManyBody().strength(function(d) {return array[Math.floor(Math.random() * d.weight.length)]}).distanceMax(2000))
// .force("repelForce", d3.forceManyBody().strength(-100).distanceMax(200))
.force("center", d3.forceCenter(width / 2, height / 2))
.alphaTarget(0.1)
.on("tick", ticked);
To test if it is working I put weights either 2000 or -2000 just to see how far apart nodes are, but it seems not to work. I am choosing randomly between these two weights. Thank you for the help
Upvotes: 1
Views: 366