【发布时间】:2016-06-17 12:30:48
【问题描述】:
我正在使用这个很棒的教程来学习如何将 D3.js 库与 AngularJS 一起使用: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 次。为什么?这是plunker
【问题讨论】:
-
在线
181你在网格上附加一条线,这是一个巨大的东西数组,因此它在每个网格元素上附加了线。你应该只在你想要的元素:) -
我不知道你想把它附加到哪里,但是如果你把它附加到
layersvar 上,你会得到一行 plnkr.co/edit/IYRuHptmpf08GTa8t03s?p=preview
标签: javascript angularjs d3.js