D3--------绘制柱状图

一:思路

绘制柱状图要实现的效果如下:

由图可知,绘制该柱状图总体上需要以下几个步骤:

(1)需要一个svg来承载长方形和文本。

(2)绘制长方形和文本。

(3)添加实现排序和增加数据这两个按钮并实现相应的功能。

二:代码实现

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="d3.js"></script>
	</head>
	<body>
		<script>
			//数据
			dataset=[145,115,78,145,115,136,98];
			//画布数据
			width=300;
			height=300;
			//柱状图数据
			padding={left:20,right:20,top:20,bottom:20};
			rectTotal=25;
			rectWidth=20;
			//创建画布
			svg=d3.select("body").append("svg")
					.attr("width",width)
					.attr("height",height);
			//绘制图形
			function draw(){
				/*绑定数据并绘制柱状图*/
				var rectUpdate=svg.selectAll("rect").data(dataset);
				//rect update
				rectUpdate.attr("x",function(d,i){return padding.left+i*rectTotal;})
							.attr("y",function(d){return height-padding.top-d;})
							.attr("width",rectWidth)
							.attr("height",function(d){return d;})
							.attr("fill","cyan");
				//rect enter
				var rectEnter=rectUpdate.enter().append("rect")
							.attr("x",function(d,i){return padding.left+i*rectTotal;})
							.attr("y",function(d){return height-padding.top-d;})
							.attr("width",rectWidth)
							.attr("height",function(d){return d;})
							.attr("fill","cyan");
				//rect exit
				var rectExit=rectUpdate.exit().remove();
				/*绑定数据并绘制文本*/
				var textUpdate=svg.selectAll("text").data(dataset);
				//text update
				textUpdate.attr("x",function(d,i){return padding.left+i*rectTotal;})
						  .attr("y",function(d){return height-padding.top-d;})
						  .attr("dx",rectWidth/2)
						  .attr("dy","1em")
						  .attr("text-anchor","middle")
						  .attr("fill","purple")
						  .attr("font-size","13px")
						  .text(function(d){return d;});
				//text enter
				var textEnter=textUpdate.enter().append("text")
									.attr("x",function(d,i){return padding.left+i*rectTotal;})
						  .attr("y",function(d){return height-padding.top-d;})
						  .attr("dx",rectWidth/2)
						  .attr("dy","1em")
						  .attr("text-anchor","middle")
						  .attr("fill","purple")
						  .attr("font-size","13px")
						  .text(function(d){return d;});
				//text exit
				var exit=textUpdate.exit().remove();
							
			}
			//首次展示柱状图
			draw();
			//增加数据
			function DataAdd(){
				var num=Math.round(Math.random()*200);
				dataset.push(num);
				draw();
			}
			//排序
			function DataSort(){
				dataset.sort(d3.ascending);
				draw();
			}
		</script>
	</body>
	<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<button type="button" onclick="DataAdd()">增加数据</button>
	<button type="button" onclick="DataSort()">排序</button>
</html>

三:总结

1.绘制柱状图和文本的关键在于参数的设置。

rect参数主要有 width宽度,height高度,x矩形左上角的横坐标(向右为正),y矩形左上角的纵坐标(向下为正),fill填充颜色

text的主要参数x文本左上角的横坐标(向右为正),y文本左上角的纵坐标(向下为正),dx水平方向的长度(向右为正),dy竖直方向的长度(向下为正),其中1em代表一行,text-anchor内容在文本当中的位置,text文本内容,fill填充颜色,font-size字体大小

2.对于初学者,有一个重要点需要特别注意,即在增加数据时绘制矩形的时候。第一次绘制柱状图,在第一次绑定时,update里没有任何数据,所有的数据都在enter里面。之后再绑定的时候,之前加入的数据此时全部在update里面,之后增加的数据则在enter里。

3.填增数据时,除了以上方法外,也有一种更简单粗暴的方法,即对之前的图形全部删除重新绘制,代码如下,

function draw(){
				svg.selectAll("rect").remove();
				svg.selectAll("text").remove();
				var rects=svg.selectAll("rect").data(dataset).enter().append("rect")
						.attr("x",function(d,i){
							return padding.left+rectTotal*i;
						})
						.attr("y",function(d){
							return height-padding.top-d;
						})
						.attr("width",rectWidth)
						.attr("height",function(d){return d;})
						.attr("fill","cyan");
				var texts=svg.selectAll("text").data(dataset).enter().append("text")
						.attr("x",function(d,i){
							return padding.left+rectTotal*i;
						})
						.attr("y",function(d){
							return height-padding.top-d;
						})
						.attr("dx",rectWidth/2)
						.attr("dy","1em")
						.attr("fill","purple")
						.attr("text-anchor","middle")
						.text(function(d){return d;});
			}

4.绘制文本时,比较美观的做法是让文本居于矩形中间,若text的x和y位置相同,此时需要设置dx和dy属性,此时dx设置为矩形宽度的一半即可,dy设置为"1em",表示占一行。

5.button标签,type表示按钮类别,onclick表示点击按钮触发的事件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对比柱状图可以使用d3.js中的矩形图(rect)元素来绘制。以下是一个简单的例子: HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>D3.js对比柱状图</title> <script src="https://d3js.org/d3.v5.min.js"></script> </head> <body> <svg id="chart" width="400" height="300"></svg> <script src="script.js"></script> </body> </html> ``` JavaScript代码: ```javascript // 数据 var data = [ {name: 'A', value: 20}, {name: 'B', value: 35}, {name: 'C', value: 10}, {name: 'D', value: 15}, {name: 'E', value: 25} ]; // 定义svg布大小 var width = 400; var height = 300; // 定义比例尺 var xScale = d3.scaleBand() .domain(data.map(function(d) { return d.name; })) .range([0, width]) .padding(0.1); var yScale = d3.scaleLinear() .domain([0, d3.max(data, function(d) { return d.value; })]) .range([height, 0]); // 创建svg元素 var svg = d3.select('#chart') .append('g') .attr('transform', 'translate(' + 50 + ',' + 10 + ')'); // 绘制矩形 svg.selectAll('rect') .data(data) .enter() .append('rect') .attr('x', function(d) { return xScale(d.name); }) .attr('y', function(d) { return yScale(d.value); }) .attr('width', xScale.bandwidth()) .attr('height', function(d) { return height - yScale(d.value); }) .style('fill', 'steelblue'); ``` 解释: 1. 定义数据,包含每个柱子的名称和值。 2. 定义布大小。 3. 定义x轴和y轴的比例尺,x轴比例尺使用`scaleBand`,y轴比例尺使用`scaleLinear`。 4. 创建svg元素,并将其偏移50像素到右侧和10像素到下方。 5. 绘制矩形,使用`selectAll`和`data`方法绑定数据,使用`enter`方法进入数据,使用`append`方法添加矩形元素,使用`attr`方法设置x、y、width和height属性,使用`style`方法设置颜色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值