【发布时间】:2015-05-06 06:50:31
【问题描述】:
我正在尝试从不同形状的数组中绘制 SVG 元素。我可以看到成功添加到文档中的所有形状,但我看不到它们在屏幕上呈现。有趣的是,如果我只是尝试从浏览器的 DOM 资源管理器(在我的情况下为 Chrome)中编辑 HTML 元素(不做任何更改),形状就会按预期绘制。有什么建议在这里做错了吗?
http://fiddle.jshell.net/1qd2kt9s/
谢谢
var my_data=[{"shape":"circle","attr":{"r":"50","cx":"60","cy":"60", "fill":"red"} }
,{"shape":"rect","attr":{"width":"100","height":"100","x":"120","y":"10", "fill":"blue"} }
];
var svg=d3.select('body')
.append('svg')
.attr("width",500)
.attr("height",500);
var shapes=svg.selectAll('.shapes')
.data(my_data);
shapes.enter()
.append(function(d,i) {
return document.createElement(d.shape);
})
.each(function(d,i){
d3.select(this).attr(d.attr);
});
Here is what I can see in the browser DOM elements list:
<svg width="500" height="500">
<circle r="50" cx="60" cy="60" fill="red"></circle>
<rect width="100" height="100" x="120" y="10" fill="blue"></rect>
</svg>
【问题讨论】:
标签: d3.js