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

306
Visualizações
D3v6 how to properly zoom lines and circles in force directed graph

I have based my code on this graph and I'm having problems with zooming in.

Before zooming in it looks like

enter image description here

but after zoom in things get messed up

enter image description here

My modified code looks like this

const simulation = d3.forceSimulation(nodes)
      .force('link', forceLink)
      .force('charge', forceNode)
      .force('center',  d3.forceCenter())
      .on('tick', ticked);

    const svg = d3.select('figure#' + this.graphId)
      .append('svg')
      .attr('viewBox', [-width / 2, -height / 2, width, height])
      .attr('style', 'max-width: 100%; height: auto; height: intrinsic;');

    const link = svg.append('g')
      .attr('stroke', typeof linkStroke !== 'function' ? linkStroke : null)
      .attr('stroke-opacity', typeof linkStrokeOpacity !== 'function' ? linkStrokeOpacity : null)
      .attr('stroke-width', typeof linkStrokeWidth !== 'function' ? linkStrokeWidth : null)
      .attr('stroke-linecap', linkStrokeLinecap)
      .selectAll('line')
      .data(links)
      .join('line');

    const node = svg.append('g')
      .attr('fill', nodeFill)
      .attr('stroke', nodeStroke)
      .attr('stroke-opacity', nodeStrokeOpacity)
      .attr('stroke-width', nodeStrokeWidth)
      .selectAll('g')
      .data(nodes)
      .join('g')
      .call(drag(simulation));

    const tooltip = svg.append('text')
      .attr('visibility', 'hidden')
      .attr('opacity', 0)
      .attr('stroke', 'white')
      .attr('stroke-width', 0.2)
      .attr('fill', 'black')
      .attr('font-size', 22)
    ;

    const circle =  node.append('circle')
      .attr('r', typeof nodeRadius !== 'function' ? nodeRadius : null)
    ;



    //
    // Zoom related stuff
    //

    function zoomed(e: any): void {
      node.attr('transform', e.transform);
      link.attr('transform', e.transform);
      circle.attr('transform', e.transform);
      tooltip.attr('transform', e.transform);
    }

    const zoomHandler = d3.zoom()
      .on('zoom', zoomed)
      .scaleExtent([1, 5])
    ;

    svg.call(zoomHandler);

What am I not doing right and how can I fix it?

Any help would be appreciated!

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