【D3.V3.js数据可视化系列教程】--(四)使用数据

【D3.V3.js系列教程】--(四)使用数据

1、说明

用一个d表示当前数据值。

可以使用一个匿名函数处理这个数据。

接上面一个例子可以显示数据值,汉可以根据数据值来修改颜色值。

2、示例

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.         <meta charset="utf-8">  
  5.         <title>testD3-3.html</title>  
  6.         <script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>  
  7.     </head>  
  8.     <body>  
  9.         <script type="text/javascript">  
  10.           
  11.             var dataset = [ 5, 10, 15, 20, 25 ];  
  12.               
  13.             d3.select("body").selectAll("p")  
  14.                 .data(dataset)  
  15.                 .enter()  
  16.                 .append("p")  
  17.                 .text(function(d) {  
  18.                     return "I can count up to " + d;  
  19.                 })  
  20.                 .style("color", function(d) {  
  21.                     if (d > 15) {    //Threshold of 15  
  22.                         return "red";  
  23.                     } else {  
  24.                         return "black";  
  25.                     }  
  26.                 });  
  27.               
  28.         </script>  
  29.     </body>  
  30. </html>  
<!DOCTYPE html>
<html>
  <head>
		<meta charset="utf-8">
		<title>testD3-3.html</title>
		<script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>
	</head>
	<body>
		<script type="text/javascript">
		
			var dataset = [ 5, 10, 15, 20, 25 ];
			
			d3.select("body").selectAll("p")
				.data(dataset)
				.enter()
				.append("p")
				.text(function(d) {
					return "I can count up to " + d;
				})
				.style("color", function(d) {
					if (d > 15) {	//Threshold of 15
						return "red";
					} else {
						return "black";
					}
				});
			
		</script>
	</body>
</html>


 

3、效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值