RadarAxis.as 2.9 KB

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