Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/MapMainDemo/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/MapMainDemo/Modules/ShapefileSupport.cs

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;

namespace DevExpress.XtraMap.Demos {

    public partial class ShapefileSupport : MapTutorialControl {
        const string PoliticalToolTipPattern = "{NAME}";
        const string GdpToolTipPattern = "{NAME}: ${GDP_MD_EST:#,0}M";
        const string PopulationToolTipPattern = "{NAME}: {POP_EST:#,##0,,}M";

        ChoroplethColorizer politicalColorizer;
        ChoroplethColorizer gdpColorizer;
        ChoroplethColorizer populationColorizer;
        LegendAlignment legendAlignment;

        VectorFileLayer FileLayer { get { return (VectorFileLayer)(mapControl1.Layers[0]); } }

        public ShapefileSupport() {
            InitializeComponent();
            FileLayer.FileLoader = CreateShapefileLoader();

            politicalColorizer = CreatePoliticalColorizer();
            gdpColorizer = CreateGDPColorizer();
            populationColorizer = CreatePopulationColorizer();

            cbLegendAlign.SelectedIndex = 0;
            rgMode.SelectedIndex = 1;
        }
        ShapefileLoader CreateShapefileLoader() {
            ShapefileLoader loader = new ShapefileLoader();
            loader.FileUri = new Uri("file:\\\\" + DemoUtils.GetRelativePath("Countries.shp"), UriKind.RelativeOrAbsolute);
            return loader;
        }
        ChoroplethColorizer CreatePoliticalColorizer() {
            ChoroplethColorizer colorizer = new ChoroplethColorizer();
            colorizer.RangeStops.AddRange( new List<double> { 0, 9 } );
            colorizer.Colors.AddRange(new List<Color> { Color.FromArgb(0x8D, 0xD3, 0xC7), Color.FromArgb(0xFF, 0xFF, 0xB3), Color.FromArgb(0xBE, 0xBA, 0xDA), Color.FromArgb(0xFB, 0x80, 0x72),
                                                        Color.FromArgb(0x80, 0xB1, 0xD3), Color.FromArgb(0xFD, 0xB4, 0x62), Color.FromArgb(0xB3, 0xDE, 0x69), Color.FromArgb(0xFC, 0xCD, 0xE5),
                                                        Color.FromArgb(0xD9, 0xD9, 0xD9), Color.FromArgb(0xBC, 0x80, 0xBD)});
            colorizer.ValueProvider = new ShapeAttributeValueProvider() { AttributeName = "MAP_COLOR" };
            return colorizer;
        }
        ChoroplethColorizer CreateGDPColorizer() {
            ChoroplethColorizer colorizer = new ChoroplethColorizer();
            ColorScaleLegend legend = new ColorScaleLegend();
            legend.Header = "GDP by Countries";
            legend.Description = "In US dollars";
            legend.RangeStopsFormat = "0,B";
            colorizer.Legend = legend;
            colorizer.RangeStops.AddRange(new List<double> { 0, 3000, 10000, 18000, 28000, 44000, 82000, 185000, 1000000, 2500000, 15000000 });
            colorizer.Colors.AddRange(new List<Color> { Color.FromArgb(0x5F, 0x8B, 0x95), Color.FromArgb(0x79, 0x96, 0x89), Color.FromArgb(0xA2, 0xA8, 0x75), Color.FromArgb(0xCE, 0xBB, 0x5F),
                                                        Color.FromArgb(0xF2, 0xCB, 0x4E), Color.FromArgb(0xF1, 0xC1, 0x49), Color.FromArgb(0xE5, 0xA8, 0x4D), Color.FromArgb(0xD6, 0x86, 0x4E),
                                                        Color.FromArgb(0xC5, 0x64, 0x50), Color.FromArgb(0xBA, 0x4D, 0x51)});
            colorizer.ValueProvider = new ShapeAttributeValueProvider() { AttributeName = "GDP_MD_EST" };
            return colorizer;
        }
        ChoroplethColorizer CreatePopulationColorizer() {
            ChoroplethColorizer colorizer = new ChoroplethColorizer();
            ColorListLegend legend = new ColorListLegend();
            legend.RangeStopsFormat = "0,,M";
            legend.Header = "";
            legend.Description = "";
            colorizer.Legend = legend;
            colorizer.RangeStops.AddRange(new List<double> { 0, 1000000, 2000000, 5000000, 10000000, 25000000, 50000000, 100000000, 1000000000, 1500000000 });
            colorizer.Colors.AddRange(new List<Color> { Color.FromArgb(0xFA, 0xDC, 0xF5), Color.FromArgb(0xBF, 0x84, 0xB6) });
            colorizer.ValueProvider = new ShapeAttributeValueProvider() { AttributeName = "POP_EST" };
            return colorizer;
        }
        void UpdateMapColorizer(MapColorizer colorizer, string toolTipPattern) {
            mapControl1.Colorizer = colorizer;
            FileLayer.ToolTipPattern = toolTipPattern;
            UpdateLegendAligment();
        }
        void rgMode_SelectedIndexChanged(object sender, EventArgs e) {
            switch (rgMode.SelectedIndex) {
                case 0:
                    UpdateMapColorizer(politicalColorizer, PoliticalToolTipPattern);
                    break;
                case 1:
                    UpdateMapColorizer(gdpColorizer, GdpToolTipPattern);
                    break;
                case 2:
                    UpdateMapColorizer(populationColorizer, PopulationToolTipPattern);
                    break;
            }
            bool showLegendOptions = mapControl1.Colorizer != politicalColorizer;
            lblLegendAlign.Visible = showLegendOptions;
            cbLegendAlign.Visible = showLegendOptions;
        }

        void cbLegendAlign_SelectedIndexChanged(object sender, EventArgs e) {
            this.legendAlignment = GetLegendAlignment(cbLegendAlign.EditValue as string);
            UpdateLegendAligment();
        }
        LegendAlignment GetLegendAlignment(string value) {
            if (string.IsNullOrEmpty(value))
                return LegendAlignment.TopRight;
            return (LegendAlignment)Enum.Parse(typeof(LegendAlignment), value);
        }
        void UpdateLegendAligment() {
            ChoroplethColorizer choroplethColorizer = mapControl1.Colorizer as ChoroplethColorizer;
            if(choroplethColorizer != null && choroplethColorizer.Legend != null) {
                choroplethColorizer.Legend.Alignment = legendAlignment;
            }
        }
            
    }
}