Creé algunos gráficos de líneas en d3. Después de eso, agregué algunos círculos a cada tercer elemento de la línea. Ahora quiero agregar texto a esos círculos.
// 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") Pero cuando estoy ejecutando el código, los últimos 2 bloques, a pesar de ser los mismos, generan salidas diferentes. Los círculos se generan muy bien, pero cuando agrego texto al mismo bloque de código, comienza más tarde.
. Incapaz de entender por qué..
Se cambió .selectAll("círculo") a .selelctAll("texto") para seleccionar cada marcador de posición de texto en lugar de círculo. Trabajó
// 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")