| 1 | package charts.series.bars {
|
|---|
| 2 |
|
|---|
| 3 | import charts.series.Element;
|
|---|
| 4 | import flash.display.Sprite;
|
|---|
| 5 | import flash.geom.Point;
|
|---|
| 6 | import com.serialization.json.JSON;
|
|---|
| 7 | import string.Utils;
|
|---|
| 8 | import elements.axis.XAxisLabels;
|
|---|
| 9 |
|
|---|
| 10 | public class StackCollection extends Element {
|
|---|
| 11 |
|
|---|
| 12 | protected var tip_pos:flash.geom.Point;
|
|---|
| 13 | private var vals:Array;
|
|---|
| 14 | public var colours:Array;
|
|---|
| 15 | protected var group:Number;
|
|---|
| 16 | private var total:Number;
|
|---|
| 17 |
|
|---|
| 18 | public function StackCollection( index:Number, props:Properties, group:Number ) {
|
|---|
| 19 |
|
|---|
| 20 | //this.tooltip = this.replace_magic_values( props.get('tip') );
|
|---|
| 21 | this.tooltip = props.get('tip');
|
|---|
| 22 |
|
|---|
| 23 | // this is very similar to a normal
|
|---|
| 24 | // PointBarBase but without the mouse
|
|---|
| 25 | // over and mouse out events
|
|---|
| 26 | this.index = index;
|
|---|
| 27 |
|
|---|
| 28 | var item:Object;
|
|---|
| 29 |
|
|---|
| 30 | // a stacked bar has n Y values
|
|---|
| 31 | // so this is an array of objects
|
|---|
| 32 | this.vals = props.get('values') as Array;
|
|---|
| 33 |
|
|---|
| 34 | this.total = 0;
|
|---|
| 35 | for each( item in this.vals ) {
|
|---|
| 36 | if( item != null ) {
|
|---|
| 37 | if( item is Number )
|
|---|
| 38 | this.total += item;
|
|---|
| 39 | else
|
|---|
| 40 | this.total += item.val;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | //
|
|---|
| 45 | // parse our HEX colour strings
|
|---|
| 46 | //
|
|---|
| 47 | this.colours = new Array();
|
|---|
| 48 | for each( var colour:String in props.get('colours') )
|
|---|
| 49 | this.colours.push( string.Utils.get_colour( colour ) );
|
|---|
| 50 |
|
|---|
| 51 | this.group = group;
|
|---|
| 52 | this.visible = true;
|
|---|
| 53 |
|
|---|
| 54 | var prop:String;
|
|---|
| 55 |
|
|---|
| 56 | var n:Number; // <-- ugh, leaky variables.
|
|---|
| 57 | var bottom:Number = 0;
|
|---|
| 58 | var top:Number = 0;
|
|---|
| 59 | var colr:Number;
|
|---|
| 60 | var count:Number = 0;
|
|---|
| 61 |
|
|---|
| 62 | for each( item in this.vals )
|
|---|
| 63 | {
|
|---|
| 64 | // is this a null stacked bar group?
|
|---|
| 65 | if( item != null )
|
|---|
| 66 | {
|
|---|
| 67 | colr = this.colours[(count % this.colours.length)]
|
|---|
| 68 |
|
|---|
| 69 | // override bottom, colour and total, leave tooltip, on-click, on-show etc..
|
|---|
| 70 | var defaul_stack_props:Properties = new Properties({
|
|---|
| 71 | bottom: bottom,
|
|---|
| 72 | colour: colr, // <-- colour from list (may be overriden later)
|
|---|
| 73 | total: this.total
|
|---|
| 74 | }, props);
|
|---|
| 75 |
|
|---|
| 76 | //
|
|---|
| 77 | // a valid item is one of [ Number, Object, null ]
|
|---|
| 78 | //
|
|---|
| 79 | if ( item is Number ) {
|
|---|
| 80 | item = { val: item };
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | if ( item == null ) {
|
|---|
| 84 | item = { val: null };
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | // MERGE:
|
|---|
| 88 | top += item.val;
|
|---|
| 89 | item.top = top;
|
|---|
| 90 | // now override on-click, on-show, colour etc...
|
|---|
| 91 | var stack_props:Properties = new Properties(item, defaul_stack_props);
|
|---|
| 92 |
|
|---|
| 93 | var p:Stack = new Stack( index, stack_props, group );
|
|---|
| 94 | this.addChild( p );
|
|---|
| 95 |
|
|---|
| 96 | bottom = top;
|
|---|
| 97 | count++;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | public override function resize( sc:ScreenCoordsBase ):void {
|
|---|
| 104 |
|
|---|
| 105 | for ( var i:Number = 0; i < this.numChildren; i++ )
|
|---|
| 106 | {
|
|---|
| 107 | var e:Element = this.getChildAt(i) as Element;
|
|---|
| 108 | e.resize( sc );
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | //
|
|---|
| 113 | // for tooltip closest - return the middle point
|
|---|
| 114 | // of this stack
|
|---|
| 115 | //
|
|---|
| 116 | public override function get_mid_point():flash.geom.Point {
|
|---|
| 117 |
|
|---|
| 118 | // get the first bar in the stack
|
|---|
| 119 | var e:Element = this.getChildAt(0) as Element;
|
|---|
| 120 |
|
|---|
| 121 | return e.get_mid_point();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | //
|
|---|
| 125 | // called by get_all_at_this_x_pos
|
|---|
| 126 | //
|
|---|
| 127 | public function get_children(): Array {
|
|---|
| 128 |
|
|---|
| 129 | var tmp:Array = [];
|
|---|
| 130 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
|---|
| 131 | tmp.push( this.getChildAt(i) );
|
|---|
| 132 | }
|
|---|
| 133 | return tmp;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | public override function get_tip_pos():Object {
|
|---|
| 137 | //
|
|---|
| 138 | // get top item in stack
|
|---|
| 139 | //
|
|---|
| 140 | var e:Element = this.getChildAt(this.numChildren-1) as Element;
|
|---|
| 141 | return e.get_tip_pos();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | public override function get_tooltip():String {
|
|---|
| 146 | //
|
|---|
| 147 | // is the mouse over one of the bars in this stack?
|
|---|
| 148 | //
|
|---|
| 149 |
|
|---|
| 150 | // tr.ace( this.numChildren );
|
|---|
| 151 | for ( var i:Number = 0; i < this.numChildren; i++ )
|
|---|
| 152 | {
|
|---|
| 153 | var e:Element = this.getChildAt(i) as Element;
|
|---|
| 154 | if ( e.is_tip )
|
|---|
| 155 | {
|
|---|
| 156 | //tr.ace( 'TIP' );
|
|---|
| 157 | return e.get_tooltip();
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | //
|
|---|
| 161 | // the mouse is *near* our stack, so show the 'total' tooltip
|
|---|
| 162 | //
|
|---|
| 163 | return this.tooltip;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * See Element
|
|---|
| 168 | */
|
|---|
| 169 | public override function tooltip_replace_labels( labels:XAxisLabels ):void {
|
|---|
| 170 |
|
|---|
| 171 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
|---|
| 172 | var e:Stack = this.getChildAt(i) as Stack;
|
|---|
| 173 | e.replace_x_axis_label( labels.get( this.index ) );
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|