【问题标题】:D3 Selecting based on text value of nodeD3 根据节点的文本值进行选择
【发布时间】:2017-02-28 04:41:50
【问题描述】:

假设我有一个 svg:

<svg>
  <text font-size="24" id="svg_1" y="63" x="69.5" stroke-width="0" stroke="#000" fill="#000000">1</text>
  <text font-size="24" id="svg_2" y="194" x="73.5" stroke-width="0" stroke="#000" fill="#000000">2</text>
  <text font-size="24" id="svg_3" y="313" x="60.5" stroke-width="0" stroke="#000" fill="#000000">3</text>
 </g>
</svg>

我可以使用svg.selectAll()svg.filter() 的哪个参数来仅选择值为“2”的文本节点并使用.attr() 更改其颜色?

我发现了很多关于按属性而不是按文本值进行选择的文献。

【问题讨论】:

  • document.getElementById('svg_2').textContent 使用此代码。

标签: javascript d3.js svg


【解决方案1】:

text() 不带参数是一个getter

因此,在filter 函数内部,这段代码:

d3.select(this).text() == 2

对于任何以“2”为值的&lt;text&gt; 元素,将被评估为true

这是一个演示:

d3.selectAll("text")
  .filter(function(){ 
    return d3.select(this).text() == 2
  })
  .attr("fill", "red");
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg>
    <text font-size="24" id="svg_1" y="20" x="20" stroke-width="0" stroke="#000" fill="#000000">1</text>
    <text font-size="24" id="svg_2" y="50" x="20" stroke-width="0" stroke="#000" fill="#000000">2</text>
    <text font-size="24" id="svg_3" y="80" x="20" stroke-width="0" stroke="#000" fill="#000000">3</text>
</svg>

PS:在 D3 中,getter 通常返回一个 字符串。这就是我不使用严格相等运算符 (===) 的原因。检查它:

var value = d3.select("#svg_2").text();
console.log("value is: " + value);
console.log("type is: " + typeof(value));
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg>
    <text font-size="24" id="svg_1" y="20" x="20" stroke-width="0" stroke="#000" fill="#000000">1</text>
    <text font-size="24" id="svg_2" y="50" x="20" stroke-width="0" stroke="#000" fill="#000000">2</text>
    <text font-size="24" id="svg_3" y="80" x="20" stroke-width="0" stroke="#000" fill="#000000">3</text>
</svg>

【讨论】:

  • 你也可以使用+d3.select(this).text() === 2,如果你想让jslint、jshint、eslint等对“==”和“===”闭嘴。
  • @JarrettMeyer 是的,确实如此。我个人从不在生产环境中使用==,因此您可以使用一元加号或将getter 与"2" 进行比较。
【解决方案2】:
$(document).ready(function(){  $('#box').keyup(function(){
    var valThis = $(this).val().toLowerCase();

    var noresult = 0;
    if(valThis === ""){
        $('.navList > text').show();
        noresult = 1;
        $('.no-results-found').remove();
    } else {
        $('.navList > text').each(function(){
            var text = $(this).text().toLowerCase();
            var match = text.indexOf(valThis);
            if (match >= 0) {
                $(this).show();
                noresult = 1;
                $('.no-results-found').remove();
            } else {
                $(this).hide();
            }
        });   };

    if (noresult === 0) {
        $(".navList").append('<text font-size="24" id="" stroke-width="0" stroke="#000" fill="#000000" class="no-results-found">No results found.</text>');
    } }); });




Add a text box where you can search

<input placeholder="Search Me" id="box" type="text" />



<svg class="navList">
  <text font-size="24" id="svg_1" y="20" x="0" stroke-width="0" stroke="#000" fill="#000000">1</text>
  <text font-size="24" id="svg_2" y="40" x="0" stroke-width="0" stroke="#000" fill="#000000">2</text>
  <text font-size="24" id="svg_3" y="60" x="0" stroke-width="0" stroke="#000" fill="#000000">3</text>

</svg>

【讨论】:

  • 我认为您在 svg 标签中要求过滤器
  • 只要加上上面的脚本和文本框就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
相关资源
最近更新 更多