1 | package charts.series.bars {
|
---|
2 |
|
---|
3 | import flash.display.Sprite;
|
---|
4 | import flash.geom.Point;
|
---|
5 | import charts.series.bars.Base;
|
---|
6 |
|
---|
7 | public class ECandle extends Base {
|
---|
8 | protected var high:Number;
|
---|
9 | protected var low:Number;
|
---|
10 | protected var negative_colour:Number;
|
---|
11 |
|
---|
12 |
|
---|
13 | public function ECandle( index:Number, props:Properties, group:Number ) {
|
---|
14 |
|
---|
15 | super(index, props, group);
|
---|
16 |
|
---|
17 | tr.aces( props.has('negative-colour'), props.get_colour('negative-colour'));
|
---|
18 |
|
---|
19 | if( props.has('negative-colour') )
|
---|
20 | this.negative_colour = props.get_colour('negative-colour');
|
---|
21 | else
|
---|
22 | this.negative_colour = this.colour;
|
---|
23 | }
|
---|
24 |
|
---|
25 | //
|
---|
26 | // a candle chart has many values used to display each point
|
---|
27 | //
|
---|
28 | protected override function parse_value( props:Properties ):void {
|
---|
29 |
|
---|
30 | // set top (open) and bottom (close)
|
---|
31 | super.parse_value( props );
|
---|
32 | this.high = props.get('high');
|
---|
33 | this.low = props.get('low');
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected override function replace_magic_values( t:String ): String {
|
---|
37 |
|
---|
38 | t = super.replace_magic_values( t );
|
---|
39 | t = t.replace('#high#', NumberUtils.formatNumber( this.high ));
|
---|
40 | t = t.replace('#open#', NumberUtils.formatNumber( this.top ));
|
---|
41 | t = t.replace('#close#', NumberUtils.formatNumber( this.bottom ));
|
---|
42 | t = t.replace('#low#', NumberUtils.formatNumber( this.low ));
|
---|
43 |
|
---|
44 | return t;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override function resize( sc:ScreenCoordsBase ):void {
|
---|
48 |
|
---|
49 | // this moves everyting relative to the box (NOT the whiskers)
|
---|
50 | var h:Object = this.resize_helper( sc as ScreenCoords );
|
---|
51 |
|
---|
52 | //
|
---|
53 | //var bar_high:Number = 0;
|
---|
54 | //var bar_low:Number = height;
|
---|
55 |
|
---|
56 | // calculate the box position:
|
---|
57 | var tmp:Number = sc.get_y_from_val(Math.max(this.top, this.bottom), this.right_axis);
|
---|
58 | var bar_high:Number = sc.get_y_from_val(this.high, this.right_axis) - tmp;
|
---|
59 | var bar_top:Number = 0;
|
---|
60 | var bar_bottom:Number = sc.get_y_from_val(this.bottom, this.right_axis) - tmp;
|
---|
61 | var bar_low:Number = sc.get_y_from_val(this.low, this.right_axis) - tmp;
|
---|
62 |
|
---|
63 | //var height:Number = Math.abs( bar_bottom - bar_top );
|
---|
64 |
|
---|
65 | //
|
---|
66 | // move the Sprite to the correct screen location:
|
---|
67 | //
|
---|
68 | //this.y = bar_high;
|
---|
69 | //this.x = tmp.x;
|
---|
70 |
|
---|
71 | //
|
---|
72 | // tell the tooltip where to show its self
|
---|
73 | //
|
---|
74 | this.tip_pos = new flash.geom.Point( this.x + (h.width / 2), this.y );
|
---|
75 |
|
---|
76 | var mid:Number = h.width / 2;
|
---|
77 | this.graphics.clear();
|
---|
78 | var c:Number = this.colour;
|
---|
79 | if ( h.upside_down)
|
---|
80 | c = this.negative_colour;
|
---|
81 |
|
---|
82 | this.top_line(c, mid, bar_high);
|
---|
83 |
|
---|
84 | if ( this.top == this.bottom )
|
---|
85 | this.draw_doji(c, h.width, bar_top);
|
---|
86 | else
|
---|
87 | this.draw_box(c, bar_top, h.height, h.width, h.upside_down);
|
---|
88 |
|
---|
89 | this.bottom_line(c, mid, h.height, bar_low);
|
---|
90 | // top line
|
---|
91 |
|
---|
92 | //
|
---|
93 | // tell the tooltip where to show its self
|
---|
94 | //
|
---|
95 | this.tip_pos = new flash.geom.Point(
|
---|
96 | this.x + (h.width / 2),
|
---|
97 | this.y + bar_high );
|
---|
98 | }
|
---|
99 |
|
---|
100 | private function top_line(colour:Number, mid:Number, height:Number): void {
|
---|
101 | // top line
|
---|
102 | this.graphics.beginFill( colour, 1.0 );
|
---|
103 | this.graphics.moveTo( mid-1, 0 );
|
---|
104 | this.graphics.lineTo( mid+1, 0 );
|
---|
105 | this.graphics.lineTo( mid+1, height );
|
---|
106 | this.graphics.lineTo( mid-1, height );
|
---|
107 | this.graphics.endFill();
|
---|
108 | }
|
---|
109 |
|
---|
110 | private function bottom_line(colour:Number, mid:Number, top:Number, bottom:Number):void {
|
---|
111 | this.graphics.beginFill( colour, 1.0 );
|
---|
112 | this.graphics.moveTo( mid-1, top );
|
---|
113 | this.graphics.lineTo( mid+1, top );
|
---|
114 | this.graphics.lineTo( mid+1, bottom );
|
---|
115 | this.graphics.lineTo( mid-1, bottom );
|
---|
116 | this.graphics.endFill();
|
---|
117 | }
|
---|
118 |
|
---|
119 | //
|
---|
120 | // http://en.wikipedia.org/wiki/Candlestick_chart
|
---|
121 | //
|
---|
122 | private function draw_doji(colour:Number, width:Number, pos:Number):void {
|
---|
123 | // box
|
---|
124 | this.graphics.beginFill( colour, 1.0 );
|
---|
125 | this.graphics.moveTo( 0, pos-1 );
|
---|
126 | this.graphics.lineTo( width, pos-1 );
|
---|
127 | this.graphics.lineTo( width, pos+1 );
|
---|
128 | this.graphics.lineTo( 0, pos+1 );
|
---|
129 | this.graphics.endFill();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | private function draw_box(colour:Number, top:Number, bottom:Number, width:Number, upside_down:Boolean):void {
|
---|
135 |
|
---|
136 | // box
|
---|
137 | this.graphics.beginFill( colour, 1.0 );
|
---|
138 | this.graphics.moveTo( 0, top );
|
---|
139 | this.graphics.lineTo( width, top );
|
---|
140 | this.graphics.lineTo( width, bottom );
|
---|
141 | this.graphics.lineTo( 0, bottom );
|
---|
142 | this.graphics.lineTo( 0, top );
|
---|
143 |
|
---|
144 | if ( upside_down) {
|
---|
145 | // snip out the middle of the box:
|
---|
146 | this.graphics.moveTo( 2, top+2 );
|
---|
147 | this.graphics.lineTo( width-2, top+2 );
|
---|
148 | this.graphics.lineTo( width-2, bottom-2 );
|
---|
149 | this.graphics.lineTo( 2, bottom-2 );
|
---|
150 | this.graphics.lineTo( 2, top+2 );
|
---|
151 | }
|
---|
152 | this.graphics.endFill();
|
---|
153 |
|
---|
154 | if ( upside_down ) {
|
---|
155 |
|
---|
156 | //
|
---|
157 | // HACK: we fill an invisible rect over
|
---|
158 | // the hollow rect so the mouse over
|
---|
159 | // event fires correctly (even when the
|
---|
160 | // mouse is in the hollow part)
|
---|
161 | //
|
---|
162 | this.graphics.lineStyle( 0, 0, 0 );
|
---|
163 | this.graphics.beginFill(0, 0);
|
---|
164 | this.graphics.moveTo( 2, top-2 );
|
---|
165 | this.graphics.lineTo( width-2, top-2 );
|
---|
166 | this.graphics.lineTo( width-2, bottom-2 );
|
---|
167 | this.graphics.lineTo( 2, bottom-2 );
|
---|
168 | this.graphics.endFill();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 | }
|
---|