source: code/Website/open-flash-chart/charts/BarBase.as@ 7937

Last change on this file since 7937 was 7849, checked in by dennisw, 15 years ago
File size: 4.7 KB
Line 
1package charts {
2
3 import charts.series.Element;
4 import charts.Base;
5 import string.Utils;
6 import global.Global;
7
8
9 public class BarBase extends Base
10 {
11 protected var group:Number;
12 //protected var style:Object;
13 protected var props:Properties;
14 protected var on_show:Properties;
15
16 public function BarBase( json:Object, group:Number )
17 {
18
19 var root:Properties = new Properties( {
20 values: [],
21 colour: '#3030d0',
22 text: '', // <-- default not display a key
23 'font-size': 12,
24 tip: '#val#<br>#x_label#',
25 alpha: 0.6,
26 'on-click': false,
27 'axis': 'left'
28 } );
29
30 this.props = new Properties(json, root);
31
32 /*
33 var on_show_root:Properties = new Properties( {
34 type: "none", //"pop-up",
35 cascade: 3,
36 delay: 0
37 });
38 this.on_show = new Properties(json['on-show'], on_show_root);
39 */
40 this.on_show = this.get_on_show(json['on-show']);
41
42 this.colour = this.props.get_colour('colour');
43 this.key = this.props.get('text');
44 this.font_size = this.props.get('font-size');
45
46 // Minor hack, replace all #key# with this key text:
47 this.props.set( 'tip', this.props.get('tip').replace('#key#', this.key) );
48
49
50 //
51 // bars are grouped, so 3 bar sets on one chart
52 // will arrange them selves next to each other
53 // at each value of X, this.group tell the bar
54 // where it is in that grouping
55 //
56 this.group = group;
57
58 this.values = this.props.get('values');
59 this.add_values();
60 }
61
62 protected function get_on_show(json:Object): Properties {
63
64 var on_show_root:Properties = new Properties( {
65 type: "none", //"pop-up",
66 cascade: 3,
67 delay: 0
68 });
69
70 return new Properties(json, on_show_root);
71 }
72
73
74 //
75 // hello people in the future! I was doing OK until I found some red wine. Now I can't figure stuff out,
76 // like, do I pass in this.axis, or do I make it a member of each PointBarBase? I don't know. Hey, I know
77 // I'll flip a coin and see what happens. It was heads. What does it mean? Mmmmm.... red wine....
78 // Fuck it, I'm passing it in. Makes the resize method messy, but keeps the PointBarBase clean.
79 //
80 public override function resize( sc:ScreenCoordsBase ): void {
81
82 for ( var i:Number = 0; i < this.numChildren; i++ )
83 {
84 var e:Element = this.getChildAt(i) as Element;
85 e.resize( sc );
86 }
87 }
88
89
90 public override function get_max_x():Number {
91
92 var max_index:Number = Number.MIN_VALUE;
93
94 for ( var i:Number = 0; i < this.numChildren; i++ ) {
95
96 var e:Element = this.getChildAt(i) as Element;
97 max_index = Math.max( max_index, e.index );
98 }
99
100 // 0 is a position, so count it:
101 return max_index;
102 }
103
104 public override function get_min_x():Number {
105 return 0;
106 }
107
108 //
109 // override or don't call this if you need better help
110 //
111 protected function get_element_helper_prop( value:Object ): Properties {
112
113 var default_style:Properties = new Properties({
114 colour: this.props.get('colour'),
115 tip: this.props.get('tip'),
116 alpha: this.props.get('alpha'),
117 'on-click': this.props.get('on-click'),
118 axis: this.props.get('axis'),
119 'on-show': this.on_show
120 });
121
122 var s:Properties;
123 if( value is Number )
124 s = new Properties({'top': value}, default_style);
125 else
126 s = new Properties(value, default_style);
127
128 return s;
129 }
130
131 /*
132
133 +-----+
134 | B |
135 +-----+ | +-----+
136 | A | | | C +- - -
137 | | | | | D
138 +-----+-----+---+-----+- - -
139 1 2
140
141 */
142
143
144 public override function closest( x:Number, y:Number ): Object {
145 var shortest:Number = Number.MAX_VALUE;
146 var ex:Element = null;
147
148 for ( var i:Number = 0; i < this.numChildren; i++ )
149 {
150 var e:Element = this.getChildAt(i) as Element;
151
152 e.is_tip = false;
153
154 if( (x > e.x) && (x < e.x+e.width) )
155 {
156 // mouse is in position 1
157 shortest = Math.min( Math.abs( x - e.x ), Math.abs( x - (e.x+e.width) ) );
158 ex = e;
159 break;
160 }
161 else
162 {
163 // mouse is in position 2
164 // get distance to left side and right side
165 var d1:Number = Math.abs( x - e.x );
166 var d2:Number = Math.abs( x - (e.x+e.width) );
167 var min:Number = Math.min( d1, d2 );
168 if( min < shortest )
169 {
170 shortest = min;
171 ex = e;
172 }
173 }
174 }
175 var dy:Number = Math.abs( y - ex.y );
176
177 return { element:ex, distance_x:shortest, distance_y:dy };
178 }
179
180 public override function die():void {
181 super.die();
182 this.props.die();
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.