flash AS3生成饼图
/**
* @class:DrawPieGraph(画饼状图)
* @author:ycccc8202
* @date:2007.8.16
* @example:
* import com.ycccc.Graphics.DrawPieGraph
* var dataList:Array=[10,10,10,100,10,10,10,100];
* var pie:DrawPieGraph=new DrawPieGraph(200,200,150,90,15,dataList,null);
* addChild(pie);
* //pie.clearAll()--->清空
* //pie.contain--->获取所有分块的引用对象,方便类外进行操作
*/
package com.ycccc.Graphics{
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.events.MouseEvent;
public class DrawPieGraph extends MovieClip {
public static var PIE_NAME:String="pie"
//存放shape对象
private var __contain:Object;
//设置角度从-90开始
private var R:int=-90;
private var D:uint=30;
private var _shape:Shape;
//初始饼图的圆心位置
private var _x0:Number;
private var _y0:Number;
//椭圆饼图的长轴与短轴长度
private var _a:Number;
private var _b:Number;
//饼图的厚度
private var _h:Number;
//透明度
private var _alpha:Number;
//数据列表
private var _dataList:Array;
private var _colorList:Array;
private var _angleList:Array;
private var _depthList:Array;
//
private var _tween1:Tween;
private var _tween2:Tween;
/**
*@param:x0......>圆心x坐标
*@param:y0......>圆心y坐标
*@param:a......>长轴
*@param:b......>短轴
*@param:h......>厚度
*@param:dataList......>数据列表
*@param:dataList......>颜色列表
*@alpha:Number......>透明度(有确省值)
*/
public function DrawPieGraph(x0:Number,y0:Number,a:Number,b:Number,h:Number,dataList:Array,colorList:Array,alpha:Number=.7) {
_x0=x0;
_y0=y0;
_a=a;
_b=b;
_h=h;
_alpha=alpha
_dataList=dataList==null?[1,1,1,1,1,1,1,1]:dataList;
_colorList=colorList==null?[0xFF0F00,0xFF6600,0xFF9E01,0xFCD202,0xF8FF01,0xB0DE09,0x04D215,0x0D8ECF]:colorList;
setAngleList();
drawPie();
setDepths();
}
private function setAngleList():void {
_angleList=[];
var totalData:int;
var len:uint=_dataList.length;
for (var j:uint=0; j < len; j++) {
totalData+= _dataList[j];
}
for (j=0; j < len; j++) {
if (j == len - 1) {
_angleList.push([R,270]);
} else {
var r:uint=Math.floor(_dataList[j] / totalData * 360);
var posR:int=R + r;
_angleList.push([R,posR]);
R=posR;
trace(r+"___r");
trace(R);
}
}
trace(_angleList + ":::");
}
private function setDepths():void {
_depthList=[];
var len:uint=_angleList.length;
for (var j:uint=0; j < len; j++) {
var minJ:Number=_angleList[j][0];
var maxJ:Number=_angleList[j][1];
switch (true) {
case minJ >= -90 && minJ <= 90 && maxJ<=90 :
_depthList[j]=minJ;
break;
default :
_depthList[j]=1000-minJ;
}
}//end for
trace(_depthList + "::::_depthList");
_depthList=_depthList.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
trace(_depthList);
for (j=0; j<len; j++) {
setChildIndex(__contain[DrawPieGraph.PIE_NAME+_depthList[j]],j);
}
}
private function drawPie():void {
__contain={};
var len:uint=_angleList.length;
var step:uint=1;
for (var j:uint=0; j < len; j++) {
var pie:MovieClip=__contain[DrawPieGraph.PIE_NAME+j]=new MovieClip;
//
pie.out=false;
//设置中心角,方便以下进行点中移动
pie.r=(_angleList[j][0]+_angleList[j][1])/2;
pie.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
addChild(pie);
var drakColor:uint=getDarkColor(_colorList[j]);//深色
var g:Graphics=pie.graphics;
//g.lineStyle(1);
//先画底
//内弧
g.beginFill(_colorList[j],_alpha);
g.moveTo(_x0,_y0+_h);
var r:Number=_angleList[j][0];
var minR:Number=r;
var maxR:int=_angleList[j][1];
while (r + step < maxR) {
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
r+= step;
}
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
//
g.endFill();
//画内侧面
g.beginFill(drakColor,_alpha);
g.moveTo(_x0,_y0+_h);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
g.lineTo(_x0,_y0);
g.endFill();
//画外侧面
g.beginFill(drakColor,_alpha);
g.moveTo(_x0,_y0+_h);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.lineTo(_x0,_y0);
g.endFill();
//画外弧侧面
g.beginFill(drakColor,_alpha);
//g.lineStyle(1);
g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
r=minR;
while (r + step < maxR) {
r+= step;
g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
}
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
while (r - step > minR) {
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
r-= step;
}
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.endFill();
//画上表面
g.beginFill(_colorList[j],_alpha);
g.moveTo(_x0,_y0);
r=minR;
while (r + step < maxR) {
g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
r+= step;
}
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.endFill();
}
}
private function onMouseDownX(e:MouseEvent):void {
var TG:MovieClip=e.target as MovieClip;
var posX:Number=getRPoint(0,0,D,D,TG.r).x;
var posY:Number=getRPoint(0,0,D,D,TG.r).y;
if (!TG.out) {
trace("///////");
trace("起点");
trace(TG.name);
trace("///////");
TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,0,posX,1.5,true);
TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,0,posY,1.5,true);
} else {
trace("///////");
trace("终点");
trace(TG.name);
trace("///////");
TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
trace([TG.x,TG.y]);
TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,TG.x,0,1,true);
TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,TG.y,0,1,true);
}
TG["tweenX"].addEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
}
private function onMotionFinish(e:TweenEvent):void {
//trace(e.position+"...position");
var TG:MovieClip=e.currentTarget.obj as MovieClip;
TG["tweenX"].removeEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
TG.out=!TG.out?true:false
TG.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
}
private function getDarkColor(color:uint):uint {
var r:uint=color >> 16 & 0xFF / 1.3;
var g:uint=color >> 8 & 0xFF / 1.3;
var b:uint=color & 0xFF /1.1;
return r << 16 | g << 8 | b;
}
private function getRPoint(x0:Number,y0:Number,a:Number,b:Number,r:Number):Object {
r=r * Math.PI / 180;
return {x:Math.cos(r) * a + x0,y:Math.sin(r) * b + y0};
}
public function get contain():Object {
return __contain;
}
public function clearAll():void {//清除内容
var len:uint=_dataList.length;
for (var j:uint=0; j<len; j++) {
removeChild(__contain[DrawPieGraph.PIE_NAME+j]);
__contain[DrawPieGraph.PIE_NAME+j]=null;
}
}
}
}
* @class:DrawPieGraph(画饼状图)
* @author:ycccc8202
* @date:2007.8.16
* @example:
* import com.ycccc.Graphics.DrawPieGraph
* var dataList:Array=[10,10,10,100,10,10,10,100];
* var pie:DrawPieGraph=new DrawPieGraph(200,200,150,90,15,dataList,null);
* addChild(pie);
* //pie.clearAll()--->清空
* //pie.contain--->获取所有分块的引用对象,方便类外进行操作
*/
package com.ycccc.Graphics{
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.events.MouseEvent;
public class DrawPieGraph extends MovieClip {
public static var PIE_NAME:String="pie"
//存放shape对象
private var __contain:Object;
//设置角度从-90开始
private var R:int=-90;
private var D:uint=30;
private var _shape:Shape;
//初始饼图的圆心位置
private var _x0:Number;
private var _y0:Number;
//椭圆饼图的长轴与短轴长度
private var _a:Number;
private var _b:Number;
//饼图的厚度
private var _h:Number;
//透明度
private var _alpha:Number;
//数据列表
private var _dataList:Array;
private var _colorList:Array;
private var _angleList:Array;
private var _depthList:Array;
//
private var _tween1:Tween;
private var _tween2:Tween;
/**
*@param:x0......>圆心x坐标
*@param:y0......>圆心y坐标
*@param:a......>长轴
*@param:b......>短轴
*@param:h......>厚度
*@param:dataList......>数据列表
*@param:dataList......>颜色列表
*@alpha:Number......>透明度(有确省值)
*/
public function DrawPieGraph(x0:Number,y0:Number,a:Number,b:Number,h:Number,dataList:Array,colorList:Array,alpha:Number=.7) {
_x0=x0;
_y0=y0;
_a=a;
_b=b;
_h=h;
_alpha=alpha
_dataList=dataList==null?[1,1,1,1,1,1,1,1]:dataList;
_colorList=colorList==null?[0xFF0F00,0xFF6600,0xFF9E01,0xFCD202,0xF8FF01,0xB0DE09,0x04D215,0x0D8ECF]:colorList;
setAngleList();
drawPie();
setDepths();
}
private function setAngleList():void {
_angleList=[];
var totalData:int;
var len:uint=_dataList.length;
for (var j:uint=0; j < len; j++) {
totalData+= _dataList[j];
}
for (j=0; j < len; j++) {
if (j == len - 1) {
_angleList.push([R,270]);
} else {
var r:uint=Math.floor(_dataList[j] / totalData * 360);
var posR:int=R + r;
_angleList.push([R,posR]);
R=posR;
trace(r+"___r");
trace(R);
}
}
trace(_angleList + ":::");
}
private function setDepths():void {
_depthList=[];
var len:uint=_angleList.length;
for (var j:uint=0; j < len; j++) {
var minJ:Number=_angleList[j][0];
var maxJ:Number=_angleList[j][1];
switch (true) {
case minJ >= -90 && minJ <= 90 && maxJ<=90 :
_depthList[j]=minJ;
break;
default :
_depthList[j]=1000-minJ;
}
}//end for
trace(_depthList + "::::_depthList");
_depthList=_depthList.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
trace(_depthList);
for (j=0; j<len; j++) {
setChildIndex(__contain[DrawPieGraph.PIE_NAME+_depthList[j]],j);
}
}
private function drawPie():void {
__contain={};
var len:uint=_angleList.length;
var step:uint=1;
for (var j:uint=0; j < len; j++) {
var pie:MovieClip=__contain[DrawPieGraph.PIE_NAME+j]=new MovieClip;
//
pie.out=false;
//设置中心角,方便以下进行点中移动
pie.r=(_angleList[j][0]+_angleList[j][1])/2;
pie.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
addChild(pie);
var drakColor:uint=getDarkColor(_colorList[j]);//深色
var g:Graphics=pie.graphics;
//g.lineStyle(1);
//先画底
//内弧
g.beginFill(_colorList[j],_alpha);
g.moveTo(_x0,_y0+_h);
var r:Number=_angleList[j][0];
var minR:Number=r;
var maxR:int=_angleList[j][1];
while (r + step < maxR) {
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
r+= step;
}
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
//
g.endFill();
//画内侧面
g.beginFill(drakColor,_alpha);
g.moveTo(_x0,_y0+_h);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
g.lineTo(_x0,_y0);
g.endFill();
//画外侧面
g.beginFill(drakColor,_alpha);
g.moveTo(_x0,_y0+_h);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.lineTo(_x0,_y0);
g.endFill();
//画外弧侧面
g.beginFill(drakColor,_alpha);
//g.lineStyle(1);
g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
r=minR;
while (r + step < maxR) {
r+= step;
g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
}
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
while (r - step > minR) {
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
r-= step;
}
g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
g.endFill();
//画上表面
g.beginFill(_colorList[j],_alpha);
g.moveTo(_x0,_y0);
r=minR;
while (r + step < maxR) {
g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
r+= step;
}
g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
g.endFill();
}
}
private function onMouseDownX(e:MouseEvent):void {
var TG:MovieClip=e.target as MovieClip;
var posX:Number=getRPoint(0,0,D,D,TG.r).x;
var posY:Number=getRPoint(0,0,D,D,TG.r).y;
if (!TG.out) {
trace("///////");
trace("起点");
trace(TG.name);
trace("///////");
TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,0,posX,1.5,true);
TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,0,posY,1.5,true);
} else {
trace("///////");
trace("终点");
trace(TG.name);
trace("///////");
TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
trace([TG.x,TG.y]);
TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,TG.x,0,1,true);
TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,TG.y,0,1,true);
}
TG["tweenX"].addEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
}
private function onMotionFinish(e:TweenEvent):void {
//trace(e.position+"...position");
var TG:MovieClip=e.currentTarget.obj as MovieClip;
TG["tweenX"].removeEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
TG.out=!TG.out?true:false
TG.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
}
private function getDarkColor(color:uint):uint {
var r:uint=color >> 16 & 0xFF / 1.3;
var g:uint=color >> 8 & 0xFF / 1.3;
var b:uint=color & 0xFF /1.1;
return r << 16 | g << 8 | b;
}
private function getRPoint(x0:Number,y0:Number,a:Number,b:Number,r:Number):Object {
r=r * Math.PI / 180;
return {x:Math.cos(r) * a + x0,y:Math.sin(r) * b + y0};
}
public function get contain():Object {
return __contain;
}
public function clearAll():void {//清除内容
var len:uint=_dataList.length;
for (var j:uint=0; j<len; j++) {
removeChild(__contain[DrawPieGraph.PIE_NAME+j]);
__contain[DrawPieGraph.PIE_NAME+j]=null;
}
}
}
}
补充:flash教程,Flash游戏