简体   繁体   English

d3在selectAll上忽略SVG

[英]d3 ignore SVG on selectAll

Still learning d3.js. 仍在学习d3.js。

I would like to ignore the selection of an SVG panel when using .selectAll("svg"). 使用.selectAll(“ svg”)时,我想忽略SVG面板的选择。

I am building a visualization comprising four SVG panels. 我正在构建一个包含四个SVG面板的可视化文件。 The top SVG panel is used to display header/title information for the visualization. 顶部SVG面板用于显示可视化文件的标题/标题信息。

var svgHeader = d3.select("body")
    .append("svg")  
    .attr("width", width + margin.left + margin.right)
    .attr("height", 100)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .append("g");

The next two SVG panels are dynamically created using a range of two numbers representing two years. 接下来的两个SVG面板使用代表两年的两个数字范围动态创建。

var svg = d3.select("body")
    .selectAll("svg")   
    .data(d3.range(2012, 2013))
    .enter().append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", 200)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .append("g");

The final SVG contains detail information as the user interacts with the visualization. 当用户与可视化交互时,最终的SVG包含详细信息。

Problem: I want to exclude the first SVG panel from the .selectAll("svg") which is used to create the two middle panels. 问题:我想从.selectAll(“ svg”)中排除第一个SVG面板,该面板用于创建两个中间面板。 I would like to dynamically build SVG panels and have them locate underneath the previously created header SVG. 我想动态地构建SVG面板,并使它们位于先前创建的SVG标头下方。

Is there any way to exclude the header SVG when dynamically creating the middle panels? 动态创建中间面板时,有什么方法可以排除标题SVG?

I think the best way you should be going about this is taking advantage of classes and adding an appropriate class to the different svgs and then selecting based on the class rather than the svg. 我认为最好的方法是利用类,然后将适当的类添加到不同的svg中,然后根据该类而不是svg进行选择。 This way you know what each of the svgs represent and you can easily reference them.' 这样,您就知道每个svg代表什么,并且可以轻松地引用它们。

var svgHeader = d3.select("body")
    .append("svg")
    .attr("class", "svgHeader")
    .attr("width", width + margin.left + margin.right)
    .attr("height", 100)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .append("g");

And then the other two you add a different class name 然后另外两个添加不同的类名

var svg = d3.select("body")
    .selectAll("svg")   
    .data(d3.range(2012, 2013))
    .enter().append("svg")
    .attr("class", "data") 
    .attr("width", width + margin.left + margin.right)
    .attr("height", 200)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .append("g");

Now you can do d3.selectAll("svg.data") and select only svg elements with the class data 现在,您可以执行d3.selectAll("svg.data")并仅选择具有类data svg元素

Alternatively, you can embed your svg elements in different divs. 另外,您可以将svg元素嵌入不同的div中。 Assuming you have a div whose id is 'center-div' the following snippet returns you only the svgs contained in it. 假设您有一个id为“ center-div”的div,则以下代码段仅返回其中包含的svg。

d3.selectAll("#center-div svg")

Please also consider that you can append whatever DOM element via d3, so divs can be dinamically generated. 还请注意,您可以通过d3附加任何DOM元素,因此可以动态生成div。

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

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