2

我很难使用 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);
});

但它不起作用..请帮帮我...

4

2 回答 2

2

看到这里有一个非常相似的问题和答案:d3 map with checkbox filtering

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

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

例如,请参见此处:http ://www.ryansloan.org/d3samples/scatterplot.html

然后,假设您的数据有一些您试图过滤的属性“名称”,请执行以下操作:

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);
});
于 2013-06-11T06:53:53.100 回答
0

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

.filter(function(d) {
    return d.series == "name"
})
于 2013-06-11T06:54:31.117 回答