【D3.V3.js数据可视化系列教程】--(十)更自由的条形图

1、添加一个矩形

//Width and height
 var w = 500;
 var h = 100;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", 0)
 .attr("y", 0)
 .attr("width", 20)
 .attr("height", 100);

2、添加更多条

控制x坐标的起始位置(i*21)

//Width and height
 var w = 500;
 var h = 100;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", function(d, i) {
 return i * 21; //Bar width of 20 plus 1 for padding
 })
 .attr("y", 0)
 .attr("width", 20)
 .attr("height", 100);

3、均匀的添加条-定条宽

除的方式(i * (w / dataset.length);)作为条和间隙的总宽度,在设置固定宽度小于总宽度,这时就会自然生成一个空白间隙

一句话:条的宽度固定,总宽-条宽=空白宽。空白宽取决于总宽,总宽取决于计算表达式(w / dataset.length)

//Width and height
 var w = 500;
 var h = 100;
 var barPadding = 1;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", function(d, i) {
 return i * (w / dataset.length);
 })
 .attr("y", 0)
 .attr("width", 20)
 .attr("height", 100);

4、均匀的添加条-定间隙宽

一句话:间隙的宽度固定,总宽-空白宽=条宽。条宽取决于总宽(w / dataset.length - barPadding),总宽取决于计算表达式(w / dataset.length)

//Width and height
 var w = 500;
 var h = 100;
 var barPadding = 1;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", function(d, i) {
 return i * (w / dataset.length);
 })
 .attr("y", 0)
 .attr("width", w / dataset.length - barPadding)
 .attr("height", 100);

5、条高

用条长-纵坐标(纵坐标是从上到下计算,即下方向为正):h - (d * 4);

var w = 500;
 var h = 100;
 var barPadding = 1;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", function(d, i) {
 return i * (w / dataset.length);
 })
 .attr("y", function(d) {
 return h - (d * 4);
 })
 .attr("width", w / dataset.length - barPadding)
 .attr("height", function(d) {
 return d * 4;
 });

6、颜色

用据数据集生成的动态RGB值填充:

attr("fill", function(d) {
 return "rgb(0, 0, " + (d * 10) + ")";

//Width and height
 var w = 500;
 var h = 100;
 var barPadding = 1;
  
 var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
  
 //Create SVG element
 var svg = d3.select("body")
 .append("svg")
 .attr("width", w)
 .attr("height", h);
  
 svg.selectAll("rect")
 .data(dataset)
 .enter()
 .append("rect")
 .attr("x", function(d, i) {
 return i * (w / dataset.length);
 })
 .attr("y", function(d) {
 return h - (d * 4);
 })
 .attr("width", w / dataset.length - barPadding)
 .attr("height", function(d) {
 return d * 4;
 })
 .attr("fill", function(d) {
 return "rgb(0, 0, " + (d * 10) + ")";
 });

7、文本标记(可是数值)

指定XY的坐标

return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;

return h - (d * 4) + 14;

svg.selectAll("text")
 .data(dataset)
 .enter()
 .append("text")
 .text(function(d) {
 return d;
 })
 .attr("text-anchor", "middle")
 .attr("x", function(d, i) {
 return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
 })
 .attr("y", function(d) {
 return h - (d * 4) + 14;
 })
 .attr("font-family", "sans-serif")
 .attr("font-size", "11px")
 .attr("fill", "white");

8、源码

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.         <meta charset="utf-8">  
  5.         <title>testD3-8-drawBar.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.             var barPadding = 1;  
  16.               
  17.             var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,  
  18.                             11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];  
  19.               
  20.             //Create SVG element  
  21.             var svg = d3.select("body")  
  22.                         .append("svg")  
  23.                         .attr("width", w)  
  24.                         .attr("height", h);  
  25.   
  26.             svg.selectAll("rect")  
  27.                .data(dataset)  
  28.                .enter()  
  29.                .append("rect")  
  30.                .attr("x", function(d, i) {  
  31.                     return i * (w / dataset.length);  
  32.                })  
  33.                .attr("y", function(d) {  
  34.                     return h - (d * 4);  
  35.                })  
  36.                .attr("width", w / dataset.length - barPadding)  
  37.                .attr("height", function(d) {  
  38.                     return d * 4;  
  39.                })  
  40.                .attr("fill", function(d) {  
  41.                     return "rgb(0, 0, " + (d * 10) + ")";  
  42.                });  
  43.   
  44.             svg.selectAll("text")  
  45.                .data(dataset)  
  46.                .enter()  
  47.                .append("text")  
  48.                .text(function(d) {  
  49.                     return d;  
  50.                })  
  51.                .attr("text-anchor", "middle")  
  52.                .attr("x", function(d, i) {  
  53.                     return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;  
  54.                })  
  55.                .attr("y", function(d) {  
  56.                     return h - (d * 4) + 14;  
  57.                })  
  58.                .attr("font-family", "sans-serif")  
  59.                .attr("font-size", "11px")  
  60.                .attr("fill", "white");  
  61.         </script>  
  62.   
  63.     </body>  
  64. </html>  
<!DOCTYPE html>
<html>
  <head>
		<meta charset="utf-8">
		<title>testD3-8-drawBar.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 barPadding = 1;
			
			var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
							11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
			
			//Create SVG element
			var svg = d3.select("body")
						.append("svg")
						.attr("width", w)
						.attr("height", h);

			svg.selectAll("rect")
			   .data(dataset)
			   .enter()
			   .append("rect")
			   .attr("x", function(d, i) {
			   		return i * (w / dataset.length);
			   })
			   .attr("y", function(d) {
			   		return h - (d * 4);
			   })
			   .attr("width", w / dataset.length - barPadding)
			   .attr("height", function(d) {
			   		return d * 4;
			   })
			   .attr("fill", function(d) {
					return "rgb(0, 0, " + (d * 10) + ")";
			   });

			svg.selectAll("text")
			   .data(dataset)
			   .enter()
			   .append("text")
			   .text(function(d) {
			   		return d;
			   })
			   .attr("text-anchor", "middle")
			   .attr("x", function(d, i) {
			   		return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
			   })
			   .attr("y", function(d) {
			   		return h - (d * 4) + 14;
			   })
			   .attr("font-family", "sans-serif")
			   .attr("font-size", "11px")
			   .attr("fill", "white");
		</script>

	</body>
</html>


 

9、效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值