/**
 * example application of Zachary Forest Johnson's CoxcombChart class for ActionScript 3
 * 
 * shows the popularity of car colors 2003 - 2006
 * 
 * shows percentage of cars manufactured of each color
 * 
 * Source: DuPont Herberts Automotive Systems, Troy, Mich. 2006 DuPont Automotive Color Popularity Survey Results. Web: www.automotive.dupont.com
 * 
 */
package
{
    import com.indiemaps.charting.coxcomb.CoxcombChart;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    [SWF(backgroundColor='#ffffff', width='800', height='400')]
    public class CarColors extends Sprite
    {
        //need to embed a font so that text can be rotated
        [Embed(systemFont="Helvetica", fontName="Helvetica", mimeType='application/x-font')]
         private static var _ignoreMe:String;
        [Embed(systemFont="Helvetica", fontName="Helvetica", fontWeight="bold", mimeType='application/x-font')]
         private static var _ignoreMe2:String;
        //data array
        private var dataset2003:Array = new Array(
            { realLabel : "white", label : "", suvTruck : 22, luxury : 31 },
            { realLabel : "black", label : "", suvTruck : 12, luxury : 11 },
            { realLabel : "silver", label : "silver", suvTruck : 17, luxury : 41 },
            { realLabel : "gray", label : "", suvTruck : 9, luxury : 0 },
            { realLabel : "blue", label : "", suvTruck : 9, luxury : 4 },
            { realLabel : "red", label : "", suvTruck : 16, luxury : 4 },
            { realLabel : "yellow", label : "", suvTruck : 0, luxury : 4 },
            { realLabel : "green", label : "", suvTruck : 7, luxury : 0 },
            { realLabel : "brown", label : "", suvTruck : 6, luxury : 1 },
            { realLabel : "other", label : "other", suvTruck : 2, luxury : 4 }
        );
        private var dataset2004:Array = new Array(
            { label : "white", suvTruck : 20, luxury : 29 },
            { label : "black", suvTruck : 12, luxury : 12 },
            { label : "silver", suvTruck : 16, luxury : 26 },
            { label : "gray", suvTruck : 10, luxury : 0 },
            { label : "blue", suvTruck : 12, luxury : 9 },
            { label : "red", suvTruck : 14, luxury : 9 },
            { label : "yellow", suvTruck : 1, luxury : 4 },
            { label : "green", suvTruck : 5, luxury : 0 },
            { label : "brown", suvTruck : 9, luxury : 11 },
            { label : "other", suvTruck : 1, luxury : 1 }
        );
        private var dataset2005:Array = new Array(
            { label : "white", suvTruck : 21, luxury : 18 },
            { label : "black", suvTruck : 10, luxury : 16 },
            { label : "silver", suvTruck : 16, luxury : 20 },
            { label : "gray", suvTruck : 17, luxury : 5 },
            { label : "blue", suvTruck : 12, luxury : 13 },
            { label : "red", suvTruck : 11, luxury : 7 },
            { label : "yellow", suvTruck : 1, luxury : 6 },
            { label : "green", suvTruck : 4, luxury : 5 },
            { label : "brown", suvTruck : 7, luxury : 10 },
            { label : "other", suvTruck : 1, luxury : 1 }
        );
        private var dataset2006:Array = new Array(
            { label : "white", suvTruck : 26, luxury : 25 },
            { label : "black", suvTruck : 13, luxury : 22 },
            { label : "silver", suvTruck : 16, luxury : 16 },
            { label : "gray", suvTruck : 13, luxury : 12 },
            { label : "blue", suvTruck : 10, luxury : 7 },
            { label : "red", suvTruck : 11, luxury : 7 },
            { label : "yellow", suvTruck : 1, luxury : 6 },
            { label : "green", suvTruck : 4, luxury : 3 },
            { label : "brown", suvTruck : 6, luxury : 2 },
            { label : "other", suvTruck : 1, luxury : 1 }
        );
        //coxcomb container
        private var coxcombsContainer:Sprite = new Sprite();
        private var annotations:Sprite = new Sprite();
        public function CarColors()
        {
            super();
            addChild(coxcombsContainer);
            coxcombsContainer.x = annotations.x = 180;
            // some nasty annotations __ best not to look here
            addChild(annotations);
            var luxuryLabel:TextField = new TextField();
                luxuryLabel.defaultTextFormat = new TextFormat("Helvetica",14,0xEC087D,true);
                luxuryLabel.text = "Luxury cars";
            var suvTruckLabel:TextField = new TextField();
                suvTruckLabel.defaultTextFormat = new TextFormat("Helvetica",14,0xEC087D,true);
                suvTruckLabel.text = "SUV/Trucks";
            annotations.addChild(luxuryLabel); annotations.addChild(suvTruckLabel);
            luxuryLabel.autoSize = "right";
            suvTruckLabel.autoSize = "right";
            luxuryLabel.y = 100 - .5 * luxuryLabel.textHeight;
            suvTruckLabel.y = 300 - .5 * suvTruckLabel.textHeight;
            luxuryLabel.x = suvTruckLabel.x = -150;
            createCoxcombs();
        }
        private function createCoxcombs():void {
            var max:Number = getMax([dataset2003, dataset2004, dataset2005, dataset2006], ['suvTruck', 'luxury']);
            var colorsArray:Array = [0xffffff, 0x000000, 0xdddddd, 0xaaaaaa, 0x0000ff, 0xff0000, 0xffff00, 0x00ff00, 0x884400, 0xffffff];
            var strokeColorsArray:Array = [0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xcccccc, 0xEC087D];
            for (var year:int=2003; year<2007; year++) {
                var luxuryChart:CoxcombChart = new CoxcombChart(this['dataset'+year],200,colorsArray,strokeColorsArray,0,max,false,"label","luxury");
                var suvTruckChart:CoxcombChart = new CoxcombChart(this['dataset'+year],200,colorsArray,strokeColorsArray,0,max,false,"label","suvTruck");
                if (year == 2003) {
                    suvTruckChart.showLabels("vertical", { font : "Helvetica", color : 0x333333 });
                }
                luxuryChart.x = suvTruckChart.x = (year-2003) * 160;
                luxuryChart.y = 100; suvTruckChart.y = 300;
                coxcombsContainer.addChild(luxuryChart); coxcombsContainer.addChild(suvTruckChart);
                //add the label
                var yearLabel:TextField = new TextField();
                    yearLabel.defaultTextFormat = new TextFormat("Helvetica",16,0xcccccc,false);
                    yearLabel.text = String(year);
                yearLabel.autoSize = "left";
                annotations.addChild(yearLabel);
                yearLabel.x = luxuryChart.x - .5 * yearLabel.width;
                yearLabel.y = .5 * (suvTruckChart.y - luxuryChart.y) + luxuryChart.y - .5 * yearLabel.height - 5;
            }
        }
        private function getMax(arrays:Array, props:Array):Number {
            var max:Number = 0;
            for each (var array:Array in arrays) {
                for each (var obj:Object in array) {
                    for each (var prop:Object in props) {
                        if (obj[prop] > max) max = obj[prop];
                    }
                }
            }
            return max;
        }
    }
}