我正在使用这个很棒的教程来学习如何将AngularJS与D3.js库一起使用:http : //briantford.com/blog/angular-d3。本教程按提供的方式工作(感谢Brian!)
但是我正在尝试学习/理解这段代码,以便可以修改它并画出我想要的东西。我只是在包含以下内容的行之前添加了以下代码块var bars = layers.selectAll("g.bar")
:
console.log('Setup');
var grid = layers.selectAll("g.grid")
.data(function(d) { return d; })
.enter().append("g")
.attr("class", "grid")
.attr("transform", function(d) {
console.log("1");
return "translate(" + x(d) + ",0)";
});
console.log('About to draw a line');
grid.append("line") // attach a line
.style("stroke", "green") // colour the line
.attr("x1", 100) // x position of the first end of the line
.attr("y1", 50) // y position of the first end of the line
.attr("x2", 300) // x position of the second end of the line
.attr("y2", 150);
我希望这会画一条单条对角绿线。而是绘制700条以上的对角绿线(请参见下面的屏幕截图)。为什么?我没有看到任何for
或while
会导致这个循环。那么为什么会这样呢?我在上面插入了console.log行。它打印About to draw line
一次。但它印了1
702次。为什么?这是the子
这行:
grid.append("line")
在网格上附加一条线,这意味着您正在使用来自网格的相同数据。正如您的网格一样:
var grid = layers.selectAll("g.grid")
.data(function(d) { return d; })
它使用来自图层的相同数据:
var layers = vis.selectAll("g.layer")
.data(data)
因此,该数据的长度为18,但是每个数据都有39个元素的数组,18x39=702
即您拥有的行数。
基本上,您希望将线附加到vis
而不是网格,否则,您将使用相同的数据。
vis.append("line") // attach a line
.style("stroke", "green") // colour the line
.attr("x1", 100) // x position of the first end of the line
.attr("y1", 50) // y position of the first end of the line
.attr("x2", 300) // x position of the second end of the line
.attr("y2", 150);
更新的Plnkr:https ://plnkr.co/edit/MTQ9pNWL2784jPfRWH5F ? p = preview
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句