as3饼图
先看效果:填入统计数据后,再查看。。。。。。
DrawPieGraph.as
1package CYPL.Graphics{
2 import fl.transitions.Tween;
3 import fl.transitions.easing.*;
4 import fl.transitions.TweenEvent;
5 import flash.display.MovieClip;
6 import flash.display.Sprite;
7 import flash.display.Shape;
8 import flash.display.Graphics;
9 import flash.events.MouseEvent;
10 import flash.text.*;
11 public class DrawPieGraph extends MovieClip {
12
13 //存放shape对象
14 private var __contain:Object;
15 //设置角度从-90开始
16 private var R:int = -90;
17 private var D:uint = 30;
18 private var _shape:Shape;
19 //初始饼图的圆心位置
20 private var _x0:Number;
21 private var _y0:Number;
22 //椭圆饼图的长轴与短轴长度
23 private var _a:Number;
24 private var _b:Number;
25 //饼图的厚度
26 private var _h:Number;
27 //透明度
28 private var _alpha:Number;
29 //数据列表
30 private var _nameList:Array;
31 private var _dataList:Array;
32 private var _colorList:Array;
33 private var _angleList:Array;
34 private var _depthList:Array;
35 //
36 private var _tween1:Tween;
37 private var _tween2:Tween;
38 private var showorder:Boolean;
39 private var percent:Boolean;
40 private var fontcolor:uint;
41 private var orderx:Number;
42 private var ordery:Number;
43 private var orderxy:Number;
44 private var shape:Array;
45 private var sp:Sprite;
46 private var Rect:Sprite;
47 private var namelabel:TextField;
48
49
50 /**==========================15个参数==================================================================================
51 *@param:x0>圆心x坐标=========1
52 *@param:y0>圆心y坐标=========2
53 *@param:a>长轴===============3
54 *@param:b>短轴================4
55 *@param:h>厚度=================5
56 *@alpha:Number>透明度===============6
57 *@param:dataList>数据列表(有默认值)===============7
58 *@param:dataList>颜色列表(有默认值)==================8
59 *@param:dataList>名称列表(有默认值)==================9
60 *@param:dataList>是否显示百分比==================10
61 *@param:dataList>百分比字体颜色==================11
62 *@param:dataList>是否显示图例==================12
63 *@param:dataList>图例x坐标==================13
64 *@param:dataList>图例y坐标==================14
65 *@param:dataList>图例间距==================15
66 */
67 public function DrawPieGraph(x0:Number,y0:Number,a:Number,b:Number,h:Number,Alpha:Number,dataList:Array,colorList:Array,nameList:Array,ifpercent:Boolean,perColor:uint ,ifshoworder:Boolean, myorderx:Number, myordery:Number, myorderxy:Number) {
68 _x0 = x0;
69 _y0 = y0;
70 _a = a;
71 _b = b;
72 _h = h;
73 percent = ifpercent;
74 fontcolor = perColor;
75 showorder = ifshoworder;
76 orderx = myorderx;
77 ordery = myordery;
78 orderxy = myorderxy;
79 _nameList = nameList;
80 _alpha = Alpha;
81 _colorList = colorList == null ? [0x330099,0x04D215,0x7E9B06,0x990065,0xFF9E01,0xFF6600,0xFE9A9A,0xA00101,0xFCD202]:colorList;
82 sp=new Sprite();
83 sp.mouseEnabled = sp.mouseChildren = false;
84 ToolTip.init(sp);
85 render(dataList);
86 }
87 public function render(dataList:Array ) {
88
89 R = -90;
90 setAngleList(dataList);
91 clearAll();
92 drawRect();
93 drawPie();
94 }
95
96 private function setAngleList(dataList:Array):void {
97 _dataList = dataList == null ? [1,1,1,1,1,1,1,1]:dataList;
98 _angleList = [];
99 var totalData:int;
100 var len:uint = _dataList.length;
101 for (var j:uint=0; j < len; j++) {
102 totalData += _dataList[j];
103 }
104 if (totalData==0) {
105 for (j=0; j < len; j++) {
106 _dataList[j]=1;
107 totalData=len;
108 }
109 }
110
111 for (j=0; j < len; j++) {
112 if (j==len-1) {
113 _angleList.push([R,270]);
114 } else {
115 var r:uint=Math.floor(_dataList[j]/totalData*360);
116 var posR:int=R+r;
117 _angleList.push([R,posR]);
118 R=posR;
119 }
120 }
121 for (j=len-1; j >0; j--) {
122
123 if(_dataList[j]!=0){
124 _angleList.splice(j, 1,[_angleList[j][0],270]);
125 break;
126 }
127
128
129 }
130
131 }
132 private function setDepths():void {
133 _depthList=[];
134 var len:uint=_angleList.length;
135 for (var j:uint=0; j < len; j++) {
136 var minJ:Number=_angleList[j][0];
137 var maxJ:Number=_angleList[j][1];
138 switch (true) {
139 case minJ >= -90 && minJ <= 90 && maxJ<=90 :
140 _depthList[j]=minJ;
141 break;
142 default :
143 _depthList[j]=1000-minJ;
144 }
145 }
146 _depthList=_depthList.sort(Array.NUMERIC|Array.RETURNINDEXEDARRAY);
147 for (j=0; j<len; j++) {
148 if (this.contains(__contain["shape"+_depthList[j]])) {
149 setChildIndex(__contain["shape"+_depthList[j]],j);
150 }
151 }
152 }
153 private function drawRect():void {
154 if (showorder) {
155 for (var jk:uint=0; jk < _angleList.length; jk++) {
156 Rect=new Sprite ;//图例方块
157 addChild(Rect);
158 Rect.name="abcde"+jk;
159 if (_dataList[jk]!=0) {
160 Rect.buttonMode=true;
161 }
162 namelabel=new TextField ;//图例的文字说明,它和方块同为pie的子级
163 //namelabel.text=_nameList[jk];
164 namelabel.selectable=false;
165 namelabel.autoSize=TextFieldAutoSize.LEFT;
166 namelabel.htmlText="<font size='16' color='#ffffff' face='黑体' >"+_nameList[jk]+"</font>";
167 //下划线效果namelabel.htmlText= "<font size='16' color='#ffffff' face='黑体' >"+"<u>"+_nameList[jk]+"</u>"+"</font>";
168 addChild(namelabel);
169 var beginx:Number=orderx;
170 var beginy:Number=ordery;
171 var jianxy:Number=orderxy+12;//图例方块的边长
172 if (_dataList[jk]!=0) {
173 Rect.graphics.beginFill(_colorList[jk],1);
174 }
175 Rect.graphics.lineStyle(1,0xffffff,1);
176 Rect.graphics.moveTo(0,0);
177 Rect.graphics.lineTo(12,0);
178 Rect.graphics.lineTo(12,12);
179 Rect.graphics.lineTo(0,12);
180 Rect.graphics.lineTo(0,0);
181 if (_dataList[jk]!=0) {
182 Rect.graphics.endFill();
183 }
184 Rect.x=beginx;
185 Rect.y=beginy+jianxy*jk;
186 namelabel.x=Rect.x+20;
187 namelabel.y=Rect.y-4;
188 _dataList[jk]!=0?Rect.addEventListener(MouseEvent.MOUSE_OVER,onMouseDownX):0;
189 }
190 }
191
192
193 }
194 public function drawPie():void {
195 __contain={};
196 var len:uint=_angleList.length;
197 var step:uint=1;
198
199 shape=new Array();
200 for (var j:uint=0; j < len; j++) {
201
202 __contain["shape"+j]=new MovieClip ;
203 addChild(__contain["shape"+j]);
204 shape.push(__contain["shape" + j]);
205 if (_dataList[j]!=0) {
206
207 __contain["shape"+j].out=false;
208 //设置中心角,方便以下进行中点移动
209 __contain["shape"+j].r=(_angleList[j][0]+_angleList[j][1])/2;
210 __contain["shape"+j].addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
211
212 ToolTip.register(__contain["shape"+j], _nameList[j]);
213 __contain["shape"+j].name="shape"+j;
214 var drakColor:uint=getDarkColor(_colorList[j]);//深色
215 var g:Graphics=__contain["shape"+j].graphics;
216 //====================================================================================================先画百分比
217 if (percent) {
218 /*g.beginFill(0x000000,_alpha);
219 g.lineStyle(1, 0x000000, 1);
220 if(0<(_angleList[j][0]+ _angleList[j][1])/2 &&(_angleList[j][0]+ _angleList[j][1])/2<180 ){
221 g.moveTo(getRPoint(_x0, _y0+_h, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0+_h, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
222 g.lineTo(getRPoint(_x0,_y0+_h, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0+_h, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
223 }else{
224 g.moveTo(getRPoint(_x0, _y0, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
225 g.lineTo(getRPoint(_x0,_y0, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
226 }
227 g.endFill()*/
228 var total:Number=0;
229 for (var jh:uint=0; jh <_dataList.length; jh++) {
230 total+=_dataList[jh];
231 }
232 var prelabel:TextField=new TextField ;
233 prelabel.text=String(Math.floor((_dataList[j]/total)*10000)/100)+"%";
234 prelabel.selectable=false;
235 prelabel.background=true;
236 prelabel.backgroundColor=drakColor;
237 __contain["shape"+j].addChild(prelabel);
238 var myformat:TextFormat = new TextFormat();
239 myformat.font="Verdana";
240 myformat.size=12;
241 myformat.color=0xffffff;
242 prelabel.setTextFormat(myformat);
243 prelabel.gridFitType="pixel";
244 prelabel.antiAliasType="advanced";
245 prelabel.sharpness=-400;
246 prelabel.autoSize=TextFieldAutoSize.LEFT;
247 var zhongwei:Number=(_angleList[j][0] + _angleList[j][1] ) / 2;
248 if (zhongwei>0&&zhongwei<90) {
249 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x;
250 prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
251 } else if (zhongwei>90 && zhongwei<180) {
252 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width;
253 prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
254 } else if (zhongwei>180 && zhongwei<270) {
255 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
256 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
257 } else if (zhongwei>-90 && zhongwei<0) {
258 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
259 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
260 } else if (zhongwei == 0) {
261 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
262 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
263 } else if (zhongwei == 90) {
264 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width/2;
265 prelabel.y=getRPoint(_x0,_y0+_h+1,_a,_b,zhongwei).y;
266 } else if (zhongwei == 180) {
267 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
268 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
269 }
270 }
271 //-------------------------------------- //内弧
272 //g.lineStyle(1);
273 //先画底
274 //内弧
275 g.lineStyle(1,drakColor,_alpha);
276 g.beginFill(_colorList[j],_alpha);
277 g.moveTo(_x0,_y0+_h);
278 var r:Number=_angleList[j][0];
279 var minR:Number=r;
280 var maxR:int=_angleList[j][1];
281 while (r + step < maxR) {
282 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
283 r+=step;
284 }
285 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
286 g.endFill();
287 //画内侧面
288 g.lineStyle(1,drakColor,_alpha);
289 g.beginFill(drakColor,_alpha);
290 g.moveTo(_x0,_y0+_h);
291 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
292 g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
293 g.lineTo(_x0,_y0);
294 g.endFill();
295 //画外侧面
296 g.lineStyle(1,drakColor,1);
297 g.beginFill(drakColor,_alpha);
298 g.moveTo(_x0,_y0+_h);
299 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
300 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
301 g.lineTo(_x0,_y0);
302 g.endFill();
303 //画外弧侧面
304 //画外弧侧面
305 //--------------------------------
306 //下边高
307 //下边外侧边高
308 if (minR<=0&&maxR>=0&&maxR<=180) {
309 //解决90度时外侧面,显示事实为90度,实际为0度,因为我们整个旋转了-90度;
310 g.lineStyle(1,drakColor,1);
311 g.beginFill(drakColor,1);
312 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
313 var k:Number=minR;
314 while (k < 0) {
315 k+=step;
316 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
317 }
318 g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
319 while (k > minR) {
320 k-=step;
321 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
322 }
323 g.endFill();
324 g.lineStyle(1,drakColor,1);
325 g.beginFill(drakColor,1);
326 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
327 k=0;
328 while (k < maxR) {
329 k+=step;
330 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
331 }
332 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
333 while (k > 0) {
334 k-=step;
335 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
336 }
337 g.endFill();
338 } else if (maxR >= 180 && minR <= 180 && minR >= 0) {
339 //解决180度时外侧面,显示事实为270度,实际为180度,因为我们整个旋转了-90度;
340 g.lineStyle(1,drakColor,1);
341 g.beginFill(drakColor,1);
342 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
343 k=minR;
344 while (k < 180) {
345 k+=step;
346 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
347 }
348 g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
349 while (k > minR) {
350 k-=step;
351 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
352 }
353 g.endFill();
354 g.lineStyle(1,drakColor,1);
355 g.beginFill(drakColor,1);
356 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
357 k=180;
358 while (k < maxR) {
359 k+=step;
360 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
361 }
362 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
363 while (k > 180) {
364 k-=step;
365 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
366 }
367 g.endFill();
368 } else if (minR <= 0 && maxR >= 180) {
369 //解决同时处于0度和180度的情况
370 g.lineStyle(1,drakColor,1);
371 g.beginFill(drakColor,1);
372 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
373 k=minR;
374 while (k < 0) {
375 k+=step;
376 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
377 }
378 g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
379 while (k > minR) {
380 k-=step;
381 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
382 }
383 g.endFill();
384 g.lineStyle(1,drakColor,1);
385 g.beginFill(drakColor,1);
386 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
387 k=0;
388 while (k < 180) {
389 k+=step;
390 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
391 }
392 g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
393 while (k > 0) {
394 k-=step;
395 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
396 }
397 g.endFill();
398 g.lineStyle(1,drakColor,1);
399 g.beginFill(drakColor,1);
400 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
401 k=180;
402 while (k < maxR) {
403 k+=step;
404 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
405 }
406 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
407 while (k > 180) {
408 k-=step;
409 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
410 }
411 g.endFill();
412 } else {
413 g.lineStyle(1,drakColor,1);
414 g.beginFill(drakColor,1);
415 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
416 k=minR;
417 while (k < maxR) {
418 k+=step;
419 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
420 }
421 //g.lineTo(getRPoint(_x0, _y0+_h, _a, _b, maxR).x, getRPoint(_x0, _y0+_h, _a, _b, maxR).y);
422 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
423 while (k > minR) {
424 k-=step;
425 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
426 }
427 g.endFill();
428 }
429 //画上表面
430 g.lineStyle(1,_colorList[j],_alpha);
431 g.beginFill(_colorList[j],_alpha);
432 g.moveTo(_x0,_y0);
433 r=minR;
434 while (r + step < maxR) {
435 g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
436 r+=step;
437 }
438 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
439 g.endFill();
440 }
441 }
442 setDepths();
443 addChild(sp);//提示标签加在这里,防止标签被饼块遮住
444 }
445 private function onMouseDownX(e:MouseEvent):void {
446 var TG:MovieClip=shape[Number(e.currentTarget.name.substring(5))] as MovieClip;
447 var posX:Number=getRPoint(0,0,D,D,TG.r).x;
448 var posY:Number=getRPoint(0,0,D,D,TG.r).y;
449 if (! TG.out) {
450 TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
451 TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,0,posX,1.5,true);
452 TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,0,posY,1.5,true);
453 } else {
454 TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
455 TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,TG.x,0,1,true);
456 TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,TG.y,0,1,true);
457 }
458 TG["tweenX"].addEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
459 }
460
461 //===============================================================================
462 private function onMotionFinish(e:TweenEvent):void {
463 var TG:MovieClip=e.currentTarget.obj as MovieClip;
464 TG["tweenX"].removeEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
465 TG.out=! TG.out?true:false;
466 TG.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
467 }
468 private function getDarkColor(color:uint):uint {
469 var r:uint=color>>16&0xFF/1.3;//设置red通道的值
470 var g:uint=color>>8&0xFF/1.3;//设置green通道的值
471 var b:uint=color&0xFF/1.1;//设置blue通道的值
472 return r << 16 | g << 8 | b;//得到新颜色
473 }
474 private function getRPoint(x0:Number,y0:Number,a:Number,b:Number,r:Number):Object {
475 r=r*Math.PI/180;
476 return {x:Math.cos(r) * a + x0,y:Math.sin(r) * b + y0};
477 }
478 public function get contain():Object {
479 return __contain;
480 }
481 private function clearAll():void {//清除内容
482
483 var len:uint=_dataList.length;
484 while(this.numChildren!=0){
485 removeChildAt(0);
486 }
487 for (var j:uint=0; j<len; j++) {
488
489 if (__contain) {
490 __contain["shape"+j]=null;
491 }
492 }
493
494 Rect=null;
495
496 }
497 }
498}
2 import fl.transitions.Tween;
3 import fl.transitions.easing.*;
4 import fl.transitions.TweenEvent;
5 import flash.display.MovieClip;
6 import flash.display.Sprite;
7 import flash.display.Shape;
8 import flash.display.Graphics;
9 import flash.events.MouseEvent;
10 import flash.text.*;
11 public class DrawPieGraph extends MovieClip {
12
13 //存放shape对象
14 private var __contain:Object;
15 //设置角度从-90开始
16 private var R:int = -90;
17 private var D:uint = 30;
18 private var _shape:Shape;
19 //初始饼图的圆心位置
20 private var _x0:Number;
21 private var _y0:Number;
22 //椭圆饼图的长轴与短轴长度
23 private var _a:Number;
24 private var _b:Number;
25 //饼图的厚度
26 private var _h:Number;
27 //透明度
28 private var _alpha:Number;
29 //数据列表
30 private var _nameList:Array;
31 private var _dataList:Array;
32 private var _colorList:Array;
33 private var _angleList:Array;
34 private var _depthList:Array;
35 //
36 private var _tween1:Tween;
37 private var _tween2:Tween;
38 private var showorder:Boolean;
39 private var percent:Boolean;
40 private var fontcolor:uint;
41 private var orderx:Number;
42 private var ordery:Number;
43 private var orderxy:Number;
44 private var shape:Array;
45 private var sp:Sprite;
46 private var Rect:Sprite;
47 private var namelabel:TextField;
48
49
50 /**==========================15个参数==================================================================================
51 *@param:x0>圆心x坐标=========1
52 *@param:y0>圆心y坐标=========2
53 *@param:a>长轴===============3
54 *@param:b>短轴================4
55 *@param:h>厚度=================5
56 *@alpha:Number>透明度===============6
57 *@param:dataList>数据列表(有默认值)===============7
58 *@param:dataList>颜色列表(有默认值)==================8
59 *@param:dataList>名称列表(有默认值)==================9
60 *@param:dataList>是否显示百分比==================10
61 *@param:dataList>百分比字体颜色==================11
62 *@param:dataList>是否显示图例==================12
63 *@param:dataList>图例x坐标==================13
64 *@param:dataList>图例y坐标==================14
65 *@param:dataList>图例间距==================15
66 */
67 public function DrawPieGraph(x0:Number,y0:Number,a:Number,b:Number,h:Number,Alpha:Number,dataList:Array,colorList:Array,nameList:Array,ifpercent:Boolean,perColor:uint ,ifshoworder:Boolean, myorderx:Number, myordery:Number, myorderxy:Number) {
68 _x0 = x0;
69 _y0 = y0;
70 _a = a;
71 _b = b;
72 _h = h;
73 percent = ifpercent;
74 fontcolor = perColor;
75 showorder = ifshoworder;
76 orderx = myorderx;
77 ordery = myordery;
78 orderxy = myorderxy;
79 _nameList = nameList;
80 _alpha = Alpha;
81 _colorList = colorList == null ? [0x330099,0x04D215,0x7E9B06,0x990065,0xFF9E01,0xFF6600,0xFE9A9A,0xA00101,0xFCD202]:colorList;
82 sp=new Sprite();
83 sp.mouseEnabled = sp.mouseChildren = false;
84 ToolTip.init(sp);
85 render(dataList);
86 }
87 public function render(dataList:Array ) {
88
89 R = -90;
90 setAngleList(dataList);
91 clearAll();
92 drawRect();
93 drawPie();
94 }
95
96 private function setAngleList(dataList:Array):void {
97 _dataList = dataList == null ? [1,1,1,1,1,1,1,1]:dataList;
98 _angleList = [];
99 var totalData:int;
100 var len:uint = _dataList.length;
101 for (var j:uint=0; j < len; j++) {
102 totalData += _dataList[j];
103 }
104 if (totalData==0) {
105 for (j=0; j < len; j++) {
106 _dataList[j]=1;
107 totalData=len;
108 }
109 }
110
111 for (j=0; j < len; j++) {
112 if (j==len-1) {
113 _angleList.push([R,270]);
114 } else {
115 var r:uint=Math.floor(_dataList[j]/totalData*360);
116 var posR:int=R+r;
117 _angleList.push([R,posR]);
118 R=posR;
119 }
120 }
121 for (j=len-1; j >0; j--) {
122
123 if(_dataList[j]!=0){
124 _angleList.splice(j, 1,[_angleList[j][0],270]);
125 break;
126 }
127
128
129 }
130
131 }
132 private function setDepths():void {
133 _depthList=[];
134 var len:uint=_angleList.length;
135 for (var j:uint=0; j < len; j++) {
136 var minJ:Number=_angleList[j][0];
137 var maxJ:Number=_angleList[j][1];
138 switch (true) {
139 case minJ >= -90 && minJ <= 90 && maxJ<=90 :
140 _depthList[j]=minJ;
141 break;
142 default :
143 _depthList[j]=1000-minJ;
144 }
145 }
146 _depthList=_depthList.sort(Array.NUMERIC|Array.RETURNINDEXEDARRAY);
147 for (j=0; j<len; j++) {
148 if (this.contains(__contain["shape"+_depthList[j]])) {
149 setChildIndex(__contain["shape"+_depthList[j]],j);
150 }
151 }
152 }
153 private function drawRect():void {
154 if (showorder) {
155 for (var jk:uint=0; jk < _angleList.length; jk++) {
156 Rect=new Sprite ;//图例方块
157 addChild(Rect);
158 Rect.name="abcde"+jk;
159 if (_dataList[jk]!=0) {
160 Rect.buttonMode=true;
161 }
162 namelabel=new TextField ;//图例的文字说明,它和方块同为pie的子级
163 //namelabel.text=_nameList[jk];
164 namelabel.selectable=false;
165 namelabel.autoSize=TextFieldAutoSize.LEFT;
166 namelabel.htmlText="<font size='16' color='#ffffff' face='黑体' >"+_nameList[jk]+"</font>";
167 //下划线效果namelabel.htmlText= "<font size='16' color='#ffffff' face='黑体' >"+"<u>"+_nameList[jk]+"</u>"+"</font>";
168 addChild(namelabel);
169 var beginx:Number=orderx;
170 var beginy:Number=ordery;
171 var jianxy:Number=orderxy+12;//图例方块的边长
172 if (_dataList[jk]!=0) {
173 Rect.graphics.beginFill(_colorList[jk],1);
174 }
175 Rect.graphics.lineStyle(1,0xffffff,1);
176 Rect.graphics.moveTo(0,0);
177 Rect.graphics.lineTo(12,0);
178 Rect.graphics.lineTo(12,12);
179 Rect.graphics.lineTo(0,12);
180 Rect.graphics.lineTo(0,0);
181 if (_dataList[jk]!=0) {
182 Rect.graphics.endFill();
183 }
184 Rect.x=beginx;
185 Rect.y=beginy+jianxy*jk;
186 namelabel.x=Rect.x+20;
187 namelabel.y=Rect.y-4;
188 _dataList[jk]!=0?Rect.addEventListener(MouseEvent.MOUSE_OVER,onMouseDownX):0;
189 }
190 }
191
192
193 }
194 public function drawPie():void {
195 __contain={};
196 var len:uint=_angleList.length;
197 var step:uint=1;
198
199 shape=new Array();
200 for (var j:uint=0; j < len; j++) {
201
202 __contain["shape"+j]=new MovieClip ;
203 addChild(__contain["shape"+j]);
204 shape.push(__contain["shape" + j]);
205 if (_dataList[j]!=0) {
206
207 __contain["shape"+j].out=false;
208 //设置中心角,方便以下进行中点移动
209 __contain["shape"+j].r=(_angleList[j][0]+_angleList[j][1])/2;
210 __contain["shape"+j].addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
211
212 ToolTip.register(__contain["shape"+j], _nameList[j]);
213 __contain["shape"+j].name="shape"+j;
214 var drakColor:uint=getDarkColor(_colorList[j]);//深色
215 var g:Graphics=__contain["shape"+j].graphics;
216 //====================================================================================================先画百分比
217 if (percent) {
218 /*g.beginFill(0x000000,_alpha);
219 g.lineStyle(1, 0x000000, 1);
220 if(0<(_angleList[j][0]+ _angleList[j][1])/2 &&(_angleList[j][0]+ _angleList[j][1])/2<180 ){
221 g.moveTo(getRPoint(_x0, _y0+_h, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0+_h, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
222 g.lineTo(getRPoint(_x0,_y0+_h, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0+_h, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
223 }else{
224 g.moveTo(getRPoint(_x0, _y0, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0, _a, _b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
225 g.lineTo(getRPoint(_x0,_y0, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).x,getRPoint(_x0, _y0, 20*_a, 20*_b, (_angleList[j][0] + _angleList[j][1] ) / 2).y);
226 }
227 g.endFill()*/
228 var total:Number=0;
229 for (var jh:uint=0; jh <_dataList.length; jh++) {
230 total+=_dataList[jh];
231 }
232 var prelabel:TextField=new TextField ;
233 prelabel.text=String(Math.floor((_dataList[j]/total)*10000)/100)+"%";
234 prelabel.selectable=false;
235 prelabel.background=true;
236 prelabel.backgroundColor=drakColor;
237 __contain["shape"+j].addChild(prelabel);
238 var myformat:TextFormat = new TextFormat();
239 myformat.font="Verdana";
240 myformat.size=12;
241 myformat.color=0xffffff;
242 prelabel.setTextFormat(myformat);
243 prelabel.gridFitType="pixel";
244 prelabel.antiAliasType="advanced";
245 prelabel.sharpness=-400;
246 prelabel.autoSize=TextFieldAutoSize.LEFT;
247 var zhongwei:Number=(_angleList[j][0] + _angleList[j][1] ) / 2;
248 if (zhongwei>0&&zhongwei<90) {
249 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x;
250 prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
251 } else if (zhongwei>90 && zhongwei<180) {
252 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width;
253 prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
254 } else if (zhongwei>180 && zhongwei<270) {
255 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
256 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
257 } else if (zhongwei>-90 && zhongwei<0) {
258 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
259 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
260 } else if (zhongwei == 0) {
261 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
262 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
263 } else if (zhongwei == 90) {
264 prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width/2;
265 prelabel.y=getRPoint(_x0,_y0+_h+1,_a,_b,zhongwei).y;
266 } else if (zhongwei == 180) {
267 prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
268 prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
269 }
270 }
271 //-------------------------------------- //内弧
272 //g.lineStyle(1);
273 //先画底
274 //内弧
275 g.lineStyle(1,drakColor,_alpha);
276 g.beginFill(_colorList[j],_alpha);
277 g.moveTo(_x0,_y0+_h);
278 var r:Number=_angleList[j][0];
279 var minR:Number=r;
280 var maxR:int=_angleList[j][1];
281 while (r + step < maxR) {
282 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
283 r+=step;
284 }
285 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
286 g.endFill();
287 //画内侧面
288 g.lineStyle(1,drakColor,_alpha);
289 g.beginFill(drakColor,_alpha);
290 g.moveTo(_x0,_y0+_h);
291 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
292 g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
293 g.lineTo(_x0,_y0);
294 g.endFill();
295 //画外侧面
296 g.lineStyle(1,drakColor,1);
297 g.beginFill(drakColor,_alpha);
298 g.moveTo(_x0,_y0+_h);
299 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
300 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
301 g.lineTo(_x0,_y0);
302 g.endFill();
303 //画外弧侧面
304 //画外弧侧面
305 //--------------------------------
306 //下边高
307 //下边外侧边高
308 if (minR<=0&&maxR>=0&&maxR<=180) {
309 //解决90度时外侧面,显示事实为90度,实际为0度,因为我们整个旋转了-90度;
310 g.lineStyle(1,drakColor,1);
311 g.beginFill(drakColor,1);
312 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
313 var k:Number=minR;
314 while (k < 0) {
315 k+=step;
316 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
317 }
318 g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
319 while (k > minR) {
320 k-=step;
321 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
322 }
323 g.endFill();
324 g.lineStyle(1,drakColor,1);
325 g.beginFill(drakColor,1);
326 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
327 k=0;
328 while (k < maxR) {
329 k+=step;
330 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
331 }
332 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
333 while (k > 0) {
334 k-=step;
335 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
336 }
337 g.endFill();
338 } else if (maxR >= 180 && minR <= 180 && minR >= 0) {
339 //解决180度时外侧面,显示事实为270度,实际为180度,因为我们整个旋转了-90度;
340 g.lineStyle(1,drakColor,1);
341 g.beginFill(drakColor,1);
342 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
343 k=minR;
344 while (k < 180) {
345 k+=step;
346 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
347 }
348 g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
349 while (k > minR) {
350 k-=step;
351 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
352 }
353 g.endFill();
354 g.lineStyle(1,drakColor,1);
355 g.beginFill(drakColor,1);
356 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
357 k=180;
358 while (k < maxR) {
359 k+=step;
360 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
361 }
362 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
363 while (k > 180) {
364 k-=step;
365 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
366 }
367 g.endFill();
368 } else if (minR <= 0 && maxR >= 180) {
369 //解决同时处于0度和180度的情况
370 g.lineStyle(1,drakColor,1);
371 g.beginFill(drakColor,1);
372 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
373 k=minR;
374 while (k < 0) {
375 k+=step;
376 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
377 }
378 g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
379 while (k > minR) {
380 k-=step;
381 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
382 }
383 g.endFill();
384 g.lineStyle(1,drakColor,1);
385 g.beginFill(drakColor,1);
386 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
387 k=0;
388 while (k < 180) {
389 k+=step;
390 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
391 }
392 g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
393 while (k > 0) {
394 k-=step;
395 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
396 }
397 g.endFill();
398 g.lineStyle(1,drakColor,1);
399 g.beginFill(drakColor,1);
400 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
401 k=180;
402 while (k < maxR) {
403 k+=step;
404 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
405 }
406 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
407 while (k > 180) {
408 k-=step;
409 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
410 }
411 g.endFill();
412 } else {
413 g.lineStyle(1,drakColor,1);
414 g.beginFill(drakColor,1);
415 g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
416 k=minR;
417 while (k < maxR) {
418 k+=step;
419 g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
420 }
421 //g.lineTo(getRPoint(_x0, _y0+_h, _a, _b, maxR).x, getRPoint(_x0, _y0+_h, _a, _b, maxR).y);
422 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
423 while (k > minR) {
424 k-=step;
425 g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
426 }
427 g.endFill();
428 }
429 //画上表面
430 g.lineStyle(1,_colorList[j],_alpha);
431 g.beginFill(_colorList[j],_alpha);
432 g.moveTo(_x0,_y0);
433 r=minR;
434 while (r + step < maxR) {
435 g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
436 r+=step;
437 }
438 g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
439 g.endFill();
440 }
441 }
442 setDepths();
443 addChild(sp);//提示标签加在这里,防止标签被饼块遮住
444 }
445 private function onMouseDownX(e:MouseEvent):void {
446 var TG:MovieClip=shape[Number(e.currentTarget.name.substring(5))] as MovieClip;
447 var posX:Number=getRPoint(0,0,D,D,TG.r).x;
448 var posY:Number=getRPoint(0,0,D,D,TG.r).y;
449 if (! TG.out) {
450 TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
451 TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,0,posX,1.5,true);
452 TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,0,posY,1.5,true);
453 } else {
454 TG.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
455 TG["tweenX"]=new Tween(TG,"x",Bounce.easeOut,TG.x,0,1,true);
456 TG["tweenY"]=new Tween(TG,"y",Bounce.easeOut,TG.y,0,1,true);
457 }
458 TG["tweenX"].addEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
459 }
460
461 //===============================================================================
462 private function onMotionFinish(e:TweenEvent):void {
463 var TG:MovieClip=e.currentTarget.obj as MovieClip;
464 TG["tweenX"].removeEventListener(TweenEvent.MOTION_FINISH,onMotionFinish);
465 TG.out=! TG.out?true:false;
466 TG.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);
467 }
468 private function getDarkColor(color:uint):uint {
469 var r:uint=color>>16&0xFF/1.3;//设置red通道的值
470 var g:uint=color>>8&0xFF/1.3;//设置green通道的值
471 var b:uint=color&0xFF/1.1;//设置blue通道的值
472 return r << 16 | g << 8 | b;//得到新颜色
473 }
474 private function getRPoint(x0:Number,y0:Number,a:Number,b:Number,r:Number):Object {
475 r=r*Math.PI/180;
476 return {x:Math.cos(r) * a + x0,y:Math.sin(r) * b + y0};
477 }
478 public function get contain():Object {
479 return __contain;
480 }
481 private function clearAll():void {//清除内容
482
483 var len:uint=_dataList.length;
484 while(this.numChildren!=0){
485 removeChildAt(0);
486 }
487 for (var j:uint=0; j<len; j++) {
488
489 if (__contain) {
490 __contain["shape"+j]=null;
491 }
492 }
493
494 Rect=null;
495
496 }
497 }
498}
ToolTip.as
1 package CYPL.Graphics
2 {
3
4 import flash.accessibility.AccessibilityProperties;
5 import flash.display.*;
6 import flash.events.*;
7 import flash.geom.Point;
8 import flash.text.*;
9 /**
10 * @link kinglong@gmail.com
11 * @author Kinglong
12 * @version 0.1
13 * @since 20090608
14 * @playerversion fp9+
15 * 热区提示
16 */
17 public class ToolTip extends Sprite {
18 private static var instance:ToolTip = null;
19 private var label:TextField;
20 private var area:DisplayObject;
21 public function ToolTip() {
22 label = new TextField();
23 label.autoSize = TextFieldAutoSize.LEFT;
24 label.selectable = false;
25 label.multiline = false;
26 label.wordWrap = false;
27 label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
28 label.text = "提示信息";
29 label.x = 5;
30 label.y = 2;
31 addChild(label);
32 redraw();
33 visible = false;
34 mouseEnabled = mouseChildren = false;
35 }
36
37 private function redraw() {
38 var w:Number = 10 + label.width;
39 var h:Number = 4 + label.height;
40 this.graphics.clear();
41 this.graphics.beginFill(0x000000, 0.4);
42 this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
43 this.graphics.moveTo(6, 3 + h);
44 this.graphics.lineTo(12, 3 + h);
45 this.graphics.lineTo(9, 8 + h);
46 this.graphics.lineTo(6, 3 + h);
47 this.graphics.endFill();
48 this.graphics.beginFill(0xffffff);
49 this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
50 this.graphics.moveTo(3, h);
51 this.graphics.lineTo(9, h);
52 this.graphics.lineTo(6, 5 + h);
53 this.graphics.lineTo(3, h);
54 this.graphics.endFill();
55 }
56
57 public static function init(base:DisplayObjectContainer) {
58 if (instance == null) {
59 instance = new ToolTip();
60 base.addChild(instance);
61 }
62 }
63
64 public static function register(area:DisplayObject, message:String):void {
65 if(instance != null){
66 var prop:AccessibilityProperties = new AccessibilityProperties();
67 prop.description = message;
68 area.accessibilityProperties = prop;
69 area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
70 }
71 }
72
73 public static function unregister(area:DisplayObject):void {
74 if (instance != null) {
75 area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
76 }
77 }
78
79 public function show(area:DisplayObject):void {
80 this.area = area;
81 this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
82 this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
83 label.text = area.accessibilityProperties.description;
84 redraw();
85 }
86
87
88 public function hide():void {
89 this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
90 this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
91 this.area = null;
92 visible = false;
93 }
94
95 public function move(point:Point):void {
96 var lp:Point = this.parent.globalToLocal(point);
97 this.x = lp.x - 6;
98 this.y = lp.y - label.height - 12;
99 if(!visible){
100 visible = true;
101 }
102 }
103
104 private function handler(event:MouseEvent):void {
105 switch(event.type) {
106 case MouseEvent.MOUSE_OUT:
107 this.hide();
108 break;
109 case MouseEvent.MOUSE_MOVE:
110 this.move(new Point(event.stageX, event.stageY));
111 break;
112 case MouseEvent.MOUSE_OVER:
113 this.show(event.currentTarget as DisplayObject);
114 this.move(new Point(event.stageX, event.stageY))
115 break;
116 }
117 }
118
119 }
120 }
2 {
3
4 import flash.accessibility.AccessibilityProperties;
5 import flash.display.*;
6 import flash.events.*;
7 import flash.geom.Point;
8 import flash.text.*;
9 /**
10 * @link kinglong@gmail.com
11 * @author Kinglong
12 * @version 0.1
13 * @since 20090608
14 * @playerversion fp9+
15 * 热区提示
16 */
17 public class ToolTip extends Sprite {
18 private static var instance:ToolTip = null;
19 private var label:TextField;
20 private var area:DisplayObject;
21 public function ToolTip() {
22 label = new TextField();
23 label.autoSize = TextFieldAutoSize.LEFT;
24 label.selectable = false;
25 label.multiline = false;
26 label.wordWrap = false;
27 label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
28 label.text = "提示信息";
29 label.x = 5;
30 label.y = 2;
31 addChild(label);
32 redraw();
33 visible = false;
34 mouseEnabled = mouseChildren = false;
35 }
36
37 private function redraw() {
38 var w:Number = 10 + label.width;
39 var h:Number = 4 + label.height;
40 this.graphics.clear();
41 this.graphics.beginFill(0x000000, 0.4);
42 this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
43 this.graphics.moveTo(6, 3 + h);
44 this.graphics.lineTo(12, 3 + h);
45 this.graphics.lineTo(9, 8 + h);
46 this.graphics.lineTo(6, 3 + h);
47 this.graphics.endFill();
48 this.graphics.beginFill(0xffffff);
49 this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
50 this.graphics.moveTo(3, h);
51 this.graphics.lineTo(9, h);
52 this.graphics.lineTo(6, 5 + h);
53 this.graphics.lineTo(3, h);
54 this.graphics.endFill();
55 }
56
57 public static function init(base:DisplayObjectContainer) {
58 if (instance == null) {
59 instance = new ToolTip();
60 base.addChild(instance);
61 }
62 }
63
64 public static function register(area:DisplayObject, message:String):void {
65 if(instance != null){
66 var prop:AccessibilityProperties = new AccessibilityProperties();
67 prop.description = message;
68 area.accessibilityProperties = prop;
69 area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
70 }
71 }
72
73 public static function unregister(area:DisplayObject):void {
74 if (instance != null) {
75 area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
76 }
77 }
78
79 public function show(area:DisplayObject):void {
80 this.area = area;
81 this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
82 this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
83 label.text = area.accessibilityProperties.description;
84 redraw();
85 }
86
87
88 public function hide():void {
89 this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
90 this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
91 this.area = null;
92 visible = false;
93 }
94
95 public function move(point:Point):void {
96 var lp:Point = this.parent.globalToLocal(point);
97 this.x = lp.x - 6;
98 this.y = lp.y - label.height - 12;
99 if(!visible){
100 visible = true;
101 }
102 }
103
104 private function handler(event:MouseEvent):void {
105 switch(event.type) {
106 case MouseEvent.MOUSE_OUT:
107 this.hide();
108 break;
109 case MouseEvent.MOUSE_MOVE:
110 this.move(new Point(event.stageX, event.stageY));
111 break;
112 case MouseEvent.MOUSE_OVER:
113 this.show(event.currentTarget as DisplayObject);
114 this.move(new Point(event.stageX, event.stageY))
115 break;
116 }
117 }
118
119 }
120 }
时间轴代码
1 import CYPL.Graphics.*;
2 /**==========================15个参数==================================================================================
3 *@param:x0>圆心x坐标=========1
4 *@param:y0>圆心y坐标=========2
5 *@param:a>长轴===============3
6 *@param:b>短轴================4
7 *@param:h>厚度=================5
8 *@alpha:Number>透明度===============6
9 *@param:dataList>数据列表(有默认值)===============7
10 *@param:dataList>颜色列表(有默认值)==================8
11 *@param:dataList>名称列表(有默认值)==================9
12 *@param:dataList>是否显示百分比==================10
13 *@param:dataList>百分比字体颜色==================11
14 *@param:dataList>是否显示图例==================12
15 *@param:dataList>图例x坐标==================13
16 *@param:dataList>图例y坐标==================14
17 *@param:dataList>图例间距==================15
18 */
19 var dataList:Array = [100,100,100,100,100,100];
20 //var dataList:Array = [0,0,100,200,100,300,100,0,0];
21 for (var i:int=0; i<6; i++) {
22
23 this["txt" + i].restrict = "0-9";
24
25
26 }
27 var nameList:Array = ["星光大道","春节晚会","同一首歌","梦想剧场","NBA 赛场","非常“6+1”"];
28 //trace(dataList+"..list");
29 var pie:DrawPieGraph = new DrawPieGraph(200,240,120,80,40,1,dataList,null,nameList,true,0xffffff,true,440,240,10);
30 addChild(pie);
31 btn.addEventListener(MouseEvent.CLICK,fun);
32 btn.setStyle("textFormat",new TextFormat("宋体",14,0xffffff));
33 function fun(e):void {
34 for (var i:int=0; i<6; i++) {
35 dataList[i] = Number(this["txt" + i].text);
36 }
37 //pie.clearAll()
38 pie.render(dataList);
39 }
2 /**==========================15个参数==================================================================================
3 *@param:x0>圆心x坐标=========1
4 *@param:y0>圆心y坐标=========2
5 *@param:a>长轴===============3
6 *@param:b>短轴================4
7 *@param:h>厚度=================5
8 *@alpha:Number>透明度===============6
9 *@param:dataList>数据列表(有默认值)===============7
10 *@param:dataList>颜色列表(有默认值)==================8
11 *@param:dataList>名称列表(有默认值)==================9
12 *@param:dataList>是否显示百分比==================10
13 *@param:dataList>百分比字体颜色==================11
14 *@param:dataList>是否显示图例==================12
15 *@param:dataList>图例x坐标==================13
16 *@param:dataList>图例y坐标==================14
17 *@param:dataList>图例间距==================15
18 */
19 var dataList:Array = [100,100,100,100,100,100];
20 //var dataList:Array = [0,0,100,200,100,300,100,0,0];
21 for (var i:int=0; i<6; i++) {
22
23 this["txt" + i].restrict = "0-9";
24
25
26 }
27 var nameList:Array = ["星光大道","春节晚会","同一首歌","梦想剧场","NBA 赛场","非常“6+1”"];
28 //trace(dataList+"..list");
29 var pie:DrawPieGraph = new DrawPieGraph(200,240,120,80,40,1,dataList,null,nameList,true,0xffffff,true,440,240,10);
30 addChild(pie);
31 btn.addEventListener(MouseEvent.CLICK,fun);
32 btn.setStyle("textFormat",new TextFormat("宋体",14,0xffffff));
33 function fun(e):void {
34 for (var i:int=0; i<6; i++) {
35 dataList[i] = Number(this["txt" + i].text);
36 }
37 //pie.clearAll()
38 pie.render(dataList);
39 }