【问题标题】:Selecting multiple path elements in a d3 map在 d3 地图中选择多个路径元素
【发布时间】:2014-01-09 21:28:05
【问题描述】:

我有一张世界地图,其中有不同的国家作为路径元素。每个国家都属于一个集群,该集群是一个包含国家名称的对象。这些名称对应于各个要素的 geojson 属性中的名称。当点击一个国家时,我想改变该国家所属的整个集群的样式。

我可以突出显示单击的节点,因为它是这样传入事件处理程序的:

    svg.selectAll("path").data(json.features).enter().append("path")
    .attr("d", path)
    .style("fill", function(d) {
        var value = d.properties.value;
        var size = d.properties.size;
        if (value) {
            var hsv = hsvToRgb((value / moreThanOne) *0.66, 1, 0.1 + (size / maxSize) * 0.9);
            if (size == 1)  return "#000000";
            else return "rgba(" + Math.floor(hsv[0]) + "," + Math.floor(hsv[1]) + "," + Math.floor(hsv[2]) + ",255)";
        } else return "#999999";
    }).on("click", function(d) {
        var name = d.properties.name;
        var value = d.properties.value;
        hilite(value, this);
    })

这里是高亮功能:

function hilite(cluster, node) { //highlights the single clicked node
d3.select(node).style("stroke", "#ff0000");
//clustermap is the object holding the clusters data
Object.keys(clusterMap[cluster]).forEach(function(key) {
    svg.selectAll("svg").data().each(function(d) {
        if (d.properties.name.toLowerCase() == key.replace(".gif", "").toLowerCase()) {
            //if it matches, style ... but how?
            console.log(d.properties.name.toLowerCase() + " " + key.replace(".gif", "").toLowerCase());
        }
    });
});

}

如何按名称选择所有节点并对其应用样式?我是 d3 的新手,所以我想一定有一个简单的方法可以做到这一点,但我想不通。

谢谢!

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    您可以使用.filter() 来执行此操作,假设要比较clusterd.properties.name

    d3.selectAll("path").filter(function(d) {
      return d.properties.name == cluster;
    }).style("stroke", "#f00");
    

    【讨论】:

    • 谢谢,我会尝试,但我必须修改它,因为“cluster”只是 clusterMap 中的一个 id,然后包含具有国家名称的对象,例如:1:坦桑尼亚:坦桑尼亚澳大利亚:澳大利亚 2:国家/地区名称:国家/地区名称等,因此该功能必须返回多项选择...这是我的问题 :)
    • 那么您从哪里获得所选国家/地区的集群名称?
    • yes ...当我第一次解析geojson时,我用集群名称(数字)附加一个属性(“值”),然后我用“var value = d. properties.value”在调用 hilite 函数之前。我根据您的建议修改了代码,添加了一个循环,该循环通过已传递集群中的国家/地区,但它在样式方面没有做任何事情。也许在网上看到代码? :virostatiq.com/data/flags
    • 所以.value 包含集群编号并且对于同一集群中的国家/地区是否相同?然后您应该可以将其与cluster 进行比较以进行过滤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2014-08-03
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多