/**
 * example application of Coxcomb charting classes
 * 
 * U.S. soldier deaths in Iraq, March 2003 - October 2008, by month
 * data are from icasualties.org (retrieved October 13, 2008)
 * 
 * by Zachary Forest Johnson
 * indiemaps.com/blog
 * 
 */

package
{
    import com.indiemaps.charting.coxcomb.CoxcombChart;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.*;
    import flash.text.TextField;
    import flash.text.TextFormat;
    
    [SWF(backgroundColor='#ffffff', width="900", height="200")]
    public class IraqDeathCountCoxcombs 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;
         
         
        private var coxcombsContainer:Sprite = new Sprite();
        private var datasets:Array = new Array(); // array of arrays of objects
        
        public function IraqDeathCountCoxcombs()
        {
            loadDataFile();
            
            addChild(coxcombsContainer);
            coxcombsContainer.x = 75;
            coxcombsContainer.y = 100;
        }
        
        private function loadDataFile():void {
            var fileName:String = "iraqWarDeaths.csv";
            var dataLoader:URLLoader = new URLLoader(new URLRequest(fileName));
            dataLoader.addEventListener(Event.COMPLETE, processData); 

        }
        
        private function processData(e:Event):void {
            var pastHeader:Boolean = false;
            var lines:Array = e.target.data.split("\r");             
            for each (var line:String in lines) {
                if (!pastHeader) {
                    pastHeader = true;
                    continue;
                }
                var lineArray:Array = line.split(',');
                var year:int = int((lineArray[0] as String).substr(4,2)) + 2000;
                var month:String = (lineArray[0] as String).substr(0,1);
                if (!datasets[year - 2003]) datasets[year - 2003] = new Array();
                datasets[year - 2003].push(
                    {
                        label : month,
                        value : Number(lineArray[1]),
                        year : year
                    }
                );
            }
            onDataProcessed();
        }
        
        private function onDataProcessed():void {
            createCoxcombs();
        }
        
        private function createCoxcombs():void {
            //first, get max to fix each chart to
            var fixedMax:Number = 0;
            for each (var dataset:Array in datasets) {
                for each (var monthObj:Object in dataset) {
                    if (monthObj.value > fixedMax) fixedMax = monthObj.value;
                }
            }
            
            //now create the charts
            for each (var dataset:Array in datasets) {
                var toLabel:Boolean = (datasets.indexOf(dataset)==0);
                var yearChart:CoxcombChart = new CoxcombChart(dataset, 150, 0x000000, 0xffffff, 1, fixedMax, false, "label", "value", false, 15);
                if (toLabel)
                    yearChart.showLabels("horizontal", { font : "Helvetica", size : 8 , color : 0xdd0000 }, 999, 15);
                yearChart.x = datasets.indexOf(dataset) * 150;
                coxcombsContainer.addChild(yearChart);
                
                //add a label for the year
                var yearTextField:TextField = new TextField();
                yearTextField.defaultTextFormat = new TextFormat("Helvetica",12,0xbbbbbb,true);
                    yearTextField.text = String(Number(datasets.indexOf(dataset)) + 2003);
                    yearTextField.autoSize = "left";
                    yearTextField.y = 85; 
                    yearTextField.x = datasets.indexOf(dataset) * 150; 
                    yearTextField.x -= .5 * yearTextField.width;
                    
                    coxcombsContainer.addChild(yearTextField);
            }
        }

    }
}