source: code/Website/open-flash-chart/charts/series/bars/Round3D.as@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 21.7 KB
Line 
1 package charts.series.bars {
2 import flash.filters.DropShadowFilter;
3 import flash.geom.Matrix;
4 import charts.series.bars.Base;
5
6 public class Round3D extends Base
7 {
8
9 public function Round3D( index:Number, props:Properties, group:Number ) {
10
11 super(index, props, group);
12 //super(index, {'top':props.get('top')}, props.get_colour('colour'), props.get('tip'), props.get('alpha'), group);
13
14 var dropShadow:DropShadowFilter = new flash.filters.DropShadowFilter();
15 dropShadow.blurX = 5;
16 dropShadow.blurY = 5;
17 dropShadow.distance = 3;
18 dropShadow.angle = 45;
19 dropShadow.quality = 2;
20 dropShadow.alpha = 0.4;
21 // apply shadow filter
22 this.filters = [dropShadow];
23 }
24
25 public override function resize( sc:ScreenCoordsBase ):void {
26
27 this.graphics.clear();
28 var h:Object = this.resize_helper( sc as ScreenCoords );
29
30 this.bg( h.width, h.height, h.upside_down );
31 this.glass( h.width, h.height, h.upside_down );
32 }
33
34 private function bg( w:Number, h:Number, upside_down:Boolean ):void {
35
36 var rad:Number = w/3;
37 if ( rad > ( w / 2 ) )
38 rad = w / 2;
39
40 this.graphics.lineStyle(0, 0, 0);// this.outline_colour, 100);
41
42 var bgcolors:Array = GetColours(this.colour);
43 var bgalphas:Array = [1, 1];
44 var bgratios:Array = [0, 255];
45 var bgmatrix:Matrix = new Matrix();
46 var xRadius:Number;
47 var yRadius:Number;
48 var x:Number;
49 var y:Number;
50
51 bgmatrix.createGradientBox(w, h, (180 / 180) * Math.PI );
52 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
53
54 if (!upside_down && h > 0)
55 { /* draw bar upward */
56
57 if ( h >= w / 2)
58 { /* bar is tall enough for normal draw */
59
60 /* draw bottom half ellipse */
61 x = w/2;
62 y = h;
63 xRadius = w / 2;
64 yRadius = rad / 2;
65 halfEllipse(x, y, xRadius, yRadius, 100, false);
66
67 /* draw connecting rectangle */
68 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
69 this.graphics.moveTo(0, w/2);
70 this.graphics.lineTo(0, h);
71 this.graphics.lineTo(w, h);
72 this.graphics.lineTo(w, w / 2);
73
74 /* draw top ellipse */
75 //this.graphics.beginFill(this.colour, 1);
76 x = w / 2;
77 y = w / 2;
78 xRadius = w / 2;
79 yRadius = xRadius;
80 halfEllipse(x, y, xRadius, yRadius, 100, true);
81
82 }
83
84 else
85
86 { /* bar is too short for normal draw */
87
88 /* draw bottom half ellipse */
89 x = w/2;
90 y = h;
91 xRadius = w / 2;
92 yRadius = rad / 2;
93 halfEllipse(x, y, xRadius, yRadius, 100, false);
94
95 /* draw top ellipse */
96 x = w / 2;
97 y = h;
98 xRadius = w / 2;
99 yRadius = h;
100 halfEllipse(x, y, xRadius, yRadius, 100, true);
101
102 }
103 }
104
105 else
106
107 { /*draw bar downward */
108
109 if ( h >= w / 2)
110 { /* bar is tall enough for normal draw */
111
112 /* draw top half ellipse */
113 x = w/2;
114 y = 0;
115 xRadius = w / 2;
116 yRadius = rad / 2;
117 halfEllipse(x, y, xRadius, yRadius, 100, true);
118
119 /* draw connecting rectangle */
120 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
121 this.graphics.moveTo(0, 0);
122 this.graphics.lineTo(0, h - w / 2);
123 this.graphics.lineTo(w, h - w / 2);
124 this.graphics.lineTo(w, 0);
125
126 /* draw bottom ellipse */
127 //this.graphics.beginFill(this.colour, 1);
128 x = w / 2;
129 y = h - w / 2;
130 xRadius = w / 2;
131 yRadius = xRadius;
132 halfEllipse(x, y, xRadius, yRadius, 100, false);
133
134 }
135
136 else
137
138 { /* bar is too short for normal draw */
139
140 if (h > 0)
141
142 { /* bar greater than zero */
143
144 /* draw top half ellipse */
145 x = w/2;
146 y = 0;
147 xRadius = w / 2;
148 yRadius = rad / 2;
149 halfEllipse(x, y, xRadius, yRadius, 100, true);
150
151 /* draw bottom ellipse */
152 x = w / 2;
153 y = 0;
154 xRadius = w / 2;
155 yRadius = h;
156 halfEllipse(x, y, xRadius, yRadius, 100, false);
157
158 }
159
160 else
161
162 { /* bar is zero */
163
164 /* draw top ellipse */
165 x = w/2;
166 y = h;
167 xRadius = w / 2;
168 yRadius = rad / 4;
169 Ellipse(x, y, xRadius, yRadius, 100);
170
171 }
172
173 }
174
175 }
176
177 this.graphics.endFill();
178 }
179
180 private function glass( w:Number, h:Number, upside_down:Boolean ): void {
181
182 /* if this section is commented out, the white shine overlay will not be drawn */
183
184 this.graphics.lineStyle(0, 0, 0);
185 var bgcolors:Array = GetColours(this.colour);
186 var bgalphas:Array = [1, 1];
187 var bgratios:Array = [0, 255];
188 var bgmatrix:Matrix = new Matrix();
189
190 bgmatrix.createGradientBox(w, h, (180 / 180) * Math.PI );
191
192 /* set gradient fill */
193 var colors:Array = [0xFFFFFF, 0xFFFFFF];
194 var alphas:Array = [0, 0.75];
195 var ratios:Array = [100,255];
196 var xRadius:Number;
197 var yRadius:Number;
198 var x:Number;
199 var y:Number;
200 var matrix:Matrix = new Matrix();
201 matrix.createGradientBox(width, height, (180 / 180) * Math.PI );
202 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
203 var rad:Number = w / 3;
204
205 if (!upside_down && h > 0)
206 { /* draw shine upward */
207
208 if (h >= w / 2)
209 { /* bar is tall enough for normal draw */
210
211 /* draw bottom half ellipse shine */
212 x = w/2;
213 y = h;
214 xRadius = w / 2 - (0.05 * w);
215 yRadius = rad / 2 - (0.05 * w);
216 halfEllipse(x, y, xRadius, yRadius, 100, false);
217
218 /*draw connecting rectangle shine */
219 this.graphics.moveTo(0 + (0.05 * w), w/2);
220 this.graphics.lineTo(0 + (0.05 * w), h);
221 this.graphics.lineTo(w - (0.05 * w), h);
222 this.graphics.lineTo(w - (0.05 * w), w/2);
223
224
225 /* redraw top ellipse (to overwrite connecting rectangle shine overlap)*/
226 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
227 x = w / 2;
228 y = w / 2;
229 xRadius = w / 2;
230 yRadius = xRadius;
231 halfEllipse(x, y, xRadius, yRadius, 100, true);
232
233 /* draw top ellipse shine */
234 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
235 x = w / 3;
236 y = w / 2;
237 xRadius = w / 3 - (0.05 * w);
238 yRadius = xRadius + (0.05 * w);
239 halfEllipse(x, y, xRadius, yRadius, 100, true);
240
241 }
242
243 else
244
245 { /* bar is not tall enough for normal draw */
246
247 /* draw bottom half ellipse shine */
248 x = w/2;
249 y = h;
250 xRadius = w / 2 - (0.05 * w);
251 yRadius = rad / 2 - (0.05 * w);
252 halfEllipse(x, y, xRadius, yRadius, 100, false);
253
254 /* draw top ellipse shine */
255 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
256 x = w / 3;
257 y = h;
258 xRadius = w / 3 - (0.05 * w);
259 yRadius = h - 2.5*(0.05 * w);
260 halfEllipse(x, y, xRadius, yRadius, 100, true);
261
262 }
263
264 }
265
266 else
267
268 { /* draw shine downward */
269
270 if ( h >= w / 2)
271 { /* bar is tall enough for normal draw */
272
273 /*draw connecting rectangle shine */
274 this.graphics.moveTo(0 + (0.05 * w), 0);
275 this.graphics.lineTo(0 + (0.05 * w), h - w / 2);
276 this.graphics.lineTo(w - (0.05 * w), h - w / 2);
277 this.graphics.lineTo(w - (0.05 * w), 0);
278
279
280 /* redraw bottom ellipse (to overwrite connecting rectangle shine overlap)*/
281 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
282 x = w / 2;
283 y = h - w / 2;
284 xRadius = w / 2;
285 yRadius = xRadius;
286 halfEllipse(x, y, xRadius, yRadius, 100, false);
287
288 /* draw bottom ellipse shine */
289 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
290 x = w / 3;
291 y = h - w / 2;
292 xRadius = w / 3 - (0.05 * w);
293 yRadius = xRadius + (0.05 * w);
294 halfEllipse(x, y, xRadius, yRadius, 100, false);
295
296 /* redraw top half ellipse (to overwrite connecting rectangle shine overlap */
297 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
298 x = w/2;
299 y = 0;
300 xRadius = w / 2;
301 yRadius = rad / 2;
302 Ellipse(x, y, xRadius, yRadius, 100);
303
304 /* draw top half ellipse shine */
305 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, [0.1, 0.7], [0,255], matrix, 'pad'/*SpreadMethod.PAD*/ );
306 x = w/2;
307 y = 0;
308 xRadius = w / 2 - (0.05 * w);
309 yRadius = rad / 2 - (0.05 * w);
310 Ellipse(x, y, xRadius, yRadius, 100);
311
312 }
313
314 else
315
316 { /* bar is not tall enough for normal draw */
317
318 if (h > 0)
319 { /* bar is greater than zero */
320
321 /* draw bottom ellipse shine */
322 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
323 x = w / 3;
324 y = 0;
325 xRadius = w / 3 - (0.05 * w);
326 yRadius = h - 2.5*(0.05 * w);
327 halfEllipse(x, y, xRadius, yRadius, 100, false);
328
329 /* redraw top half ellipse (to overwrite connecting rectangle shine overlap */
330 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
331 x = w/2;
332 y = 0;
333 xRadius = w / 2;
334 yRadius = rad / 2;
335 Ellipse(x, y, xRadius, yRadius, 100);
336
337 /* draw top half ellipse shine */
338 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, [0.1, 0.67], [0,255], matrix, 'pad'/*SpreadMethod.PAD*/ );
339 x = w/2;
340 y = 0;
341 xRadius = w / 2 - (0.05 * w);
342 yRadius = rad / 2 - (0.05 * w);
343 Ellipse(x, y, xRadius, yRadius, 100);
344
345 }
346
347 else
348
349 { /* bar is zero */
350
351 /* redraw top half ellipse (to overwrite connecting rectangle shine overlap */
352 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
353 x = w/2;
354 y = 0;
355 xRadius = w / 2;
356 yRadius = rad / 2;
357 Ellipse(x, y, xRadius, yRadius, 100);
358
359 /* draw top half ellipse shine */
360 this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, [0.1, 0.7], [0,255], matrix, 'pad'/*SpreadMethod.PAD*/ );
361 x = w/2;
362 y = 0;
363 xRadius = w / 2 - (0.05 * w);
364 yRadius = rad / 2 - (0.05 * w);
365 Ellipse(x, y, xRadius, yRadius, 100);
366
367 }
368
369 }
370
371 }
372
373 this.graphics.endFill();
374 }
375
376 /* function to process colors */
377 /* returns a base color and a highlight color for the gradients based on the color passed in */
378 public static function GetColours( col:Number ):Array {
379 var rgb:Number = col; /* decimal value for color */
380 var red:Number = (rgb & 16711680) >> 16; /* extacts the red channel */
381 var green:Number = (rgb & 65280) >> 8; /* extacts the green channel */
382 var blue:Number = rgb & 255; /* extacts the blue channel */
383 var shift:Number = 2; /* shift factor */
384 var basecolor:Number = col; /* base color to be returned */
385 var highlight:Number = col; /* highlight color to be returned */
386 var bgred:Number = (rgb & 16711680) >> 16; /* red channel for highlight */
387 var bggreen:Number = (rgb & 65280) >> 8; /* green channel for highlight */
388 var bgblue:Number = rgb & 255; /* blue channel for highlight */
389 var hired:Number = (rgb & 16711680) >> 16; /* red channel for highlight */
390 var higreen:Number = (rgb & 65280) >> 8; /* green channel for highlight */
391 var hiblue:Number = rgb & 255; /* blue channel for highlight */
392
393 /* set base color components based on ability to shift lighter */
394 if (red + red / shift > 255 || green + green / shift > 255 || blue + blue / shift > 255)
395 {
396 bgred = red - red / shift;
397 bggreen = green - green / shift;
398 bgblue = blue - blue / shift;
399 }
400
401 /* set highlight components based on base colors */
402 hired = bgred + red / shift;
403 hiblue = bgblue + blue / shift;
404 higreen = bggreen + green / shift;
405
406 /* reconstruct base and highlight */
407 basecolor = bgred << 16 | bggreen << 8 | bgblue;
408 highlight = hired << 16 | higreen << 8 | hiblue;
409
410 /* return base and highlight */
411 return [highlight, basecolor];
412 }
413
414 /* ellipse cos helper function */
415 public static function magicTrigFunctionX (pointRatio:Number):Number{
416 return Math.cos(pointRatio*2*Math.PI);
417 }
418
419 /* ellipse sin helper function */
420 public static function magicTrigFunctionY (pointRatio:Number):Number{
421 return Math.sin(pointRatio*2*Math.PI);
422 }
423
424 /* ellipse function */
425 /* draws an ellipse from passed center coordinates, x and y radii, and number of sides */
426 public function Ellipse(centerX:Number, centerY:Number, xRadius:Number, yRadius:Number, sides:Number):Number{
427
428 /* move to first point on ellipse */
429 this.graphics.moveTo(centerX + xRadius, centerY);
430
431 /* loop through sides and draw curves */
432 for(var i:Number=0; i<=sides; i++){
433 var pointRatio:Number = i/sides;
434 var xSteps:Number = magicTrigFunctionX(pointRatio);
435 var ySteps:Number = magicTrigFunctionY(pointRatio);
436 var pointX:Number = centerX + xSteps * xRadius;
437 var pointY:Number = centerY + ySteps * yRadius;
438 this.graphics.lineTo(pointX, pointY);
439 }
440
441 /* return 1 */
442 return 1;
443 }
444
445 /* half ellipse function */
446 /* draws half of an ellipse from passed center coordinates, x and y radii, number of sides , and top/bottom */
447 public function halfEllipse(centerX:Number, centerY:Number, xRadius:Number, yRadius:Number, sides:Number, top:Boolean):Number{
448
449 var loopStart:Number;
450 var loopEnd:Number;
451
452 if (top == true)
453 {
454 loopStart = sides / 2;
455 loopEnd = sides;
456 }
457 else
458 {
459 loopStart = 0;
460 loopEnd = sides / 2;
461 }
462
463 /* move to first point on ellipse */
464 this.graphics.moveTo(centerX + xRadius, centerY);
465
466 /* loop through sides and draw curves */
467 for(var i:Number=loopStart; i<=loopEnd; i++){
468 var pointRatio:Number = i/sides;
469 var xSteps:Number = magicTrigFunctionX(pointRatio);
470 var ySteps:Number = magicTrigFunctionY(pointRatio);
471 var pointX:Number = centerX + xSteps * xRadius;
472 var pointY:Number = centerY + ySteps * yRadius;
473 this.graphics.lineTo(pointX, pointY);
474 }
475
476 /* return 1 */
477 return 1;
478 }
479
480 }
481 }
Note: See TracBrowser for help on using the repository browser.