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; });
根據dataset里面的數據設置svg circle的坐標以及半徑
<!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">
// SVG尺寸
var w = 500;
var h = 50;
// 數據
var dataset = [ 5, 10, 15, 20, 25 ];
// 創建SVG容器
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>
