Posted By

adrianparr on 01/20/11


Tagged

graphics chart draw as3 pie Degrees segment animate radians quadrant curveTo


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

burnandbass
kure69
junaidrao
makeyko


AS3 Pie Chart


 / Published in: ActionScript 3
 

URL: http://www.terrypaton.com/as3-pie-chart/

All credit for this goes to Terry Paton

  1. package com.terrypaton.graphics {
  2. //import flash
  3. import flash.display.Shape;
  4.  
  5. public class DrawPieChart{
  6. public function DrawPieChart() {
  7.  
  8. }
  9. private static var CONVERT_TO_RADIANS:Number = Math.PI / 180;
  10. public static function drawChart(_shape:Shape, _radius:Number, _percent:Number, _colour:uint = 0xFF0000, _rotationOffset:Number = 0) {
  11. if (_percent > 1) {
  12. _percent = 1
  13. }
  14. var angle:Number = 360*_percent
  15.  
  16. _shape.graphics.clear()
  17. _shape.graphics.lineStyle (0);
  18. _shape.graphics.moveTo (0,0);
  19. _shape.graphics.beginFill (_colour,100);
  20. _shape.graphics.lineTo (_radius, 0);
  21. _shape.rotation =_rotationOffset
  22. var nSeg:Number = Math.floor (angle/30);
  23. var pSeg:Number = angle - nSeg * 30
  24. var a:Number = 0.268;
  25. for (var i = 0; i < nSeg; i++) {
  26. var endx:Number = _radius * Math.cos ((i + 1)* 30* CONVERT_TO_RADIANS);
  27. var endy:Number = _radius * Math.sin ((i + 1) * 30* CONVERT_TO_RADIANS);
  28. var ax:Number = endx + _radius * a * Math.cos (((i + 1)* 30 - 90) * CONVERT_TO_RADIANS);
  29. var ay:Number = endy + _radius * a * Math.sin (((i + 1)* 30 - 90) * CONVERT_TO_RADIANS);
  30. _shape.graphics.curveTo (ax,ay,endx,endy);
  31. }
  32. if (pSeg > 0) {
  33. a = Math.tan (pSeg / 2 * CONVERT_TO_RADIANS);
  34. endx = _radius * Math.cos ((i* 30 + pSeg) * CONVERT_TO_RADIANS);
  35. endy = _radius * Math.sin ((i * 30 + pSeg) * CONVERT_TO_RADIANS);
  36. ax = endx + _radius * a * Math.cos ((i* 30+ pSeg - 90) * CONVERT_TO_RADIANS);
  37. ay = endy + _radius * a * Math.sin ((i* 30+ pSeg - 90) * CONVERT_TO_RADIANS);
  38. _shape.graphics.curveTo (ax,ay,endx,endy);
  39. }
  40. }
  41. }
  42. }
  43.  
  44. // Usage Example
  45. //
  46. // import com.terrypaton.graphics.DrawPieChart;
  47. // var segment:Shape = new Shape();
  48. // segment.x = 100;
  49. // segment.y = 100;
  50. // addChild(segment);
  51. // var percent:Number = 0.33;
  52. // DrawPieChart.drawChart(segment, 80, percent, 0x0000FF, -90);

Report this snippet  

You need to login to post a comment.