Hi I have a click button event to start a setinterval / clear interval animation of some text and to also add an image on a button click. I would like help in how to better stop and remove the animating text and remove the image when a new button is clicked. At the moment the text is replaced by an empty string when the array ends. the image just duplicates each time I click the button.
I have looked at examples that use exit().remove() or merge but I can't work out how to apply it in my case. My code uses D3 nest and is part of a much larger map project using svg and d3.line and other transitioning elements elements.
This is a simplified sample of my D3 code:
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v0.min.js"></script>
<style>
</style>
</head>
<body>
<div id ="container">
<div id ="mapdata"></div>
<div id = "buttons"></div>
</div>
<script>
d3.queue()
.defer(d3.csv, "data/testData.csv")
.await(ready);
function ready (error, data){
var dataGroup = d3.nest()
.key(function(d) {return d.year;}).sortKeys(d3.ascending)
.key(function(d){return d.VoyageID;}).sortKeys(d3.ascending)
.entries(data);
dataGroup.forEach(function(yearObject,i) {
var buttons = d3.select("#buttons").selectAll("button")
.data(dataGroup)
.enter()
.append("button")
.attr("class", "buttons")
.attr("data-group", function(d){return d.key})
.attr("id", function(d){return "button_" + d.key})
.append("label")
.text(function(d){return d.key;})
var testData = d3.select("#mapdata").selectAll(".voyageData")
.data(dataGroup)
.enter()
.append("div")
.attr("class", "voyageData")
.attr("data-group", function(d){return d.key})
.attr("id", function(d){return "voyageText_" + d.key})
var data = testData.selectAll(".data")
.data(function(d) {return d.values;})
.enter()
.append("div")
.attr("class", "data")
.attr("id", function(d){return "data_" + d.key})
var images = testData.selectAll(".images")
.data(function(d) {return d.values;})
images.exit().remove();
images.enter()
.append("div")
.attr("class", "images")
.attr("id", function(d){return "images_" + d.key})
//buttons
d3.select("#button_1906").on("click", function(d){
clickButton(d,i);
});
d3.select("#button_1845").on("click", function(d){
clickButton(d,i);
});
function clickButton(d,i) {
var voyageClass = d.key;
var voyageID = d.values[0].key;
//animate place name and dates
var j = 0;
var animation = setInterval(function(){
d3.select("#data_"+ voyageID)
.text(function(d) { return d.values[j].arrivalDateTxt +" "+d.values[j].placeName; });
j = j + 1;
if(j==d.values[0].values.length)
clearInterval(animation)
if(j==d.values[0].values.length)
d3.select("#data_"+ voyageID)
.text("") // is there a better way to stop or remove text on completion of animation? I am just replacing it with an empty string.
},1000);
//add image
d3.select("#images_" + voyageID)
.append("img")
.attr("src", function(d){return d.values[j].groupPic })
.attr("width", "40")
//how do I remove image on completion of animation or click of new button?
}
});
}
</script>
</body>
</html>
This is my sample csv:
VoyageID,arrivalDateTxt,year,placeName,groupPic
1,14 January 1906,1906,Place 1,ANMS1113[006].jpg
1,1 May 1907,1906,Place 2,ANMS1113[006].jpg
1,26 October 1907,1906,Place 3,ANMS1113[006].jpg
1,4 November 1907,1906,Place 4,ANMS1113[006].jpg
1,26 November 1907,1906,Place 5,ANMS1113[006].jpg
1,3 December 1907,1906,Place 6,ANMS1113[006].jpg
1,10 December 1907,1906,Place 7,ANMS1113[006].jpg
1,20 December 1907,1906,Place 8,ANMS1113[006].jpg
1,26 December 1907,1906,Place 9,ANMS1113[006].jpg
3,12 March 1845,1845,Island 1,00038301_4.jpg
3,15 March 1845,1845,Island 2,00038301_4.jpg
3,22 March 1845,1845,Place in ocean 3,00038301_4.jpg
3,23 July 1845,1845,Place in ocean 4,00038301_4.jpg
3,19 December 1845,1845,Place in ocean 5,00038301_4.jpg
3,22 January 1846,1845,Place in ocean 6,00038301_4.jpg
3,30 January 1846,1845,Back home,00038301_4.jpg
I am very much hoping someone can help me and help me to improve my code in a more D3 way.
Thanks in advance, Sally