Using d3, I created a histogram that displays the text value of each line. I switch between two different datasets through the click event on the button. Datasets are successfully resized in mousedown, i.e. the histograms are resized, as one would expect, but I cannot change the text labels on the columns. My redrawText function does nothing, and calling the drawText function again simply redraws the data on top of the previous label (as you would expect). I am looking for a way to remove an old label and redraw a label reflecting the new data inside my removeText function.
Here is my drawText function, which is originally created to create a label. 'datachoose' is the name of the variable that is selected to display the correct data set.
function drawText(dataChoose) { new_svg.selectAll("text.dataChoose") .data(dataChoose) .enter().append("text") .text(function(d) { return d; }) .attr("fill", "white") .attr("style", "font-size: 12; font-family: Garamond, sans-serif"); }
Here are the relevant parts of my mousedown event handler, which is used to update the dataset and redraw the chart:
.on("mousedown", function() { if (dataChoose == data) { dataChoose = data2; } else { dataChoose = data; } redraw(dataChoose); redrawText(dataChoose); });
and here is my redrawText () function
function redrawText(dataChoose) { var new_text = new_svg.selectAll("text.dataChoose") .data(dataChoose); new_text.transition() .duration(1000) .text(function(d) { return d; }) .attr("fill", "white") .attr("style", "font-size: 16; font-family: Garamond, sans-serif"); }
source share