【D3.V3.js数据可视化系列教程】--(八)绘制SVG

【D3.V3.js系列教程】--(八)绘制SVG

1、创建SVG

  1. //Width and height  
  2. var w = 500;  
  3. var h = 50;  
  4. var svg = d3.select("body")  
  5.             .append("svg")  
  6.             .attr("width", w)   // <-- Here  
  7.             .attr("height", h); // <-- and here!  
//Width and height
var w = 500;
var h = 50;
var svg = d3.select("body")
            .append("svg")
            .attr("width", w)   // <-- Here
            .attr("height", h); // <-- and here!

2、数据驱动的SVG

var dataset = [ 5, 10, 15, 20, 25 ];
  1. var dataset = [ 5, 10, 15, 20, 25 ];  
var dataset = [ 5, 10, 15, 20, 25 ];
  1. var circles = svg.selectAll("circle")  
  2.                  .data(dataset)  
  3.                  .enter()  
  4.                  .append("circle");  
  5. circles.attr("cx", function(d, i) {  
  6.             return (i * 50) + 25;  
  7.         })  
  8.        .attr("cy", h/2)  
  9.        .attr("r", function(d) {  
  10.             return d;  
  11.        });  
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、源码

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.         <meta charset="utf-8">  
  5.         <title>testD3-7-drawSVG.html</title>  
  6.         <script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>  
  7.     <style type="text/css">  
  8.         </style>  
  9.     </head>  
  10.     <body>  
  11.         <script type="text/javascript">  
  12.         //Width and height  
  13.             var w = 500;  
  14.             var h = 50;  
  15.               
  16.             //Data  
  17.             var dataset = [ 5, 10, 15, 20, 25 ];  
  18.               
  19.             //Create SVG element  
  20.             var svg = d3.select("body")  
  21.                         .append("svg")  
  22.                         .attr("width", 500)  
  23.                         .attr("height", 50);  
  24.   
  25.             var circles = svg.selectAll("circle")  
  26.                 .data(dataset)  
  27.                 .enter()  
  28.                 .append("circle");  
  29.   
  30.             circles.attr("cx", function(d, i) {  
  31.                         return (i * 50) + 25;  
  32.                     })  
  33.                    .attr("cy", h/2)  
  34.                    .attr("r", function(d) {  
  35.                         return d;  
  36.                    });  
  37.         </script>  
  38.   
  39.     </body>  
  40. </html>  
<!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.v3.js"></script>
	<style type="text/css">
		</style>
	</head>
	<body>
		<script type="text/javascript">
		//Width and height
			var w = 500;
			var h = 50;
			
			//Data
			var dataset = [ 5, 10, 15, 20, 25 ];
			
			//Create SVG element
			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、效果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值