【发布时间】:2014-05-05 04:35:47
【问题描述】:
目前我正在学习一些“D3.js”并试图了解数据的处理和选择方式。
我坚持执行我为自己创建的以下任务。
理想情况下,我想要的东西在功能上等同于:
<svg>
<circle r="20.5" cx="100" cy="200"></circle>
<circle r="20.5" cx="300" cy="10"></circle>
</svg>
我目前拥有的(按照我的逻辑)是:
var matrix = [ [{ "x": 100, "y": 200 }], [{ "x": 300, "y": 10 }]];
var result = d3.select("body").append("svg") // Append SVG to end of Body
.data(matrix) // select this data
.selectAll("g") //g is a svg grouping tag
.data(function (d) { return d; }) //Unwrap the first part of the array
.enter() // Grab all the data that is new to the selection in each array
.selectAll("g")
.data(function (d) { return d;}) // foreach each item inside the 1D array
.enter() // For all the data that doesn't exist already in the SVG
.append("circle") // Append Circle to the DOM with the following attributes
.attr("r", 20.5)
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
};
奇怪的是:
var result = d3.select("body").append("svg")
.data(matrix)
.selectAll("g")
.enter()
.append("circle")
.attr("r", 20.5)
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
};
似乎能够以某种方式获取数组中的第一项,但无法正确迭代。我不太确定它是如何进入数组的。
D3 似乎与我习惯的编程范例相去甚远,而且更难调试,所以如果有人能解释我哪里出错了,那就太棒了。
哦,虽然这个例子毫无用处,我可以使用合并命令将它展平——为了完全理解 D3 操作。我想在没有合并的情况下画几个圆圈:)
谢谢!
【问题讨论】:
标签: javascript arrays multidimensional-array d3.js