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

98
Visualizações
D3 Drag Axis and Zoom Chart

I finally almost got it working! The X and Y axes on my scatter plot each have a separate d3.drag()behavior that adjusts the scale and the plot itself calls d3.zoom() for panning/zooming. I can pan perfectly but the zooming still isn't right.

See here: codepen

Typically, zooming is done by setting the scale's domain to a rescaled version of some default x and y scales (x2, y2) domains that are set initially.

 x.domain(d3.event.transform.rescaleX(x2).domain());          
 y.domain(d3.event.transform.rescaleY(y2).domain());

This works fine by itself but in combination with other zoom and drag behaviors the rescaling causes the zoom state of the plot to be out of sync which makes the plot jump around when panning or zooming.

To circumvent this, each zoom/drag instance directly modifies the scales domains. Everything works well however zooming in and zooming out doesn't set the domains correctly. I believe I need to implement the d3.event.transform.k value into calculating the delta somehow. Zooming is possible but it then goes wrong.

    // ZOOMING
    if (d3.event.sourceEvent && d3.event.sourceEvent.type == "wheel") {
      
      let domainX = x.domain();      
      let linearX = d3.scaleLinear().domain(x.range()).range([0, domainX[0] - domainX[1]]);
      let deltaX = linearX(t.x - lastX);      
            
      let domainY = y.domain();
      let linearY = d3.scaleLinear().domain(y.range()).range([domainY[1] - domainY[0], 0]);
      let deltaY = linearY(t.y - lastY)
      
      if (t.k > lastK) {
        // ZOOM IN
        x.domain([domainX[0] - deltaX, domainX[1] + deltaX]); 
        y.domain([domainY[0] + deltaY, domainY[1] - deltaY]);
        
      } else {
        // ZOOM OUT
        x.domain([domainX[0] + deltaX, domainX[1] - deltaX]);         
        y.domain([domainY[0] - deltaY, domainY[1] + deltaY]);
      } 
              
    }

Please help! Thank you.

D3fc is used here but it's not important.

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