I have a problem with my first attempt at an Exit event. The code below draws one of three circles based on three different JSON files. The default is the medium sized circle. The drop-down menu allows you to select either a Small, Medium, or Large circle to be drawn next.
My problem is that I have not written the Exit mode correctly. If I comment out the exit() then each selection appears on the screen without deleting any of the previous elements, as you would expect. If the exit mode is not commented out then only the default (medium) circle is displayed.
This is likely very obvious to someone with D3JS experience. Please help me to learn where I am going wrong. Thanks so much!
- Tim
<body>
<select id = "opts">
<option value="circle1">Small</option>
<option value="circle2" selected="selected">Medium</option>
<option value="circle3">Large</option>
</select>
<script>
// data sources for small, med, large circles
var circle1 = "/MenuSelections/circle1.JSON";
var circle2 = "/MenuSelections/circle2.JSON";
var circle3 = "/MenuSelections/circle3.JSON";
function drawCirc(newData){
// Read in the JSON data
console.log(newData); // Confirms data source
d3.json(newData, function(dataset) {
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//ENTER
var circles = svgContainer.selectAll("circle")
.data(dataset.circle)
.enter()
.append("circle");
var circleAttributes = circles
.attr("cx", function (d) {
console.log(d)
return d.x_axis; })
.attr("cy", function (d) { return d.y_axis; })
.attr("r", function (d) { return d.radius; })
.style("fill", function(d) { return d.color; });
//EXIT - NOT WORKING. Original remains, no new graph.
// if this section commented out: new circles draw in without remove of prev.
d3.select("body").selectAll("circle")
.data(dataset.circle)
.exit()
.remove();
});
}
drawCirc(circle2); // Initiate with default selection
// handle on click event for the ID (opts) of the menu item
d3.select('#opts')
.on('change', function() {
var newData = eval(d3.select(this).property('value'));
// console.log(newData);
drawCirc(newData); // Call with new selection.
});