Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
D3.js repel or pull nodes closer depending on the edge weight

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!