/**
 * example application of Coxcomb charting classes
 * 
 * recreation of Florence Nightingale's famous coxcombs of British deaths during the Crimean War
 *         charts are only approximate, and data below are only accurate relative to one another
 * 
 *
 * 
 * by Zachary Forest Johnson
 * indiemaps.com/blog
 * 
 */
package
{
    import com.cartogrammar.drawing.DashedLine;
    import com.indiemaps.charting.coxcomb.CoxcombChart;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    [SWF(backgroundColor='#ffffff', width='1000', height='600')]
    public class NightingaleCoxcomb 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;
        [Embed(systemFont="Helvetica", fontName="Helvetica", fontStyle="italic", mimeType='application/x-font')]
         private static var _ignoreMe3:String;
        //data definition (since we have just 2 years of data, I'm just hardcoding it in)
        
        //first, April 1855 to March 1856 (total in blue, wounds+others in black, wounds in red)
        private var year1855:Array = new Array(
            { label : "APRIL 1855", total : 16.7, woundsPlusOthers : 0, wounds : 2 },
            { label : "MAY", total : 16.2, woundsPlusOthers : 1.55, wounds : 1.15 },
            { label : "JUNE", total : 23.4, woundsPlusOthers : 6.1, wounds : 1 },
            { label : "JULY", total : 10.35, woundsPlusOthers : 3.75, wounds : 1 },
            { label : "AUGUST", total : 12.6, woundsPlusOthers : 4.2, wounds : .75 },
            { label : "SEPTEMBER", total : 6.7, woundsPlusOthers : 2.9, wounds : .55 }, /* a weird one */
            { label : "OCTOBER", total : 3.35, woundsPlusOthers : 1.1, wounds : .5 },
            { label : "NOVEMBER", total : 5.9, woundsPlusOthers : 1.1, wounds : .5 }, /* a weird one */
            { label : "DECEMBER", total : 2.6, woundsPlusOthers : .9, wounds : .15 }, /* a weird one */
            { label : "JANUARY 1856", total : 1.35, woundsPlusOthers : 0, wounds : 1.35}, /* a weird one */
            { label : "FEBRUARY", total : .55, woundsPlusOthers : 0, wounds : .55 },
            { label : "MARCH", total : .9, woundsPlusOthers : 0, wounds : .45 }
        );
        //then, April 1854 to March 1855 (total in blue, wounds+others in red, wounds in black)
        private var year1854:Array = new Array(
            { label : "APRIL 1854", total : .7, woundsPlusOthers : .7, wounds : 0 },
            { label : "MAY", total : .55, woundsPlusOthers : .45, wounds : 0 },
            { label : "JUNE", total : .4, woundsPlusOthers : .25, wounds : 0 },
            { label : "JULY", total : 14.5, woundsPlusOthers : 1, wounds : 0 },
            { label : "AUGUST", total : 33, woundsPlusOthers : 1.2, wounds : 0 },
            { label : "SEPTEMBER", total : 31, woundsPlusOthers : 2.9, wounds : 2 },
            { label : "OCTOBER", total : 20, woundsPlusOthers : 5, wounds : 4.6 },
            { label : "NOVEMBER", total : 35, woundsPlusOthers : 11.5, wounds : 11.2 }, /* a weird one */
            { label : "DECEMBER", total : 62, woundsPlusOthers : 5, wounds : 4.1 },
            { label : "JANUARY 1855", total : 100, woundsPlusOthers : 11.8, wounds : 3 },
            { label : "FEBRUARY", total : 80, woundsPlusOthers : 13.8, wounds : 1.65 },
            { label : "MARCH 1855", total : 47, woundsPlusOthers : 6.9, wounds : 1.25 }
        );
        private var coxcombsContainer:Sprite = new Sprite();
        private var annotations:Sprite = new Sprite();
        public function NightingaleCoxcomb()
        {
            super();
            createCoxcombs();
            annotateCoxcombs();
            addChild(coxcombsContainer);
            coxcombsContainer.addChild(annotations);
            //errr some corrections
            annotations.x += 58;
            annotations.y -= 148;
            //scale it on up
            coxcombsContainer.scaleX = coxcombsContainer.scaleY = 1.75;
            coxcombsContainer.x = - coxcombsContainer.getBounds(this).x/coxcombsContainer.scaleX;
            coxcombsContainer.y = - coxcombsContainer.getBounds(this).y/coxcombsContainer.scaleY;
        }
        private function createCoxcombs():void {
            var max:Number = getMax();
            trace("max", max);
            var coxcomb1854_total:CoxcombChart = new CoxcombChart(year1854, 368, 0x4ab5e2, 0x4ab5e2, 0, max, false, "label", "total", false, 15 + 270);
            var coxcomb1854_woundedPlusOthers:CoxcombChart = new CoxcombChart(year1854, 368, 0x222222, 0x4ab5e2, 0, max, false, "label", "woundsPlusOthers", false, 15 + 270);
            var coxcomb1854_wounds:CoxcombChart = new CoxcombChart(year1854, 368, 0xEE000F, 0x4ab5e2, 0, max, false, "label", "wounds", false, 15 + 270);
            var coxcomb1855_total:CoxcombChart = new CoxcombChart(year1855, 368, 0x4ab5e2, 0x4ab5e2, 0, max, false, "label", "total", false, 15 + 270);
            var coxcomb1855_woundedPlusOthers:CoxcombChart = new CoxcombChart(year1855, 368, 0xEE000F, 0x4ab5e2, 0, max, false, "label", "woundsPlusOthers", false, 15 + 270);
            var coxcomb1855_wounds:CoxcombChart = new CoxcombChart(year1855, 368, 0x222222, 0x4ab5e2, 0, max, false, "label", "wounds", false, 15 + 270);
            coxcomb1854_total.showLabels("horizontal", { size : 5 , color : 0x222222, bold : true, font : "Helvetica" }, 999, 9);
            coxcomb1855_total.showLabels("horizontal", { size : 5 , color : 0x222222, bold : true, font : "Helvetica" }, 40, 9);
            coxcombsContainer.addChild(coxcomb1855_total);
            coxcombsContainer.addChild(coxcomb1855_woundedPlusOthers);
            coxcombsContainer.addChild(coxcomb1855_wounds);
            coxcombsContainer.addChild(coxcomb1854_total);
            coxcombsContainer.addChild(coxcomb1854_woundedPlusOthers);
            coxcombsContainer.addChild(coxcomb1854_wounds);
            trace(coxcombsContainer.width);
            coxcomb1854_total.x = coxcomb1854_woundedPlusOthers.x = coxcomb1854_wounds.x = 332;
        }
        private function getMax():Number {
            var max:Number = 0;
            for each (var month:Object in year1854) {
                if (month.total > max) max = month.total;
            }
            for each (var month:Object in year1855) {
                if (month.total > max) max = month.total;
            }
            return max;
        }
        private function annotateCoxcombs():void {
            //alright, here's where it just gets tricky, best not to look around here
            
            //first I'll do the dashed line from March 1855 to April 1855
            
            //I'm using Andy Woodruff's DashedLine class
            var myDashy:DashedLine = new DashedLine(1,0x222222,new Array(2,2,2,2));
            myDashy.moveTo(-75,0);
            myDashy.lineTo(63,62);
            myDashy.lineTo(205,0);
            coxcombsContainer.addChild(myDashy);
            //okay, now the title and subtitles
            //i'm just sticking to Helvetica for all these, no fancy fonts
            
            var titleText:String = "DIAGRAM OF THE CAUSES OF MORTALITY";
            var dateText1:String = "APRIL 1855 TO MARCH 1856 .";
            var dateText2:String = "APRIL 1854 TO MARCH 1855 .";
            var subtitleText:String = "IN THE ARMY IN THE EAST .";
            var titleTextField:TextField = new TextField();
                applyTextFormat(titleTextField,titleText, { size : 11.5, color : 0x222222, font : "Helvetica" });
            var subtitleTextField:TextField = new TextField();
                applyTextFormat(subtitleTextField,subtitleText, { size : 9.5, color : 0x222222, font : "Helvetica" });
            var dateText1Field:TextField = new TextField();
                applyTextFormat(dateText1Field,dateText1, { size : 8, color : 0x222222, font : "Helvetica" });
            var dateText2Field:TextField = new TextField();
                applyTextFormat(dateText2Field,dateText2, { size : 8, color : 0x222222, font : "Helvetica" });
            annotations.addChild(titleTextField); annotations.addChild(subtitleTextField); annotations.addChild(dateText1Field); annotations.addChild(dateText2Field);
            titleTextField.x = 4; titleTextField.y = 15;
            subtitleTextField.x = 52; subtitleTextField.y = 30;
            dateText1Field.x = -127; dateText1Field.y = 35;
            dateText2Field.x = 260; dateText2Field.y = 35;
            //the lines
            var lines:Sprite = new Sprite();
            annotations.addChild(lines);
            lines.graphics.lineStyle(1, 0x444444);
            lines.graphics.moveTo(-113, 45);
                lines.graphics.lineTo(-48, 45);
            lines.graphics.moveTo(280, 45);
                lines.graphics.lineTo(340, 45);
            lines.graphics.moveTo(64, 46.5);
                lines.graphics.lineTo(174, 46.5);
            lines.graphics.moveTo(55, 48);
                lines.graphics.lineTo(183, 48);
            //the numbers
            var oneText:TextField = new TextField();
                applyTextFormat(oneText, '1 .', { size : 7, color : 0x444444, bold : true, font : "Helvetica" });
            var twoText:TextField = new TextField();
                applyTextFormat(twoText, '2 .', { size : 7, color : 0x444444, bold : true, font : "Helvetica" });
            annotations.addChild(oneText); annotations.addChild(twoText);
            twoText.x = -77; oneText.y = 25;
            oneText.x = 310; twoText.y = 25;
            //the actual chart annotations ("CRIMEA" & "BULGARIA")
            var crimeaText:TextField = new TextField();
                applyTextFormat(crimeaText, 'CRIMEA', { size : 5.5, color : 0x444444, bold : true, font : "Helvetica" });
            var bulgariaText:TextField = new TextField();
                applyTextFormat(bulgariaText, 'BULGARIA', { size : 5.5, color : 0x444444, bold : true, font : "Helvetica" });
            annotations.addChild(crimeaText); annotations.addChild(bulgariaText);
            crimeaText.x = 357; crimeaText.y = 149;
            bulgariaText.x = 265; bulgariaText.y = 123; bulgariaText.rotation = -90;
            //the explanatory text bottom left
            var s:Object = new Object();
            s.sentence1Text = "The Areas of the blue, red, & black wedges are each measured from\n       the centre as the common vertex";
            s.sentence2Text = "The blue wedges measured from the centre of the circle represent area\n       for are the deaths from Preventible or Mitigable Zymotic Diseases, the\n       red wedges measured from the centre the deaths from all other causes";
            s.sentence3Text = "The black line across the red triangle in Nov' 1854 marks the boundary\n       of the deaths from all other causes during the month";
            s.sentence4Text = "In October 1854, & April 1855, the black area coincides with the red,\n       in January & February 1856, the blue coincides with the black";
            s.sentence5Text = "The entire areas may be compared by following the blue, the red & the\n       black lines enclosing them.";
            var t:Object = new Object();
            t.sentence1 = new TextField();
            t.sentence2 = new TextField();
            t.sentence3 = new TextField();
            t.sentence4 = new TextField();
            t.sentence5 = new TextField();
            var bottomSentences:Sprite = new Sprite();
            annotations.addChild(bottomSentences);
            bottomSentences.x = -140;
            bottomSentences.y = 240;
            for (var i:int=1; i<6; i++) {                 
                applyTextFormat(t['sentence' + i], s['sentence' + i + 'Text'], { size : 7.5, color : 0x222222, font : "Helvetica", italic : true, leading : 2.5 });  
                t['sentence' + i].y = bottomSentences.height;
                bottomSentences.addChild(t['sentence' + i]);
            }
        }
        private function applyTextFormat(textField:TextField, text:String, specialProps:Object=null):void {
            textField.embedFonts = true;
            textField.autoSize = "left";
            var tf:TextFormat = new TextFormat();
            for (var prop:Object in specialProps) {
                trace(prop, specialProps[prop]);
                tf[prop] = specialProps[prop];
            }
            textField.defaultTextFormat = tf;
            textField.text = text;    
        }
    }
}