Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!