【D3.V3.js数据可视化系列教程】--(十一)散点图

1、一般的散点绘制

用二维数组表示每个点的坐标

//Width and height
 var w = 500;
 var h = 100;
  
 var dataset = [
 [5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
 [410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("circle")
 .data(dataset)
 .enter()
 .append("circle")
 .attr("cx", function(d) {
 return d[0];
 })
 .attr("cy", function(d) {
 return d[1];
 })
 .attr("r", 5);

2、点的大小

控制半径

.attr("r", function(d) {
 return Math.sqrt(h - d[1]);
 });

3、点的文本

注意文本的位置与圆中心的位置

svg.selectAll("text")
 .data(dataset)
 .enter()
 .append("text")
 .text(function(d) {
 return d[0] + "," + d[1];
 })
 .attr("x", function(d) {
 return d[0];
 })
 .attr("y", function(d) {
 return d[1];
 })
 .attr("font-family", "sans-serif")
 .attr("font-size", "11px")
 .attr("fill", "red");

4、源码

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.         <meta charset="utf-8">  
  5.         <title>testD3-9-drawScatterLot.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 = 100;  
  15.               
  16.             var dataset = [  
  17.                             [5, 20], [480, 90], [250, 50], [100, 33], [330, 95],  
  18.                             [410, 12], [475, 44], [25, 67], [85, 21], [220, 88]  
  19.                           ];  
  20.       
  21.             //Create SVG element  
  22.             var svg = d3.select("body")  
  23.                         .append("svg")  
  24.                         .attr("width", w)  
  25.                         .attr("height", h);  
  26.   
  27.             svg.selectAll("circle")  
  28.                .data(dataset)  
  29.                .enter()  
  30.                .append("circle")  
  31.                .attr("cx", function(d) {  
  32.                     return d[0];  
  33.                })  
  34.                .attr("cy", function(d) {  
  35.                     return d[1];  
  36.                })  
  37.                .attr("r", function(d) {  
  38.                     return Math.sqrt(h - d[1]);  
  39.                });  
  40.   
  41.             svg.selectAll("text")  
  42.                .data(dataset)  
  43.                .enter()  
  44.                .append("text")  
  45.                .text(function(d) {  
  46.                     return d[0] + "," + d[1];  
  47.                })  
  48.                .attr("x", function(d) {  
  49.                     return d[0];  
  50.                })  
  51.                .attr("y", function(d) {  
  52.                     return d[1];  
  53.                })  
  54.                .attr("font-family", "sans-serif")  
  55.                .attr("font-size", "11px")  
  56.                .attr("fill", "red");  
  57.         </script>  
  58.   
  59.     </body>  
  60. </html>  
<!DOCTYPE html>
<html>
  <head>
		<meta charset="utf-8">
		<title>testD3-9-drawScatterLot.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 = 100;
			
			var dataset = [
							[5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
							[410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
						  ];
	
			//Create SVG element
			var svg = d3.select("body")
						.append("svg")
						.attr("width", w)
						.attr("height", h);

			svg.selectAll("circle")
			   .data(dataset)
			   .enter()
			   .append("circle")
			   .attr("cx", function(d) {
			   		return d[0];
			   })
			   .attr("cy", function(d) {
			   		return d[1];
			   })
			   .attr("r", function(d) {
			   		return Math.sqrt(h - d[1]);
			   });

			svg.selectAll("text")
			   .data(dataset)
			   .enter()
			   .append("text")
			   .text(function(d) {
			   		return d[0] + "," + d[1];
			   })
			   .attr("x", function(d) {
			   		return d[0];
			   })
			   .attr("y", function(d) {
			   		return d[1];
			   })
			   .attr("font-family", "sans-serif")
			   .attr("font-size", "11px")
			   .attr("fill", "red");
		</script>

	</body>
</html>


 

5、效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值