封装构造函数,用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