d3映射与复选框过滤

rys*_*oan 5 html javascript d3.js

我有一个d3符号映射,并希望用户能够通过名为'type'的属性过滤点.有三种类型:a,b,c,每个都有一个关联的html复选框,检查时应显示a类型的点,未检查时应删除这些点.我想知道将检查/取消检查事件传递给d3的最佳方法是什么?我在想是否有办法将检查的类型传递给select.filter(),这将是最好的方法.这是代码:

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>
Run Code Online (Sandbox Code Playgroud)

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); });;
Run Code Online (Sandbox Code Playgroud)

目前,过滤器设置为捕获所有点:

.filter(function(d) { return d.properties.type != null ? this : null; })
Run Code Online (Sandbox Code Playgroud)

我希望用户能够改变这一点.

干杯

And*_*ard 5

这样的事情应该有效.为您的复选框添加值属性,以便您知道它们所指的是什么.

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);
});
Run Code Online (Sandbox Code Playgroud)