d3.js - 使用 .selectAll() 根据属性值选择节点元素

Lou*_*_Ds 3 javascript filter node.js d3.js

我有一个节点,如:

<div>
  <svg ...>
    <g class="graph">
      <path class="myarea" d="...AAA..." pos="p1">  </path>
    </g>
    <g class="graph">
      <path class="myarea" d="...BBB..." pos="p2">  </path>
    </g>
    <g class="graph">
      <path class="myarea" d="...CCC..." pos="p3">  </path>
    </g>
  /svg>
</div>
Run Code Online (Sandbox Code Playgroud)

我正在尝试选择具有属性的特定节点(例如 where d="...BBB..."pos='p2'

d3.selectAll(".myarea").each(function(d,i) {
   console.log("-> ",  d3.select(this).attr("pos"), d3.select(this).attr("pos") == 'p2' );
  
});
Run Code Online (Sandbox Code Playgroud)

这将产生/返回一个包含所有 3 个条目的数组:

-> p0 假

-> p1 真

-> p2 假

-> [数组(3)]

我有兴趣只选择一个,在这种情况下选择一个,.attr("pos") == 'p2'所以我只想返回一个元素。

我试着map()filter(),但没有成功。

根据属性值选择特定元素的正确方法是什么?

And*_*eid 5

选择.filter()

可以根据类选择一堆元素,然后使用以下内容过滤选择selection.filter()

svg.selectAll("whatever")
  .filter(function() {
    return d3.select(this).attr("col") == 2; // filter by single attribute
  })
Run Code Online (Sandbox Code Playgroud)

svg.selectAll("whatever")
  .filter(function() {
    return d3.select(this).attr("col") == 2; // filter by single attribute
  })
Run Code Online (Sandbox Code Playgroud)
var svg = d3.select("svg");

svg.selectAll("rect")
  .filter(function() {
    return d3.select(this).attr("col") == 2
  })
  .attr("fill","orange");
Run Code Online (Sandbox Code Playgroud)

使用 CSS 选择器按属性值进行选择

但是,与其选择一堆元素然后过滤选择,我们还可以使用此处描述的 CSS 选择器一步完成所有操作。这让我们可以选择元素的属性等于特定值:

svg.selectAll("rect[col='2']")
Run Code Online (Sandbox Code Playgroud)

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg>
  <rect width="20" height="20" x="5" y="5" col="1" row="1"></rect>
  <rect width="20" height="20" x="30" y="5" col="2" row="1" ></rect>
  <rect width="20" height="20" x="5" y="30" col="1" row="2" ></rect>
  <rect width="20" height="20" x="30" y="30" col="2" row="2" ></rect>
</svg>
Run Code Online (Sandbox Code Playgroud)
svg.selectAll("rect[col='2']")
Run Code Online (Sandbox Code Playgroud)

在上面我也使用:

d3.selectAll("rect[col='1'][row='1']") to select based on two attribute values.
Run Code Online (Sandbox Code Playgroud)

您还可以替换查找类而不是通用标签,因此对您来说这可能如下所示:

d3.selectAll(".myarea[d='BBB'][pos='p2']")
Run Code Online (Sandbox Code Playgroud)