1 创建SVG
var w = 500;
var h = 50;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
2 数据驱动的SVG
var dataset = [ 5, 10, 15, 20, 25 ];
var circles = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle");
circles.attr("cx", function(d, i) {
return (i * 50) + 25;
})
.attr("cy", h/2)
.attr("r", function(d) {
return d;
});
3 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>testD3-7-drawSVG.html</title>
<script type="text/javascript" src="http://localhost:8080/spring/js/d3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var w = 500;
var h = 50;
var dataset = [ 5, 10, 15, 20, 25 ];
var svg = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 50);
var circles = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle");
circles.attr("cx", function(d, i) {
return (i * 50) + 25;
})
.attr("cy", h/2)
.attr("r", function(d) {
return d;
});
</script>
</body>
</html>
4 效果
![使用D3绘制SVG](https://img-blog.csdn.net/20130908094109796?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmdfX3RpYW54dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)