D3 map with flag filtering

I made a symbolic map d3 and would like the user to be able to filter the points with an attribute called "type". There are three types: a, b, c, each of which has a html flag associated with it, which when checking should display points of type a, and when not checking, delete these points. I was wondering what is the best way to pass the check / uncheck event to d3? I think if there was a way to pass validated types to select.filter (), that would be a better way. 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 installed to capture all points:

.filter(function(d) { return d.properties.type != null ? this : null; })

I would like the user to be able to change this.

Greetings

+5
source share
1 answer

- . , , .

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);
});
+5

All Articles