简体   繁体   English

D3中的嵌套SVG选择

[英]Nested SVG selections in D3

I have a list of n associative arrays. 我有一个n个关联数组的列表。

[{'path': 'somepath', 'relevant': [7, 8, 9]}, {'path': 'anotherpath', 'relevant': [9], ...}

Within a large SVG, I want to: a) create rects ("buckets") whose dimensions are proportional to the lengths of their 'relevant' sublists, and b) create rects ("pieces"), for each of the elements in the sublists, placed "inside" their respective buckets. 在一个大的SVG中,我想:a)创建其尺寸与其'relevant'子列表的长度成比例的rects(“buckets”),以及b)为每个元素创建rects(“pieces”)。子列表,放置在各自的桶内“。

After reading Mike Bostock's response to a similar question , I'm sure that I need to use group ("g") elements to group the pieces together. 在阅读了Mike Bostock对类似问题的回答后 ,我确信我需要使用组(“g”)元素将这些部分组合在一起。 I can get the code below to produce the DOM tree that I want, but I'm stumped on how to code the y values of the pieces. 我可以得到下面的代码来生成我想要的DOM树,但是我对如何编写片段的y值感到困惑。 At the point where I need the value, D3 is iterating over the subarrays. 在我需要该值的点上,D3正在遍历子阵列。 How can I get the index of the current subarray that I'm iterating over from inside it when i no longer points to the index within the larger array? i不再指向较大数组中的索引时,如何获取当前从其内部迭代的子数组的索引?

var piece_bukcets = svg.selectAll("g.piece_bucket")
                      .data(files)
                      .enter()
                      .append("g")
                      .attr("class", "piece_bucket")
                      .attr("id", function (d, i) { return ("piece_bucket" + i) })
                      .append("rect")
                      .attr("y", function (d, i) { return (i * 60) + 60; })
                      .attr("class", "bar")
                      .attr("x", 50)
                      .attr("width", function (d) { 
                        return 10 * d["relevant"].length;
                      })
                      .attr("height", 20)
                      .attr("fill", "red")
                      .attr("opacity", 0.2)

        var pieces = svg.selectAll("g.piece_bucket")
                        .selectAll("rect.piece")
                        .data( function (d) { return d["relevant"]; })
                        .enter()
                        .append("rect")
                        .attr("class", "piece")
                        .attr("id", function (d) { return ("piece" + d) })
                        .attr("y", ????)  // <<-- How do I get the y value of d's parent?
                        .attr("x", function (d, i) { return i * 10; })
                        .attr("height", 10)
                        .attr("width", 10)
                        .attr("fill", "black");

Is there a method on d available to find the index of the node it's currently inside? 是否有一种方法d可以找到它目前内部节点的指数? In this case, is there a method I can call on a "piece" to find the index of its parent "bucket"? 在这种情况下,是否有一种方法可以调用“片段”来查找其父“桶”的索引?

You can use the secret third argument to the function: 你可以使用函数的秘密第三个参数:

.attr("y", function(d, i, j) {
  // j is the i of the parent
  return (j * 60) + 60;
})

There's a simpler way however. 然而,有一种更简单的方法。 You can simply translate the g element and everything you add to it will fall into place. 您可以简单地翻译g元素,添加到它的所有内容都将落实到位。

var piece_buckets = svg.selectAll("g.piece_bucket")
                  .data(files)
                  .enter()
                  .append("g")
                  .attr("class", "piece_bucket")
                  .attr("transform", function(d, i) {
                    return "translate(0," + ((i*60) + 60) + ")";
                  })
                  .attr("id", function (d, i) { return ("piece_bucket" + i) });
piece_buckets.append("rect")
                  .attr("class", "bar")
                  .attr("x", 50)
                  .attr("width", function (d) { 
                    return 10 * d["relevant"].length;
                  })
                  .attr("height", 20)
                  .attr("fill", "red")
                  .attr("opacity", 0.2);

var pieces = piece_buckets.selectAll("rect.piece")
                    .data(function (d) { return d["relevant"]; })
                    .enter()
                    .append("rect")
                    .attr("class", "piece")
                    .attr("id", function (d) { return ("piece" + d); })
                    .attr("x", function (d, i) { return i * 10; })
                    .attr("height", 10)
                    .attr("width", 10)
                    .attr("fill", "black");

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

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