Back to Devexpress

How to: Implement a Custom Colorizer

windowsforms-15443-controls-and-libraries-map-control-examples-vector-data-colorize-data-how-to-implement-a-custom-colorizer.md

latest7.1 KB
Original Source

How to: Implement a Custom Colorizer

  • Nov 13, 2018
  • 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.

csharp
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];
                }
            }

        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace CustomColorizer
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Const powerAttrName As String = "PowerValue"
        Private Const maxPower As Double = 1000
        Private Const rectNumber As Integer = 50

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

            ' Create a map control, set its dock style and add it to the form.
            Dim map As New MapControl()
            map.Dock = DockStyle.Fill
            Me.Controls.Add(map)

            ' Create a vector items layer and add it to the map.
            Dim itemsLayer As New VectorItemsLayer()
            map.Layers.Add(itemsLayer)

            Dim storage As 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()

        End Sub

        Private Sub GenerateVectorItems(ByVal col As MapItemCollection)
            Dim width As Integer = 5
            Dim singlePower As Double = maxPower / rectNumber

            For i As Integer = 0 To rectNumber - 1
                Dim polygon As MapPolygon = 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)
            Next i
        End Sub

        Private Function CreatePolygon(ByVal power As Double, ByVal points() As GeoPoint) As MapPolygon
            Dim item As New MapPolygon()

            For Each point As GeoPoint In points
                item.Points.Add(point)
            Next point

            item.Attributes.Add(New MapItemAttribute() With {.Name = powerAttrName, .Type = GetType(Double), .Value = power})

            Return item
        End Function

        Private Class CustomColorizer
            Inherits MapColorizer

            Private colors() As Color = {Color.Violet, Color.Blue, Color.LightBlue, Color.Green,Color.Yellow, Color.Orange, Color.Red}

            Public Overrides Sub ColorizeElement(ByVal element As IColorizerElement)

                Dim polygon As MapPolygon = TryCast(element, MapPolygon)
                If polygon IsNot Nothing Then
                    Dim power As Double = CDbl(polygon.Attributes(powerAttrName).Value)

                    Dim linearizedPower As Integer = CInt(Fix(Math.Truncate(power * colors.Length / maxPower)))

                    element.ColorizerColor = colors(linearizedPower)
                End If
            End Sub

        End Class
    End Class
End Namespace