Canvas画布绘制折线图

<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		canvas {
			border: 1px solid #ddd;
			margin: 100px;
		}
	</style>
</head>

<body>
	<canvas width="600" height="400"></canvas>
	<script type="text/javascript">
		/*构造函数*/
		var LineChart = function(ctx) {
			/*获取绘图工具*/
			this.ctx = ctx || document.querySelector("canvas").getContext("2d");
			/*画布的大小*/
			this.canvasWidth = this.ctx.canvas.width;
			this.canvasHeight = this.ctx.canvas.height;
			/*网格大小*/
			this.gridSize = 10;
			/*坐标系间距*/
			this.space = 10;
			/*坐标原点*/
			this.x0 = this.space;
			this.y0 = this.canvasHeight - this.space;
			/*箭头大小*/
			this.arrowSize = 10;
			/*绘制点*/
			this.dottedSize = 6;

		}
		/*行为方法*/
		LineChart.prototype.init = function(data) {
			this.drawGrid();
			this.drawAxis();
			this.drawDotted(data);
		}
		/*绘制网格*/
		LineChart.prototype.drawGrid = function() {
			/*x方向的线*/
			var xLineTotal = Math.floor(this.canvasHeight / this.gridSize);
			for(var i = 0; i <= xLineTotal; i++) {
				this.ctx.beginPath();
				this.ctx.moveTo(0, i * this.gridSize - 0.5);
				this.ctx.lineTo(this.canvasWidth, i * this.gridSize - 0.5);
				this.ctx.strokeStyle = "#ddd";
				this.ctx.stroke();
			}
			/*y方向的线*/
			var yLineTotal = Math.floor(this.canvasWidth / this.gridSize);
			for(var i = 0; i <= yLineTotal; i++) {
				this.ctx.beginPath();
				this.ctx.moveTo(i * this.gridSize - 0.5, 0);
				this.ctx.lineTo(i * this.gridSize - 0.5, this.canvasHeight);
				this.ctx.strokeStyle = "#ddd";
				this.ctx.stroke();
			}
		}
		/*绘制坐标系*/
		LineChart.prototype.drawAxis = function() {
			/*x轴*/
			this.ctx.beginPath();
			this.ctx.strokeStyle = "black";
			this.ctx.moveTo(this.x0, this.y0);
			this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
			this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 + this.arrowSize / 2);
			this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 - this.arrowSize / 2);
			this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
			this.ctx.stroke();
			this.ctx.fill();
			/*y轴*/
			this.ctx.beginPath();
			this.ctx.strokeStyle = "black";
			this.ctx.moveTo(this.x0, this.y0);
			this.ctx.lineTo(this.x0, this.space);
			this.ctx.lineTo(this.x0 + this.arrowSize / 2, this.space + this.arrowSize);
			this.ctx.lineTo(this.x0 - this.arrowSize / 2, this.space + this.arrowSize);
			this.ctx.lineTo(this.x0, this.space);
			this.ctx.stroke();
			this.ctx.fill();
		}
		/*绘制点*/
		LineChart.prototype.drawDotted = function(data) {
			/*转换成Canvas坐标*/
			/*绘制点*/
			/*连线*/
			var that = this;
			/*记录当前坐标*/
			var prevCanvasX = 0;
			var prevCanvasY = 0;
			data.forEach(function(item, i) {
				/*x=原点坐标+数据坐标*/
				/*y=原点坐标-数据坐标*/
				var canvasX = that.x0 + item.x;
				var canvasY = that.y0 - item.y;
				/*绘制点*/
				that.ctx.beginPath();
				that.ctx.moveTo(canvasX - that.dottedSize / 2, canvasY - that.dottedSize / 2);
				that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY - that.dottedSize / 2);
				that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY + that.dottedSize / 2);
				that.ctx.lineTo(canvasX - that.dottedSize / 2, canvasY + that.dottedSize / 2);
				that.ctx.closePath();
				that.ctx.fill();
				/*点的连线*/
				if(i == 0) {
					that.ctx.beginPath();
					that.ctx.moveTo(that.x0, that.y0);
					that.ctx.lineTo(canvasX, canvasY);
					that.ctx.stroke();
				} else {
					that.ctx.beginPath();
					that.ctx.moveTo(prevCanvasX, prevCanvasY);
					that.ctx.lineTo(canvasX, canvasY);
					that.ctx.stroke();
				}
				/*记录当前坐标*/
				prevCanvasX = canvasX;
				prevCanvasY = canvasY;
			})
		}
		/*初始化*/
		var data = [{
			x: 100,
			y: 100
		}, {
			x: 200,
			y: 200
		}, {
			x: 300,
			y: 300
		}, {
			x: 400,
			y: 200
		}, {
			x: 500,
			y: 100
		}]
		var LineChart = new LineChart();
		LineChart.init(data);
	</script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值