简体   繁体   English

无法使用d3选择svg元素

[英]Can't select svg element with d3

I am working on a visualization tool that uses a svg image of the brain. 我正在研究使用大脑svg图像的可视化工具。 Now this svg has paths that are filled with a color. 现在,此svg的路径充满了颜色。 I want to loop over all these paths to set the fill color to white, but for some reason I cannot get the element. 我想遍历所有这些路径以将填充颜色设置为白色,但是由于某些原因,我无法获得该元素。

The project can be seen here . 该项目可以在这里看到。 The svg is inside a div and I even assigned an identifier brain to the div. svg位于div ,我什至为div分配了一个标识符brain The svg itself has an id svg2 . svg本身的ID为svg2 So far I've tried the following: 到目前为止,我已经尝试了以下方法:

function clearBrainColors() {
    var brain = d3.select("#svg2");
    console.log(brain);
    var paths = brain.selectAll("path");
    console.log(paths.length);
    brain.selectAll('path').each(function(d,i) { console.log(this); });
}

But it outputs null in the array[0] component of the selection and 0 with paths.length. 但是它在选择的array [0]组件中输出null,并在path.length中输出0。

I've also tried to use lines such as 我也尝试过使用诸如

var brain = d3.select("#brain svg"); and var brain = d3.select("#brain svg#svg2"); var brain = d3.select("#brain svg#svg2"); but those do not work either. 但是那些也不起作用。

So, how can I select the brain svg using d3? 那么,如何使用d3选择大脑svg?

Decided to put the svg inline as it apparently speeds things up . 决定将svg内联,因为它显然可以加快处理速度

The code I used to fill the svg is now: 我用来填充svg的代码现在是:

$("#svg2").find("path").each(function(){
       $(this).css({ fill: "#ff0000" });
    });

You can try setTimeOut() , following example 您可以尝试以下示例的setTimeOut():

setTimeOut(function() {

    var brain = d3.select("#svg2");
    console.log(brain);

}, 1000);

this could be the svg is generate on the spot, d3 unable get at that moment. 这可能是当场生成的svg,当时d3无法获取。

Hope this help :) 希望这个帮助:)

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

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