D3.js绘制柱形图

注意:此次绘制的柱形图不涉及坐标。

步骤:

一.添加矩形。

  <script type="text/javascript">
		var datest = [30,78,90,210,105,98,150,177];     //绘制柱形图所用的数据
		var width = 400;        //svg绘图区域的宽度
		var height = 400;       //svg绘图区域的高度
		var svg = d3.select('body')       //选择<tbody>
								.append('svg')					//在<tbody>中添加<svg>
								.attr('width', width)				//设置<svg>的宽度属性
								.attr('height', height)				//设置<svg>的高度属性
		var padding = {top: 20, right: 20, bottom: 20, left: 20};     //定义上下左右的内边距
		//矩形所占的宽度(包括空白)
		var rectStep = 35;
		//矩形所占的宽度(不包括空白)
		var rectWidth = 30;

		//绘制矩形
		var rect = svg.selectAll('rect')			
					.data(datest)							//绑定数据
					.enter()									
					.append('rect')						//添加rect元素,使他的数量和数组长度一致
				    .attr('fill', 'steelblue')      //设置颜色为steelblue
					.attr('x', function(d,i) {
							return padding.left + i * rectStep;					//设置矩形的x坐标
					})
					.attr('y', function(d) {
							return height - padding.bottom - d;        //设置矩形的y坐标
					})
					.attr('width', rectWidth)                //设置矩形的宽度
					.attr('height', function(d) {
							return d;														//设置矩形的高度
					});
  </script>

效果图:


二.添加文字。

		//添加文字部分
		var text = svg.selectAll('text')
					.data(datest)
					.enter()
					.append('text')
					.attr('fill', 'white')
					.attr('font-size', '14px')
					.attr('text-anchor', 'middle')
					.attr('x', function(d,i) {
							return padding.left + i * rectStep;
					})
					.attr('y', function(d) {
							return height - padding.bottom - d;
					})
					.attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
					.attr('dy', '1em')								//dy是相对于y平移的大小
					.text(function(d) {
							return d;
					})

效果图:


三.更新数据。

	  //更新数据
	  function draw() {
	    //获取矩形的update部分
	    var updateRect = svg.selectAll('rect')
	    										.data(datest);
	    //获取矩形的enter部分
	    var enterRect = updateRect.enter();
	    //获取矩形的exit部分
	    var exitRect = updateRect.exit();
	    //1.矩形的update部分的处理方法
	    updateRect.attr('fill', 'steelblue')      //设置颜色为steelblue
	    		.attr('x', function(d,i) {       //设置矩形的x坐标
	    				return padding.left + i * rectStep;
	    		})
	    		.attr('y', function(d) {         //设置矩形的y坐标
	    			 return height - padding.bottom - d;
	    		})
	    		.attr('width', rectWidth)        //设置矩形的宽度
	    		.attr('height', function(d) {			//设置矩形的高度
	    			return d;
	    		});

	    //2.矩形的enter部分处理办法
	    enterRect.append('rect')
	    		.attr('fill', 'steelblue')        //设置矩形的颜色
	    		.attr('x', function(d,i) {        //设置矩形的x坐标
	    				return padding.left + i * rectStep;
	    		})
	    		.attr('y', function(d) {						//设置巨型的y坐标
	    				return height - padding.bottom - d;
	    		})
	    		.attr('width', rectWidth)        //设置矩形的宽度
	    		.attr('height', function(d) {			//设置矩形的高度
	    				return d;
	    		});

	    //3.矩形的exit处理方法
	    exitRect.remove();


	    //获取文字的update部分
	    var updateText = svg.selectAll('text')
	    										.data(datest);
	    //获取文字的enter部分
	    var enterText = updateText.enter();
	    //获取文字的exit部分
	    var exitText = updateText.exit();

	    //1.文字的update部分的处理方法
			updateText.attr('fill', 'white')
					.attr('font-size', '14px')
					.attr('text-anchor', 'middle')
					.attr('x', function(d,i) {
							return padding.left + i * rectStep;
					})
					.attr('y', function(d) {
							return height - padding.bottom - d;
					})
					.attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
					.attr('dy', '1em')								//dy是相对于y平移的大小
					.text(function(d) {
							return d;
					});
			//2.文字的enter部分的处理方法
			enterText.append('text')
					.attr('fill', 'white')
					.attr('font-size', '14px')
					.attr('text-anchor', 'middle')
					.attr('x', function(d,i) {
							return padding.left + i * rectStep;
					})
					.attr('y', function(d) {
							return height - padding.bottom - d;
					})
					.attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
					.attr('dy', '1em')								//dy是相对于y平移的大小
					.text(function(d) {
							return d;
					});
			//3.文字的exit处理办法
			exitText.remove();
	  }

	  //排序函数
	  function mySort() {
	    datest.sort(d3.ascending);
	    draw();
	  }

	  //增加一个项函数
	  function myAdd() {
	    datest.push(Math.floor(Math.random() * 100));
	    draw();
	  }

  </script>
  <div class="box">
  	<button type="button" onclick="mySort()">排序</button>
		<button type="button" onclick="myAdd()">增加数据</button>
  </div>
</body>

 

效果图:

 

 


全部代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	  .box {
	    width: 300px;
	    display: flex;
	    flex-direction: row;
	    justify-content: space-around;
	    margin-bottom: 50px;
	  }
	</style>
	<script src="https://cdn.bootcss.com/d3/3.2.1/d3.js"></script>
</head>
<body>
  <script type="text/javascript">
		var datest = [30,78,90,210,105,98,150,177];     //绘制柱形图所用的数据
		var width = 500;        //svg绘图区域的宽度
		var height = 500;       //svg绘图区域的高度
		var svg = d3.select('body')       //选择<tbody>
								.append('svg')					//在<tbody>中添加<svg>
								.attr('width', width)				//设置<svg>的宽度属性
								.attr('height', height)				//设置<svg>的高度属性
		var padding = {top: 20, right: 20, bottom: 20, left: 20};     //定义上下左右的内边距
		//矩形所占的宽度(包括空白)
		var rectStep = 35;
		//矩形所占的宽度(不包括空白)
		var rectWidth = 30;

		//绘制矩形
		var rect = svg.selectAll('rect')			
									.data(datest)							//绑定数据
									.enter()									
									.append('rect')						//添加rect元素,使他的数量和数组长度一致
									.attr('fill', 'steelblue')      //设置颜色为steelblue
									.attr('x', function(d,i) {
									  return padding.left + i * rectStep;					//设置矩形的x坐标
									})
									.attr('y', function(d) {
										return height - padding.bottom - d;        //设置矩形的y坐标
									})
									.attr('width', rectWidth)                //设置矩形的宽度
									.attr('height', function(d) {
									  return d;														//设置矩形的高度
									});


		//添加文字部分
		var text = svg.selectAll('text')
									.data(datest)
									.enter()
									.append('text')
									.attr('fill', 'white')
									.attr('font-size', '14px')
									.attr('text-anchor', 'middle')
									.attr('x', function(d,i) {
									  return padding.left + i * rectStep;
									})
									.attr('y', function(d) {
									  return height - padding.bottom - d;
									})
									.attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
									.attr('dy', '1em')								//dy是相对于y平移的大小
									.text(function(d) {
									  return d;
									});

	  //更新数据
	  function draw() {
	    //获取矩形的update部分
	    var updateRect = svg.selectAll('rect')
	    										.data(datest);
	    //获取矩形的enter部分
	    var enterRect = updateRect.enter();
	    //获取矩形的exit部分
	    var exitRect = updateRect.exit();
	    //1.矩形的update部分的处理方法
	    updateRect.attr('fill', 'steelblue')      //设置颜色为steelblue
	    					.attr('x', function(d,i) {       //设置矩形的x坐标
	    					  return padding.left + i * rectStep;
	    					})
	    					.attr('y', function(d) {         //设置矩形的y坐标
	    					  return height - padding.bottom - d;
	    					})
	    					.attr('width', rectWidth)        //设置矩形的宽度
	    					.attr('height', function(d) {			//设置矩形的高度
	    						return d;
	    					});

	    //2.矩形的enter部分处理办法
	    enterRect.append('rect')
	    					.attr('fill', 'steelblue')        //设置矩形的颜色
	    					.attr('x', function(d,i) {        //设置矩形的x坐标
	    					  return padding.left + i * rectStep;
	    					})
	    					.attr('y', function(d) {						//设置巨型的y坐标
	    					  return height - padding.bottom - d;
	    					})
	    					.attr('width', rectWidth)        //设置矩形的宽度
	    					.attr('height', function(d) {			//设置矩形的高度
	    					  return d;
	    					});

	    //3.矩形的exit处理方法
	    exitRect.remove();


	    //获取文字的update部分
	    var updateText = svg.selectAll('text')
	    										.data(datest);
	    //获取文字的enter部分
	    var enterText = updateText.enter();
	    //获取文字的exit部分
	    var exitText = updateText.exit();

	    //1.文字的update部分的处理方法
			updateText.attr('fill', 'white')
								.attr('font-size', '14px')
								.attr('text-anchor', 'middle')
								.attr('x', function(d,i) {
								  return padding.left + i * rectStep;
								})
								.attr('y', function(d) {
								  return height - padding.bottom - d;
								})
								.attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
								.attr('dy', '1em')								//dy是相对于y平移的大小
								.text(function(d) {
								  return d;
								});
			//2.文字的enter部分的处理方法
			enterText.append('text')
							 .attr('fill', 'white')
							 .attr('font-size', '14px')
							 .attr('text-anchor', 'middle')
							 .attr('x', function(d,i) {
							   return padding.left + i * rectStep;
							 })
							 .attr('y', function(d) {
							   return height - padding.bottom - d;
							 })
							 .attr('dx', rectWidth / 2)         //dx是相对于x平移的大小
							 .attr('dy', '1em')								//dy是相对于y平移的大小
							 .text(function(d) {
							   return d;
							 });
			//3.文字的exit处理办法
			exitText.remove();
	  }

	  //排序函数
	  function mySort() {
	    datest.sort(d3.ascending);
	    draw();
	  }

	  //增加一个项函数
	  function myAdd() {
	    datest.push(Math.floor(Math.random() * 100));
	    draw();
	  }

  </script>
  <div class="box">
  	<button type="button" onclick="mySort()">排序</button>
		<button type="button" onclick="myAdd()">增加数据</button>
  </div>
</body>
</html

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,以下是实现这个需求的步骤: 1. 准备数据:你需要有一组收集处理后的数据,例如一个数组或者一个 JSON 对象。数据应该包括每个柱子的名称和高度。 2. 导入 D3.js 库:在 HTML 中导入 D3.js 库,例如: ```html <script src="https://d3js.org/d3.v6.min.js"></script> ``` 3. 创建 SVG 元素:使用 D3.js 创建 SVG 元素,例如: ```javascript const svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height); ``` 其中 `width` 和 `height` 是 SVG 元素的宽度和高度。 4. 创建柱:使用 D3.js 创建柱,例如: ```javascript const bars = svg.selectAll("rect") .data(data) .enter() .append("rect") .attr("x", (d, i) => i * barWidth) .attr("y", d => height - d.height) .attr("width", barWidth - barPadding) .attr("height", d => d.height) .attr("fill", barColor); ``` 其中 `data` 是数据,`barWidth` 是每个柱子的宽度,`barPadding` 是柱子之间的间距,`barColor` 是柱子的颜色。 5. 创建例:使用 D3.js 创建例,例如: ```javascript const legend = svg.selectAll(".legend") .data(data) .enter() .append("g") .attr("class", "legend") .attr("transform", (d, i) => `translate(${width - 100},${i * 20})`); legend.append("rect") .attr("x", 0) .attr("y", 0) .attr("width", 18) .attr("height", 18) .attr("fill", barColor); legend.append("text") .attr("x", 25) .attr("y", 9) .attr("dy", ".35em") .text(d => d.name); ``` 其中 `name` 是每个柱子的名称。 6. 增加交互性功能:使用 D3.js 增加交互性功能,例如: ```javascript bars.on("mouseover", function(d) { d3.select(this).attr("fill", "red"); }) .on("mouseout", function(d) { d3.select(this).attr("fill", barColor); }) .on("click", function(d) { console.log(d.name); }); ``` 其中 `mouseover` 和 `mouseout` 事件用于鼠标悬停和移开时改变柱子的颜色,`click` 事件用于点击柱子时输出其名称。 以上是使用 D3.js 绘制、增加例和交互性功能的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值