Reputation: 615
I have a made a d3 symbolic map and would like the user to be able to filter points by an attribute called 'type'. There are three types: a, b, c, each of which has an associated html checkbox, that when checked should display points of type a, and when un-checked should remove those points. I was wondering what the best way to pass the check/uncheck event into d3? I am thinking if there was a way to pass the checked types into a select.filter(), that would be the best way to go. Here is the code:
HTML
<div class="filter_options">
<input class="filter_button" id="a_button" type="checkbox">a</input><br>
<input class="filter_button" id="b_button" type="checkbox">b</input><br>
<input class="filter_button" id="c_button" type="checkbox">c</input><br>
</div>
js
queue()
.defer(d3.json, "basemap.json")
.defer(d3.json, "points.json")
.await(ready);
function ready(error, base, points) {
var button =
svg.append("path")
.attr("class", "polys")
.datum(topojson.object(us, base.objects.polys))
.attr("d", path);
svg.selectAll(".symbol")
.data(points.features)
.enter().append("path")
.filter(function(d) { return d.properties.type != null ? this : null; })
.attr("class", "symbol")
.attr("d", path.pointRadius(function(d) { return radius(d.properties.frequency * 50000); }))
.style("fill", function(d) { return color(d.properties.type); });;
Currently, the filter is set to catch all points:
.filter(function(d) { return d.properties.type != null ? this : null; })
I would like the user to be able to change this.
Cheers
Upvotes: 5
Views: 9580
Reputation: 1684
Something like this should work. Add a value attribute for your checkboxes so you know what they're referring to.
d3.selectAll(".filter_button").on("change", function() {
var type = this.value,
// I *think* "inline" is the default.
display = this.checked ? "inline" : "none";
svg.selectAll(".symbol")
.filter(function(d) { return d.properties.type === type; })
.attr("display", display);
});
Upvotes: 5