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

249
Visualizações
force directed graph in d3

i am trying to make a force directed graph using d3 and i am in latest version v7.

  const width = 800;
const height = 800;

var layoutCanvas = d3
  .select("#graphDiv")
  .append("canvas")
  .attr("width", width + "px")
  .attr("height", height + "px")
  .node();

// this.setState({ canvasAdded: true });
let context = layoutCanvas!.getContext("2d")!;

if (!context) {
  console.log("no 2d context found");
  return;
}

var transform = d3.zoomIdentity;

d3.json("test1.json").then((data: any) => {
  console.log(data);
  const simulation = d3
    .forceSimulation(data.nodes)
    .force("charge", null)
    .force("center", d3.forceCenter())
    .force("link", d3.forceLink(data.edges));

  initGraph(data);

  function initGraph(data) {
    simulation.on("tick", simulationUpdate);

    function simulationUpdate() {
      context.save();
      context.clearRect(0, 0, width, height);
      context.translate(transform.x, transform.y);
      context.scale(transform.k, transform.k);

      data.edges.forEach(function (d) {
        context.beginPath();
        context.moveTo(d.source.x, d.source.y);
        context.lineTo(d.target.x, d.target.y);
        context.stroke();
      });

      // Draw the nodes
      data.nodes.forEach(function (d, i) {
        context.beginPath();
        context.arc(d.x, d.y, 2, 0, 2 * Math.PI, true);
        context.fillStyle = d.col ? "red" : "black";
        context.fill();
      });

      context.restore();
    }
  }
});

this is my code and the test1.json file is this:source

When i log that data that is read from the d3.json() function

enter image description here My major problem is that the canvas is 800x800 but when i log the node and edge position it shows above 800.

If anyone can help it would be appreciated.

Ps: the position X and Y in json is in between 0 and 1.

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