canvas的矩形,圆弧,扇形,文本基本用法+ 绘制病状图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas</title>
<style> canvas{ border:1px solid #eee; } </style>
</head>
<body>
<canvas width="600" height="400">
</canvas>
<script> var mycanvas = document.querySelector('canvas') var ctx =mycanvas.getContext('2d'); var w = ctx.canvas.width; var h = ctx.canvas.height; var num = 10; var ongle = Math.PI*2/10; var x0 = w/2; var y0 = h/2; var getRandomColor =function(){ var r = Math.floor(Math.random()*256) var g = Math.floor(Math.random()*256) var b = Math.floor(Math.random()*256) return 'rgb('+r+','+g+','+b+')'; } for(var i=0;i<num;i++){ var startAngle = i*ongle; var endAngle = (i+1)*ongle; ctx.beginPath(); ctx.moveTo(x0,y0); ctx.arc(x0,y0,150,startAngle,endAngle); ctx.fillStyle =getRandomColor(); ctx.fill(); } </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas绘制饼状图</title>
<style> canvas{ border:1px solid #aaa; margin: 0 auto; } </style>
</head>
<body>
<canvas width='800' height='400'>
</canvas>
<script> var mycanvas = document.querySelector('canvas') var ctx =mycanvas.getContext('2d'); var PieChart = function(){ this.ctx = ctx || document.querySelector('canvas').getContext('2d') this.w = this.ctx.canvas.width; this.h = this.ctx.canvas.height; this.x0 = this.w/2 +60; this.y0 = this.h/2; this.radius = 150; this.outLine = 20; this.rectW = 30; this.rectH = 16; this.space = 20; } PieChart.prototype.init = function(data){ this.drawPie(data); }; PieChart.prototype.drawPie = function(data){ var that = this; var angleList = this.transformAngle(data); var startAngle = 0; angleList.map((item,i) =>{ var endAngle = startAngle + item.angle; ctx.beginPath(); ctx.moveTo(that.x0,that.y0); ctx.arc(that.x0,that.y0,that.radius,startAngle,endAngle); var color = ctx.fillStyle = that.getRandomColor(); ctx.fill(); that.drawTitle(startAngle,item.angle,color,item.title); that.drawDesc(i,item.title); startAngle = endAngle; }) }; PieChart.prototype.drawTitle = function(startAngle,angle,color,title){ var edge = this.radius + this.outLine; var edgeX = Math.cos(startAngle+angle/2)*edge; var edgeY = Math.sin(startAngle+angle/2)*edge; var outX = this.x0 + edgeX; var outY = this.y0 + edgeY; this.ctx.beginPath(); this.ctx.moveTo(this.x0,this.y0); this.ctx.lineTo(outX,outY); this.ctx.strokeStyle =color; this.ctx.textAlign = 'right'; this.ctx.font = '14px Microsoft YaHei' var textWidth = this.ctx.measureText(title).width; console.log(textWidth) console.log(outX+textWidth) console.log(outY) console.log(outX>this.x0) if(outX>this.x0){ this.ctx.lineTo(outX+textWidth,outY); this.ctx.textAlign = 'left'; }else{ this.ctx.lineTo(outX-textWidth,outY); this.ctx.textBaseline = 'bottom'; } this.ctx.stroke(); this.ctx.fillText(title,outX,outY); }; PieChart.prototype.drawDesc = function(index,title){ this.ctx.fillRect(this.space,this.space+index*(this.rectH+this.space),this.rectW,this.rectH) this.ctx.beginPath(); this.ctx.textAlign='left'; this.ctx.textBaseline ='top'; this.ctx.fillText(title,this.space+this.rectW+10,this.space+index*(this.rectH+this.space)) }; PieChart.prototype.transformAngle = function(data){ var total =0; data.map((item,i)=>{ total += item.num; }); data.map((item,i) =>{ var angle = item.num/total* Math.PI*2; item.angle = angle; }) return data }; PieChart.prototype.getRandomColor = function(data){ var r = Math.floor(Math.random()*256) var g = Math.floor(Math.random()*256) var b = Math.floor(Math.random()*256) return 'rgb('+r+','+g+','+b+')'; }; var data = [ { title:'15-20岁', num:30 }, { title:'20-25岁', num:6 }, { title:'25-30岁', num:10 }, { title:'30-35岁', num:35 }, ]; var pieChart = new PieChart(); pieChart.init(data); </script>
</body>
</html>