Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda