我曾认为d3.js的append
函数返回已附加到选择器的对象,但我发现以下两个代码块给出不同的结果:
var svg = d3.select("body")
.append("svg")
.attr("width", fig_width)
.attr("height", fig_height);
svg.append("g")
.attr("class", "graph")
.attr("transform",
"translate(" + graph_margin.left + "," + graph_margin.top + ")");
这似乎无法将图形组进行翻译,因为它偏移了左边和上边的边距:
var svg = d3.select("body")
.append("svg")
.attr("width", fig_width)
.attr("height", fig_height)
.append("g")
.attr("class", "graph")
.attr("transform",
"translate(" + graph_margin.left + "," + graph_margin.top + ")");
这在SVG / d3.js中是如何工作的,我有什么不理解的地方?
<svg width="100" height="100"><g class="graph" transform="translate(30,30)"></g></svg>
和<svg width="100" height="100"><g class="graph" transform="translate(30,30)"></g></svg>
。 - Marksvg
来添加额外的元素 - 请记住,在第一种情况下,svg
包含 SVG 元素,在第二种情况下包含已转换的g
元素。因此,在第一种情况下,你附加到svg
的任何内容都不会被转换(因为它不包含在g
元素中)。 - Lars Kotthoffsvg
是一个不好的变量名选择。如果有帮助的话,也许你可以把你的评论转化成一个答案,这样我就可以接受了? - xnx