1

sunburst中,如何在生成所有弧之后让代码选择根弧?

例如,在代码中:

var first_arc = ""
.json("../data/flare.json", function(json) {
   var path = vis.data([json]).selectAll("path")
       .data(partition.nodes)
     .enter().append("path")
       .attr("display", function(d) { return d.depth ? null : "none"; })
       .attr("d", arc)
       .attr("t_name", function(d) {return d.name})
       .style("fill-rule", "evenodd")
       .on("click", function(d)...

单击中间弧时,它将作为“d”传递给“函数”。

(它的数据首先在 json 文件中)

更新 1:像这样更改代码……</p>

.style("fill-rule", function(d) {
   if (first_arc == "") first_arc = d; return "evenodd"})

…解决了问题,它返回object

name: "flare"
children: Array[10]
...

但这个解决方案看起来不正确,也不通用。

更新 2:我尝试了几个选择,例如:

first_arc = d3.select("[name='flare']")

它通常返回array

0: null
length: 1
parentNode: HTMLHtmlElement
__proto__: Array[0]

或“未定义”

更新 3

first_arc = d3.select("[t_name='flare']")

array带孩子的 1 号退货:

0: SVGPathElement
   __data__: Object

__data__我要的对象在哪里,但我无法选择它。

4

1 回答 1

2

The root node is the one with a "depth" attribute set to 0. So you can say:

d3.selectAll("path").filter(function(d) { return d.depth === 0; })

Your attempts above weren't working because D3 uses CSS3 to select elements. So you can only use d3.select and d3.selectAll with CSS3 selectors i.e. you can't access the data bound to each element this way. The way to filter on the bound data is to use selection.filter.

D3 selections are literally an array of elements, see the "Operating on Selections" section.

Lastly, you can get the bound __data__ property for an element using selection.datum().

于 2012-07-31T16:03:27.583 回答