Flash AS3 Generate pie charts

Source: Internet
Author: User
Tags addchild
/**
* @class:D rawpiegraph (Gao chart)
* @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 ()---> Empty
*//pie.contain---> Get all the chunking references to facilitate operation outside the class
*/
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"
Store Shape objects
private Var __contain:object;
Set angle starting from-90
private Var r:int=-90;
private Var d:uint=30;
private Var _shape:shape;
The center position of the initial pie chart
private Var _x0:number;
private Var _y0:number;
Long axis and short axis length of elliptic pie graphs
private Var _a:number;
private Var _b:number;
The thickness of the pie chart
private Var _h:number;
Transparency
private Var _alpha:number;
List of data
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......> Center x coordinates
* @param:y0......> Center y-coordinate
* @param:a......> Long axis
* @param:b......> Short axis
* @param:h......> thickness
* @param:d atalist......> Data list
* @param:d atalist......> Color list
* @alpha:number......> transparency (with definite value)
*/
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]:d atalist;
_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 <= && 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;
Set the center angle to facilitate the following points to move
Pie.r= (_anglelist[j][0]+_anglelist[j][1])/2;
Pie.addeventlistener (MOUSEEVENT.MOUSE_DOWN,ONMOUSEDOWNX);
AddChild (PIE);
var drakcolor:uint=getdarkcolor (_colorlist[j]);//Dark
var g:graphics=pie.graphics;
G.linestyle (1);
Draw the bottom first
Inner arc
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 ();
Picture inside side
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 ();
Draw the outside side
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 ();

Draw the outer arc side
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 ();
Draw on the surface
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 ("beginning");
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 ("End");
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 >> & 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 {//clear content
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;

}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.