source: code/Website/open-flash-chart/elements/axis/RadarAxis.as

Last change on this file was 7849, checked in by dennisw, 15 years ago
File size: 3.1 KB
Line 
1package elements.axis {
2 import flash.display.Sprite;
3 import flash.geom.Point;
4 import string.Utils;
5
6
7 public class RadarAxis extends Sprite {
8
9 private var style:Object;
10 private var TO_RADIANS:Number = Math.PI / 180;
11
12 private var colour:Number;
13 private var grid_colour:Number;
14 private var labels:RadarAxisLabels;
15 private var spoke_labels:RadarSpokeLabels;
16
17 function RadarAxis( json:Object )
18 {
19 // default values
20 this.style = {
21 stroke: 2,
22 colour: '#784016',
23 'grid-colour': '#F5E1AA',
24 min: 0,
25 max: null,
26 steps: 1
27 };
28
29 if( json != null )
30 object_helper.merge_2( json, this.style );
31
32 this.colour = Utils.get_colour( this.style.colour );
33 this.grid_colour = Utils.get_colour( this.style['grid-colour'] );
34
35 this.labels = new RadarAxisLabels( json.labels );
36 this.addChild( this.labels );
37
38 this.spoke_labels = new RadarSpokeLabels( json['spoke-labels'] );
39 this.addChild( this.spoke_labels );
40 }
41
42 //
43 // how many items in the X axis?
44 //
45 public function get_range():Range {
46 return new Range( this.style.min, this.style.max, this.style.steps, false );
47 }
48
49 public function resize( sc:ScreenCoordsRadar ):void
50 {
51 this.x = 0;
52 this.y = 0;
53 this.graphics.clear();
54
55 // this is going to change the radius
56 this.spoke_labels.resize( sc );
57
58 var count:Number = sc.get_angles();
59
60 // draw the grid behind the axis
61 this.draw_grid( sc, count );
62 this.draw_axis( sc, count );
63
64 this.labels.resize( sc );
65 }
66
67 private function draw_axis( sc:ScreenCoordsRadar, count:Number ): void {
68
69 this.graphics.lineStyle(this.style.stroke, this.colour, 1, true);
70
71 for ( var i:Number = 0; i < count; i++ ) {
72
73 //
74 // assume 0 is MIN
75 //
76 var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, 0 );
77 this.graphics.moveTo( p.x, p.y );
78
79 var q:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, sc.get_max() );
80 this.graphics.lineTo( q.x, q.y );
81 }
82 }
83
84 private function draw_grid( sc:ScreenCoordsRadar, count:Number ):void {
85
86 this.graphics.lineStyle(1, this.grid_colour, 1, true);
87
88 // floating point addition error:
89 var max:Number = sc.get_max() + 0.00001;
90
91 var r_step:Number = this.style.steps;
92 var p:flash.geom.Point;
93
94 //
95 // start in the middle and move out drawing the grid,
96 // don't draw at 0
97 //
98 for ( var r_pos:Number = r_step; r_pos <= max; r_pos+=r_step ) {
99
100 p = sc.get_get_x_from_pos_and_y_from_val( 0, r_pos );
101 this.graphics.moveTo( p.x, p.y );
102
103 // draw from each spoke
104 for ( var i:Number = 1; i < (count+1); i++ ) {
105
106 p = sc.get_get_x_from_pos_and_y_from_val( i, r_pos );
107 this.graphics.lineTo( p.x, p.y );
108 }
109 }
110 }
111
112 public function die(): void {
113
114 this.style = null;
115 this.labels.die();
116 this.spoke_labels.die();
117
118 this.graphics.clear();
119 while ( this.numChildren > 0 )
120 this.removeChildAt(0);
121 }
122 }
123}
Note: See TracBrowser for help on using the repository browser.