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

134
Vistas
updating existing d3 graph without creating a new one in vue 3

I have vue app which displays a rectangle "created" by d3 in a child component

the number of rect created depends on the number of values in data property of the parent
changing the data changes the exisitng rects in the child component

the data gets updated but with the correct number of recs coresponding to the data
what Im trying to do /solve prevent creating a "g" element

only idea so far is to use d3.remove to delete the existing "g" if a update ooccurs and create a new one ,

// parent

<template>
  <child :data="data" />

  <button type="button" @click="changeData">click to change</button>
</template>

<script>
import Child from "./components/child.vue";

export default {
  name: "App",
  components: {
    Child,
  },

  data() {
    return {
      data: [1],
    };
  },

  methods: {
    changeData() {
      this.data = [1, 2];
    },
  },
};
</script>



// child

<template>
  <div class="foo">
    <svg ref="svgRef"></svg>

    <h1>I have this prop</h1>
  </div>
</template>

<script>
import d3 from "@/assets/d3";

import { onMounted, ref, watchEffect } from "@vue/runtime-core";

export default {
  name: "Circles",
  props: ["data"],

  setup(props) {
    const svgRef = ref(null);

    onMounted(() => {
      watchEffect(() => {
        const selectSvg = d3.select(svgRef.value);

        selectSvg

          .selectAll("g")
          .data(Object.values(props.data))
          .join("g")

          .attr("transform", (d, i) => `translate(60, ${i * 21})`) // use attr instead of style
          .append("rect")
          .attr("width", 20 + "px")
          .attr("height", 20 + "px")
          .attr("fill", d3.color("orange"));
      });
    });

    return { svgRef };
  },
};
</script>

<style scoped>
.limit {
  max-width: 100px;
}
</style>



about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can add a class to the rect element and use it to remove by class.

now you can select and remove only the rect by the class using d3.select(".react-class").remove();

about 4 years ago · Juan Pablo Isaza Denunciar
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