使用复选框过滤数据- D3



我很难过滤使用d3图中的复选框。这里我有五个复选框,我必须根据复选框的值过滤特定的数据。

HTML

<div id="company"
    <ul id='BestBrands'>
    <label class="checkbox">
                                                    <input class="select-all" type="checkbox" id="brandAll"  name="bands" value="M&B"
                                                    onclick="selectMainBrandALL('brandAll');">
                                                    Select All</label>
    <label class="checkbox">
                                                    <input type="checkbox" id="TCS"  name="TCS" 
                                                     >
                                                    TCS</label>
    <label class="checkbox">
                                                    <input id="CTS" type="checkbox" name="CTS" " >
                                                    CTS </label>
     <label class="checkbox">
                                                    <input type="checkbox" id="info"  name="info" >
                                                    Info</label>
    <label 
    </ul>
</div>

这是生成图表的D3散点图代码。我在这里使用人力车框架…

 v
ar nodes = graph.vis.selectAll("path")
                               .data(series.stack.filter( function(d) { return d.y !== null } ))
                               .enter().append("svg:circle")
                               .attr("cx", function(d) { return graph.x(d.x) })
                               .attr("cy", function(d) { return graph.y(d.y) })
                               .attr("fill-opacity", 1.0)
                              // .attr("render-order", 1)
                               .attr("r", function(d) { return ("r" in d) ? d.r : graph.renderer.dotSize});

如果我必须根据数据筛选数据我可以这样做

.filter(function(d) {
    return d.x < 100;
  });

我已经做了这个

   var check  =graph.vis.selectAll(".ck input").on("change", function () {
  console.log(this.name)
  var selected = this.name, 
  display = this.checked ? "inline" : "none";
  graph.vis.selectAll("path")
   series.stack.filter( function(series) { return d.name == selected; } )
    .attr("display", display);
});

但它不起作用…

查看这里非常类似的问题和答案:d3地图与复选框过滤

我认为你应该首先将散点图中的圆圈附加到"点"类。像这样:

svg.selectAll(".dot")
  .data(data)
  .enter().append("circle")...

参见这里的例子:http://www.ryansloan.org/d3samples/scatterplot.html

然后,假设你的数据有一些属性"name",你正在尝试过滤,做一些像:

d3.selectAll(".input class id").on("change", function () {
  var selected = this.name, 
  display = this.checked ? "inline" : "none";
  svg.selectAll(".dot")
    .filter(function(d) { return d.name == selected; })
    .attr("display", display);
});

对于系列的名称可以使用与处理数值相同的方法。例如

.filter(function(d) {
    return d.series == "name"
})

最新更新