微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!
饼状图专题提供饼状图的最新资讯内容,帮你更好的了解饼状图。
canvas制作柱形图/折线图/饼状图,Konva写动态饼状图
制作饼状图<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>canvas绘制扇形图</title><style>body{background:#ccc;}canvas{background:#fff;}</style></head><body><canvas id="myCanvas"></canvas><script>(function(){//定义数据var data = [{title:"apple", value:0.24, color:"green"},{title:"三星", value:0.26, color:"yellow"},{title:"华为", value:0.13, color:"orange"},{title:"锤子", value:0.17, color:"red"},{title:"小米", value:0.08, color:"purple"},{title:"其他", value:0.12, color:"cyan"}];var w=800;var h=600;var cx=w/2;var cy=h/2;var r=200;//获取canvas元素var canvas=document.getElementById("myCanvas");//设置画布宽高canvas.width=w;canvas.height=h;//获取绘图环境var ctx=canvas.getContext("2d");ctx.font="18px Micorsoft YaHei";//遍历数据var beginAngle=Math.PI/6; //开始的点data.forEach(function(item){//弧度范围var angle=item.value*2*Math.PI;//结束的点var endAngle=beginAngle+angle;//绘制扇形区域ctx.beginPath();ctx.moveTo(cx,cy);ctx.arc(cx,cy,r,beginAngle,endAngle);ctx.fillitem.color;ctx.fill();//写字var txtAngle=beginAngle+angle/2;if(txtAngle>=Math.PI/2 && txtAngle<Math.PI*3/2){ctx.textAlign="end";}else{ctx.textAlign="start";}var tx=cx+(r+10)*Math.cos(txtAngle);//Math.cos(x) x 的余弦值。返回的是 -1.0 到 1.0 之间的数;var ty=cy+(r+10)*Math.sin(txtAngle);//Math.sin(x) x 的正玄值。返回值在 -1.0 到 1.0 之间;ctx.fillText(item.title+" "+item.value*100+"%",tx,ty);beginAngle=endAngle;})})()</script></body></html>Konva制作动态饼状图<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>扇形图</title><style>body{background:#ccc;}.konvajs-content{background:#fff;}</style></head><body><div id="box"></div><script src="konva.js"></script><script>var radius=150;var duration=1;var data = [{title:"apple", value:0.24, color:"pink"},{title:"三星", value:0.26, color:"skyblue"},{title:"华为", value:0.13, color:"orange"},{title:"锤子", value:0.17, color:"red"},{title:"小米", value:0.08, color:"purple"},{title:"其他", value:0.12, color:"cyan"}];//创建舞台var stage=new Konva.Stage({container:"#box",width:800,height:600})//创建层var layer=new Konva.Layer({});stage.add(layer);//创建扇形的组var wedgeGroup=new Konva.Group({x:stage.getWidth()/2,y:stage.getHeight()/2});//创建文字的组var textGroup=new Konva.Group({x:stage.getWidth()/2,y:stage.getHeight()/2})var startAngle=0;data.forEach(function(item,index){var angle=item.value*360;//绘制扇形var wedgeShape=new Konva.Wedge({x:0,y:0,radius:radius,angle:0,rotation:startAngle,fill:item.color,name:angle+""});wedgeGroup.add(wedgeShape);//绘制文字var textAngle=startAngle+angle/2;//文字对应的角度var textX=Math.cos(textAngle/180*Math.PI)*(radius+30);var textY=Math.sin(textAngle/180*Math.PI)*(radius+30);var text=new Konva.Text({text:item.title,x:textX,y:textY,fill:item.color,fontSize:12,visible:false});if(textAngle>90&&textAngle<270){text.x(text.x()-text.getWidth())}textGroup.add(text);startAngle+=angle;});//绘制外圆var outerCircle=new Konva.Circle({x:stage.getWidth()/2,y:stage.getHeight()/2,radius:radius+5,stroke:"#ccc",strokeWidth:1});layer.add(wedgeGroup);layer.add(textGroup);layer.add(outerCircle);layer.draw();//设置动画来让扇形动起来var wedgeList=wedgeGroup.getChildren();var animateIndex=0;playAnimate();//调用函数执行动画function playAnimate(){if(animateIndex>=data.length){return;}var wedge=wedgeList[animateIndex];var angle=Number(wedge.name());var animateDuration=duration*(angle/360);wedge.to({angle:angle,duration:animateDuration,onFinish:function(){textGroup.getChildren()[animateIndex].show();animateIndex++;playAnimate();}})}</script></body></html>制作折线图<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>折线图</title><style>body{background:#ccc;}</style></head><body><canvas id="myCanvas"></canvas><script>//定义相关数据 最大100var data=[{year:"2008",value:40},{year:"2009", value:48},{year:"2010", value:50},{year:"2011", value:70},{year:"2012", value:60},{year:"2013", value:40},{year:"2014", value:20},{year:"2015", value:60},{year:"2016", value:80},{year:"2017", value:90}];var xWidth=40;//x轴每一个格子宽度var yWidth=40;//y轴每一个格子宽度var x=100,y=500;//原点坐标var len=data.length;//数据长度//获取canvas元素var canvas=document.querySelector("#myCanvas");canvas.width=800;canvas.height=600;canvas.style.border="1px solid #ccc";canvas.style.backgroundColor="#fff";//获取绘图环境var ctx=canvas.getContext("2d");//绘制坐标系ctx.beginPath();//x轴var xLength=xWidth*(len+1);//x轴长度ctx.moveTo(x,y);ctx.lineTo(x+xLength,y);//箭头ctx.lineTo(x+xLength-10,y-10);//上箭头ctx.moveTo(x+xLength,y);//下箭头ctx.lineTo(x+xLength-10,y+10);//下箭头//刻度ctx.textAlign=&#
封装构造函数,用canvas写饼状图和柱状图
封装构造函数,用canvas写饼状图和柱状图封装函数// 场景function XDLScence( options ) {this.stage = options.stage;//执行场景的初始化this.init = options.init || XDLScence.voidFn;//执行场景的进场动画this.pre = options.pre || XDLScence.voidFn;//执行场景的出场动画this.post = options.post || XDLScence.voidFn;this.layers = options.layers || [new Konva.Layer()];this.name = options.name || '';this.init();}XDLScence.prototype = {constructor: XDLScence,voidFn: function() {},CurrentScence: null,//场景要进入舞台,只需要调用场景的 play方法。play: function () {var _this = this;// doPre,var doPre = function doPre() {// stage.add(_this.layer);// 把当前层添加到舞台_this.layers.forEach(function( val ){_this.stage.add( val );});XDLScence.currentScene = _this;_this.pre();};//如果不是第一个场景,先执行当前场景的出场动画,然后执行下一个场景的入场动画//需要在场景的post方法中执行传进去的 next 方法。if (XDLScence.currentScene) {//执行上一个场景的出场动画XDLScence.currentScene.post(doPre);} else {//如果是第一个场景直接执行入场动画doPre();}}// play};//=============>S=============柱状图数据demo// var data = [{// name: '百度', value: .2, color: 'lightgreen'// },{// name: '阿里', value: .4, color: 'lightgreen'// },{// name: '新浪', value: .1, color: 'lightgreen'// },{// name: '搜狐', value: .1, color: 'lightgreen'// },{// name: '360', value: .2, color: 'lightgreen'// }];//柱状图构造函数function Histogram(option) {this.group = new Konva.Group({x: option.x,y: option.y});this.init(option);}//柱状图的原型对象Histogram.prototype = {constructor: Histogram,init: function( option ) {option.data = option.data || [];//底线的宽度option.blWidth = option.blWidth || 2;option.blColor = option.blColor || 'lightgreen';option.width = option.width || 200;option.height = option.height || 200;option.fontSize = option.fontSize || 12;//把最高的柱状图的高度换算成 柱状图要求的高度。var maxValue = 0;for(var i = 0; i < option.data.length; i++ ) {maxValue = maxValue > option.data[i].value ? maxValue : option.data[i].value;}option.height = option.height / maxValue;//创建底线var bottomLine = new Konva.Line({strokeWidth: option.blWidth,stroke: option.blColor,points: [ 0, 0, option.width, 0 ],lineCap: 'round',lineJoin: 'round'});this.group.add( bottomLine );//rectWidthvar rectAllWidth = option.width / option.data.length;for(var i = 0; i < option.data.length; i++ ) {var tempData = option.data[i];//创建每个柱状图var rect = new Konva.Rect({x: rectAllWidth * (1/4 + i),y: -1 * (option.height * tempData.value) - 1/2*option.blWidth,width: 1/2 * rectAllWidth,height: option.height * tempData.value,fill: tempData.color,shadowBlur: 5,shadowColor: tempData.color,// shadowOffset: {x : 10, y : 10},shadowOpacity: 0.5,opacity: .5,name: 'histogramRect',cornerRadius: 5});//创建 柱状图百分比文本var text = new Konva.Text({x: rectAllWidth * i,y: -1 * (option.height * tempData.value) - 1/2*option.blWidth - option.fontSize -3,fontSize: option.fontSize,fill: tempData.color,fontFamily: '微软雅黑',text: tempData.value * 100 + '%',name: 'txt',width: rectAllWidth,align: 'center'});//创建 柱状图名字文本var nameText = new Konva.Text({x: rectAllWidth * (1/2 + i),y: option.blWidth + 3,fontSize: option.fontSize,fill: tempData.color,fontFamily: '微软雅黑',text: tempData.name,rotation: 30});this.group.add( rect );this.group.add( text );this.group.add( nameText );}},playAnimate: function() {this.group.to({duration: .1,opacity: 1});this.group.find('.histogramRect').each(function( value, index ) {var oldY = value.y();var oldHeight = value.height();value.y(0);value.height(0);value.to({duration: .8,y: oldY,height: oldHeight,opacity: .9});});this.group.find(".txt").each(function(val){var oldY = val.y();val.y(0);val.opacity(.1);val.to({duration: .8,y: oldY,opacity: 1});});},addToGroupOrLayer: function( group ) {group.add(this.group);}};/*使用案例dmeo:var data = [{name: '阿里', value: .2, color: 'lightgreen'},{name: '百度', value: .4, color: 'red'},{name: '新浪', value: .1, color: 'blue'},{name: '盛大', value: .1, color: '#8E8E38'},{name: '360', value: .2, color: 'purple'}];var his = new Histogram({data: data,x: 1/8 * stage.width(),y: 3/4 * stage.height(),blWidth: 2,blColor: 'lightblue',width: 3/4 * stage.width(),height: 1 * stage.height(),fontSize: 14});his.addToGroupOrLayer(layer);layer.draw();his.playAnimate();layer.on('click',function(){his.playAnimate();});*///=============>E 柱状图=============//=============>S 进度条=============function ProgressBar(option) {this.group = new Konva.Group({x: option.x,y: option.y});this.maxWidth = option.width;this.drawLayer = null;this.init(option);}ProgressBar.prototype = {constructor: ProgressBar,init: function( option ) {var innerRect = new Konva.Rect({x: 0,y: 0,width: 0,height: option.height,fill: option.fillColor,name: 'innerRect',cornerRadius: 1/2 * option.height});this.group.add( innerRect );var outerRect = new Konva.Rect({x: 0,y: 0,width: option.width,height: option.height,strokeWidth: option.strokeWidth,stroke: option.strokeColor,name: 'outerRect',cornerRadius: 1/2 * option.height});this.group.add( outerRect );var drawText = new Konva.Tex