D3.JS如何计算鼠标悬停时某个属性的圆圈数

时间:2016-01-26 20:38:00

标签: javascript jquery d3.js

假设我在D3中创建了几个圆圈并且都使用了鼠标悬停功能......

.on("mouseover", function(d) {
    //I get the co attribute of the one being hovered
    var theCompany = d3.select(this).attr("co"); 
    //and then I style all of the circles with that same co attribute       
    d3.selectAll("circle.circle[co='"+theCompany+"']").attr("stroke", "#999"); 
}

在这个鼠标悬停功能中,有没有办法计算我正在悬停的“co”属性的圈数?

1 个答案:

答案 0 :(得分:3)

您可以使用d3.js内置方法size来获取所选项目的计数。

var count = d3.selectAll("circle.circle[co='"+theCompany+"']").size()

此外(这可能会对前一个功能派上用场)有一种方法可以检查选择是否为空。

d3.select('<selector'>).empty()根据您的选择是否有任何结果,这将返回true/false

相关问题