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

265
Views
D3 append text to circles on a line not working properly

I created some line charts on d3. After that I appended some circles to every 3rd element in the line. Now I want to append text to those circles.

// created a circle variable

var circles=g2
    .append("g")
    .attr("id","symbols-b")
    .selectAll("circles")
    .data(slicesCircle)
    .enter()
    .append("g")

    circles.style("fill", function(d){
      return d.color=color(d.id);
    })

//append circles

    circles
    .selectAll("circle")
    .data(function (d){return d.values})
    .enter()
    .filter((d,i)=>(i%3==0) && i>0) //to attach every 3rd datapoint
    .append("circle")
    .attr("r", 7.5)
    .attr("cx", function(d,i) {return xScale(d.date);})
    .attr("cy", function(d,i) {return yScale(d.measurement);})

// Using the circle variable again to append text
    circles
    .selectAll("circle")
    .data(function (d){return d.values})
    .enter()
    .filter((d,i)=>(i%3==0) && i>0)
    .append("text")
    .attr("x",function(d,i) {return xScale(d.date);})
    .attr("y",function(d,i) {return yScale(d.measurement);})
    .text("0") // testing with 0
    .style("stroke","white")
    .style("font-size","12px")

But when I am running the code, the last 2 blocks despite being the same are generating different outputs. The circles are being generated nicely but when I am appending text to the same block of code, it is beginning latertime series. Unable to understand why so..

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Changed .selectAll("circle") to .selelctAll("text") to select each text placeholder rather than circle. Worked

// Using the circle variable again to append text
    circles
    .selectAll("circle")
    .data(function (d){return d.values})
    .enter()
    .filter((d,i)=>(i%3==0) && i>0)
    .append("text")
    .attr("x",function(d,i) {return xScale(d.date);})
    .attr("y",function(d,i) {return yScale(d.measurement);})
    .text("0") // testing with 0
    .style("stroke","white")
    .style("font-size","12px")
// Using the circle variable again to append text
    circles
    .selectAll("text")
    .data(function (d){return d.values})
    .enter()
    .filter((d,i)=>(i%3==0) && i>0)
    .append("text")
    .attr("x",function(d,i) {return xScale(d.date);})
    .attr("y",function(d,i) {return yScale(d.measurement);})
    .text("0") // testing with 0
    .style("stroke","white")
    .style("font-size","12px")
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!