How to: Implement a Custom Colorizer

  • 4 minutes to read

This example demonstrates how to create a custom colorizer and use it for implementing a progress bar on a map.

The colorizer fills bars with colors according to the specified power value stored as a shape attribute.

To implement a custom colorizer, create a class inherited from the MapColorizer base class and implement the ColorizerBase<T>.ColorizeElement method according to your custom rules.

For example, in this case, the custom colorizer does the following:

  1. Obtains the power value from the shape attribute (via the MapItem.Attributes property);

  2. Selects a color that corresponds to this value (e.g., from the predefined array of Color objects);

  3. Assigns this color to the IColorizerElement.ColorizerColor property of the element passed to the ColorizerBase<T>.ColorizeElement method.

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace CustomColorizer {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        const string powerAttrName = "PowerValue";
        const double maxPower = 1000;
        const int rectNumber = 50;

        private void Form1_Load(object sender, EventArgs e) {

            // Create a map control, set its dock style and add it to the form.
            MapControl map = new MapControl();
            map.Dock = DockStyle.Fill;
            this.Controls.Add(map);

            // Create a vector items layer and add it to the map.
            VectorItemsLayer itemsLayer = new VectorItemsLayer();
            map.Layers.Add(itemsLayer);

            MapItemStorage storage = new MapItemStorage();
            itemsLayer.Data = storage;
            // Generate map polygons.
            GenerateVectorItems(storage.Items);

            // Specify the tooltip content.            
            itemsLayer.ToolTipPattern = "{" + powerAttrName + "}";

            // Create a custom colorizer.
            itemsLayer.Colorizer = new CustomColorizer();

        }

        private void GenerateVectorItems(MapItemCollection col) {
            int width = 5;
            double singlePower = maxPower / rectNumber;

            for (int i = 0; i < rectNumber; i++) {
                MapPolygon polygon = CreatePolygon(i * singlePower,
                new GeoPoint[] { new GeoPoint(0, width * i), 
                                 new GeoPoint(0, width * (i + 1)),
                                 new GeoPoint(40, width * (i + 1)),
                                 new GeoPoint(40, width * i), 
                                 new GeoPoint(0, width * i) });
                col.Add(polygon);
            }
        }


        private MapPolygon CreatePolygon(double power, GeoPoint[] points) {
            MapPolygon item = new MapPolygon();

            foreach (GeoPoint point in points) {
                item.Points.Add(point);
            }

            item.Attributes.Add(new MapItemAttribute() { Name = powerAttrName, Type = typeof(Double), Value = power });

            return item;
        }


        private class CustomColorizer : MapColorizer {

            Color[] colors = {Color.Violet, Color.Blue, Color.LightBlue, Color.Green,Color.Yellow, Color.Orange, Color.Red};

            public override void ColorizeElement(IColorizerElement element) {

                MapPolygon polygon = element as MapPolygon;
                if (polygon != null) {
                    double power = (double)polygon.Attributes[powerAttrName].Value;

                    int linearizedPower = (int)Math.Truncate(power * colors.Length / maxPower);

                    element.ColorizerColor = colors[linearizedPower];
                }
            }


        }
    }
}