[7849] | 1 | package charts.series.pies {
|
---|
| 2 |
|
---|
| 3 | import charts.series.Element;
|
---|
| 4 | import charts.Pie;
|
---|
| 5 |
|
---|
| 6 | import flash.display.Sprite;
|
---|
| 7 | import flash.display.GradientType;
|
---|
| 8 | import flash.geom.Matrix;
|
---|
| 9 | import flash.geom.Point;
|
---|
| 10 | import flash.text.TextField;
|
---|
| 11 | import flash.text.TextFormat;
|
---|
| 12 | import flash.events.Event;
|
---|
| 13 | import flash.events.MouseEvent;
|
---|
| 14 | import caurina.transitions.Tweener;
|
---|
| 15 | import caurina.transitions.Equations;
|
---|
| 16 |
|
---|
| 17 | public class PieSlice extends Element {
|
---|
| 18 |
|
---|
| 19 | private var TO_RADIANS:Number = Math.PI / 180;
|
---|
| 20 | private var colour:Number;
|
---|
| 21 | public var slice_angle:Number;
|
---|
| 22 | private var border_width:Number;
|
---|
| 23 | public var angle:Number;
|
---|
| 24 | public var is_over:Boolean;
|
---|
| 25 | public var nolabels:Boolean;
|
---|
| 26 | private var animate:Boolean;
|
---|
| 27 | private var finished_animating:Boolean;
|
---|
| 28 | public var value:Number;
|
---|
| 29 | private var gradientFill:Boolean;
|
---|
| 30 | private var label:String;
|
---|
| 31 | private var pieRadius:Number;
|
---|
| 32 | private var rawToolTip:String;
|
---|
| 33 |
|
---|
| 34 | public var position_original:flash.geom.Point;
|
---|
| 35 | public var position_animate_to:flash.geom.Point;
|
---|
| 36 |
|
---|
| 37 | public function PieSlice( index:Number, value:Properties ) {
|
---|
| 38 |
|
---|
| 39 | this.colour = value.get_colour('colour');
|
---|
| 40 | this.slice_angle = value.get('angle');
|
---|
| 41 | this.border_width = 1;
|
---|
| 42 | this.angle = value.get('start');
|
---|
| 43 | this.animate = value.get('animate');
|
---|
| 44 | this.nolabels = value.get('no-labels');
|
---|
| 45 | this.value = value.get('value');
|
---|
| 46 | this.gradientFill = value.get('gradient-fill');
|
---|
| 47 |
|
---|
| 48 | this.index = index;
|
---|
| 49 | this.rawToolTip = value.get('tip');
|
---|
| 50 |
|
---|
| 51 | this.label = this.replace_magic_values( value.get('label') );
|
---|
| 52 | this.tooltip = this.replace_magic_values( value.get('tip') );
|
---|
| 53 |
|
---|
| 54 | // TODO: why is this commented out in the patch file?
|
---|
| 55 | // this.attach_events();
|
---|
| 56 |
|
---|
| 57 | if ( value.has('on-click') )
|
---|
| 58 | this.set_on_click( value.get('on-click') );
|
---|
| 59 |
|
---|
| 60 | this.finished_animating = false;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | //
|
---|
| 64 | // This is called by the tooltip when it is finished with us,
|
---|
| 65 | // it is only used in modes the pie does not support
|
---|
| 66 | //
|
---|
| 67 | public override function set_tip( b:Boolean ):void {}
|
---|
| 68 |
|
---|
| 69 | //
|
---|
| 70 | // for most objects this is handled in Element,
|
---|
| 71 | // and this tip is displayed just above that object,
|
---|
| 72 | // but for PieSlice we want the tooltip to follow
|
---|
| 73 | // the mouse:
|
---|
| 74 | //
|
---|
| 75 | public override function get_tip_pos():Object {
|
---|
| 76 | var p:flash.geom.Point = this.localToGlobal( new flash.geom.Point(this.mouseX, this.mouseY) );
|
---|
| 77 | return {x:p.x,y:p.y};
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private function replace_magic_values( t:String ): String {
|
---|
| 81 |
|
---|
| 82 | t = t.replace('#label#', this.label );
|
---|
| 83 | t = t.replace('#val#', NumberUtils.formatNumber( this.value ));
|
---|
| 84 | t = t.replace('#radius#', NumberUtils.formatNumber( this.pieRadius ));
|
---|
| 85 | return t;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public override function get_tooltip():String {
|
---|
| 89 | this.tooltip = this.replace_magic_values( this.rawToolTip );
|
---|
| 90 | return this.tooltip;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | //
|
---|
| 94 | // the axis makes no sense here, let's override with null and write our own.
|
---|
| 95 | //
|
---|
| 96 | public override function resize( sc:ScreenCoordsBase ): void { }
|
---|
| 97 | public function pie_resize( sc:ScreenCoordsBase, radius:Number): void {
|
---|
| 98 |
|
---|
| 99 | this.pieRadius = radius;
|
---|
| 100 | this.x = sc.get_center_x();
|
---|
| 101 | this.y = sc.get_center_y();
|
---|
| 102 |
|
---|
| 103 | var label_line_length:Number = 10;
|
---|
| 104 |
|
---|
| 105 | this.graphics.clear();
|
---|
| 106 |
|
---|
| 107 | //line from center to edge
|
---|
| 108 | this.graphics.lineStyle( this.border_width, this.colour, 1 );
|
---|
| 109 | //this.graphics.lineStyle( 0, 0, 0 );
|
---|
| 110 |
|
---|
| 111 | //if the user selected the charts to be gradient filled do gradients
|
---|
| 112 | if( this.gradientFill )
|
---|
| 113 | {
|
---|
| 114 | //set gradient fill
|
---|
| 115 | var colors:Array = [this.colour, this.colour];// this.colour];
|
---|
| 116 | var alphas:Array = [1, 0.5];
|
---|
| 117 | var ratios:Array = [100,255];
|
---|
| 118 | var matrix:Matrix = new Matrix();
|
---|
| 119 | matrix.createGradientBox(radius*2, radius*2, 0, -radius, -radius);
|
---|
| 120 |
|
---|
| 121 | //matrix.createGradientBox(this.stage.stageWidth, this.stage.stageHeight, (3 * Math.PI / 2), -150, 10);
|
---|
| 122 |
|
---|
| 123 | this.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
|
---|
| 124 | }
|
---|
| 125 | else
|
---|
| 126 | this.graphics.beginFill(this.colour, 1);
|
---|
| 127 |
|
---|
| 128 | this.graphics.moveTo(0, 0);
|
---|
| 129 | this.graphics.lineTo(radius, 0);
|
---|
| 130 |
|
---|
| 131 | var angle:Number = 4;
|
---|
| 132 | var a:Number = Math.tan((angle/2)*TO_RADIANS);
|
---|
| 133 |
|
---|
| 134 | var i:Number = 0;
|
---|
| 135 | var endx:Number;
|
---|
| 136 | var endy:Number;
|
---|
| 137 | var ax:Number;
|
---|
| 138 | var ay:Number;
|
---|
| 139 |
|
---|
| 140 | //draw curve segments spaced by angle
|
---|
| 141 | for ( i = 0; i + angle < this.slice_angle; i += angle) {
|
---|
| 142 | endx = radius*Math.cos((i+angle)*TO_RADIANS);
|
---|
| 143 | endy = radius*Math.sin((i+angle)*TO_RADIANS);
|
---|
| 144 | ax = endx+radius*a*Math.cos(((i+angle)-90)*TO_RADIANS);
|
---|
| 145 | ay = endy+radius*a*Math.sin(((i+angle)-90)*TO_RADIANS);
|
---|
| 146 | this.graphics.curveTo(ax, ay, endx, endy);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | //when aproaching end of slice, refine angle interval
|
---|
| 151 | angle = 0.08;
|
---|
| 152 | a = Math.tan((angle/2)*TO_RADIANS);
|
---|
| 153 |
|
---|
| 154 | for ( ; i+angle < slice_angle; i+=angle) {
|
---|
| 155 | endx = radius*Math.cos((i+angle)*TO_RADIANS);
|
---|
| 156 | endy = radius*Math.sin((i+angle)*TO_RADIANS);
|
---|
| 157 | ax = endx+radius*a*Math.cos(((i+angle)-90)*TO_RADIANS);
|
---|
| 158 | ay = endy+radius*a*Math.sin(((i+angle)-90)*TO_RADIANS);
|
---|
| 159 | this.graphics.curveTo(ax, ay, endx, endy);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | //close slice
|
---|
| 163 | this.graphics.endFill();
|
---|
| 164 | this.graphics.lineTo(0, 0);
|
---|
| 165 |
|
---|
| 166 | if (!this.nolabels) this.draw_label_line( radius, label_line_length, this.slice_angle );
|
---|
| 167 | // return;
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | if( this.animate )
|
---|
| 171 | {
|
---|
| 172 | if ( !this.finished_animating ) {
|
---|
| 173 | this.finished_animating = true;
|
---|
| 174 | // have we already rotated this slice?
|
---|
| 175 | Tweener.addTween(this, { rotation:this.angle, time:1.4, transition:Equations.easeOutCirc, onComplete:this.done_animating } );
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | else
|
---|
| 179 | {
|
---|
| 180 | this.done_animating();
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | private function done_animating():void {
|
---|
| 185 | this.rotation = this.angle;
|
---|
| 186 | this.finished_animating = true;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | // draw the line from the pie slice to the label
|
---|
| 191 | private function draw_label_line( rad:Number, tick_size:Number, slice_angle:Number ):void {
|
---|
| 192 | //draw line
|
---|
| 193 |
|
---|
| 194 | // TODO: why is this commented out?
|
---|
| 195 | //this.graphics.lineStyle( 1, this.colour, 100 );
|
---|
| 196 | //move to center of arc
|
---|
| 197 |
|
---|
| 198 | // TODO: need this?
|
---|
| 199 | //this.graphics.moveTo(rad*Math.cos(slice_angle/2*TO_RADIANS), rad*Math.sin(slice_angle/2*TO_RADIANS));
|
---|
| 200 | //
|
---|
| 201 | //final line positions
|
---|
| 202 | //var lineEnd_x:Number = (rad+tick_size)*Math.cos(slice_angle/2*TO_RADIANS);
|
---|
| 203 | //var lineEnd_y:Number = (rad+tick_size)*Math.sin(slice_angle/2*TO_RADIANS);
|
---|
| 204 | //this.graphics.lineTo(lineEnd_x, lineEnd_y);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | public override function toString():String {
|
---|
| 209 | return "PieSlice: "+ this.get_tooltip();
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | public function getTicAngle():Number {
|
---|
| 213 | return this.angle + (this.slice_angle / 2);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | public function isRightSide():Boolean
|
---|
| 217 | {
|
---|
| 218 | return (this.getTicAngle() >= 270) || (this.getTicAngle() <= 90);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | public function get_colour(): Number {
|
---|
| 222 | return this.colour;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|