Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda