简体   繁体   English

如何从onClick更改多个SVG节点的颜色

[英]How to change color of multiple SVG nodes from onClick

I'm building an application that links different nodes together based on their characteristics. 我正在构建一个基于不同节点的特性将其链接在一起的应用程序。 The data structure [from Neo4j] and front end setup [D3] is like this: 数据结构[来自Neo4j]和前端设置[D3]如下所示:

graph.nodes has 4 properties: graph.nodes具有4个属性:

1) id (String) 1)id(字符串)

2) name (String) 2)名称(字符串)

3) type (String) 3)类型(字符串)

4) properties (Object) 4)属性(对象)

Where properties has several more attributes, but for this problem, we are only considering 'properties.represents' 在属性具有更多属性的情况下,但是对于这个问题,我们仅考虑“ properties.represents”

So when I click on a node I would like all other nodes with the same 'properties.represents' to change to the same color (in this case blue). 因此,当我单击一个节点时,我希望所有其他具有相同“ properties.represents”的节点更改为相同的颜色(在本例中为蓝色)。

The specific code I'm trying to use to make this work is: 我正在尝试使用的特定代码是:

 getRep = n.properties.represents;

 graph.nodes.forEach(function(d) {
                    if (d.properties.represents == getRep) {
                        d3.select(d).style('fill', 'blue');
                    }
                });

However, this is not correct. 但是,这是不正确的。 The console outputs this error message: 控制台输出此错误消息:

Uncaught TypeError: Cannot read property 'setProperty' of undefined 未捕获的TypeError:无法读取未定义的属性'setProperty'

Any advice on why this is not working? 有什么建议为什么不起作用? Thanks for your time, its greatly appreciated. 感谢您的宝贵时间,不胜感激。

    var link = svg.selectAll(".link")
            .data(graph.links).enter()
            .append("line").attr("class", "link");

    var node = svg.selectAll(".node")
            .data(graph.nodes).enter()
            .append("circle")
            .attr("class", function (d) { console.log("Represents: " + d.properties.represents); return "node "+ d.type.toString() })
            .attr("r", radius)
            .style("fill", function(d) {return d.colr; })
            .call(force.drag);

    // html title attribute
    node.append("title")
            .text(function (d) { return d.name; })

    var state = false;
    var last = null;
    var current = null;
    node.on("click", function(n)
            {

                var metainf = "";
                currRepresents = n.properties.represents;
                metainf = metainf.concat("Title: ", n.name, "<br/>Label: ", n.type);
                console.log(metainf);
                d3.select("#metainfo")
                    .html(metainf);

                last = current;
                current = d3.select(this);
                current.style('fill', 'red');
                last.style('fill', function(d) { return d.colr; });

                getRep = n.properties.represents;

                graph.nodes.forEach(function(d) {
                    if (d.properties.represents == getRep) {
                        d3.select(d).style('fill', 'blue');
                    }
                });
            });

You can't select elements by their data. 您无法通过元素数据选择元素。 However, you can filter a selection: 但是,您可以过滤选择:

svg.selectAll(".node")
   .filter(function(d) { return d.properties.represents == getRep; })
   .style('fill', 'blue');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM