Back to Devexpress

VectorItemsLayer.Colorizer Property

windowsforms-devexpress-dot-xtramap-dot-vectoritemslayer-746c3c48.md

latest12.8 KB
Original Source

VectorItemsLayer.Colorizer Property

Gets or sets a value specifying the map colorizer (choropleth colorizer or graph colorizer) with which to colorize map shapes.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
[DefaultValue(null)]
public MapColorizer Colorizer { get; set; }
vb
<DefaultValue(Nothing)>
Public Property Colorizer As MapColorizer

Property Value

TypeDefaultDescription
MapColorizernull

A MapColorizer class descendant specifying the colorizer type.

|

Remarks

Use the Colorizer property to specify a colorizer to color vector map shapes based on shape data.

For more information, see Colorizers.

Example

This example demonstrates how to paint two triangles on a map using the choropleth colorizer.

To accomplish this task for map shapes, do the following:

csharp
using DevExpress.Utils;
using DevExpress.XtraMap;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Manual_Colorization {
    public partial class Form1 : Form {
        const string areaValueAttrName = "AreaValue";
        const string polygonNameAttrName = "PolygonName";

        public Form1() {
            InitializeComponent();

            InitializeMap();
        }

        void InitializeMap() {
            // Create a map control and add it to the form.
            MapControl map = new MapControl() { Dock = DockStyle.Fill };
            this.Controls.Add(map);

            // Create a layer to display vector data.
            VectorItemsLayer itemsLayer = new VectorItemsLayer();
            map.Layers.Add(itemsLayer);

            // Generate a data storage for the layer.
            itemsLayer.Data = CreateData();

            // Create a colorizer for the layer.
            itemsLayer.Colorizer = CreateColorizer();

            // Create a legend for the layer.
            map.Legends.Add(CreateLegend(itemsLayer));

            // Specify tooltips for the map.
            map.ToolTipController = new ToolTipController() { AllowHtmlText = true };
        }

        private MapItemStorage CreateData() {
            MapItemStorage storage = new MapItemStorage();

            // Create the first triangle.
            storage.Items.Add(
                CreatePolygon(800, "Small triangle",
                    new GeoPoint[] { 
                        new GeoPoint(0, 0), new GeoPoint(0, 40), 
                        new GeoPoint(40, 0), new GeoPoint(0, 0) 
                    }
                 )
             );

            // Create the second triangle.
            storage.Items.Add(
                CreatePolygon(1800, "Large triangle",
                    new GeoPoint[] { 
                        new GeoPoint(0, 0), new GeoPoint(0, -60), 
                        new GeoPoint(-60, 0), new GeoPoint(0, 0) 
                    }
                )
             );

            return storage;
        }

        #region #CreatePolygon
        private MapPolygon CreatePolygon(double areaValue, string polygonName, GeoPoint[] points) {
            MapPolygon item = new MapPolygon();

            item.Attributes.Add(new MapItemAttribute() {
                Name = areaValueAttrName,
                Type = typeof(double),
                Value = areaValue
            });
            item.Attributes.Add(new MapItemAttribute() {
                Name = polygonNameAttrName,
                Type = typeof(string),
                Value = polygonName
            });

            item.ToolTipPattern = "{" + polygonNameAttrName + "}=<b>{" + areaValueAttrName + "}</b>";

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

            return item;
        }
        #endregion #CreatePolygon

        private MapColorizer CreateColorizer() {
            ChoroplethColorizer colorizer = new ChoroplethColorizer();
            colorizer.ValueProvider = new ShapeAttributeValueProvider {
                AttributeName = areaValueAttrName
            };

            colorizer.RangeStops.AddRange(new List<double> { 0, 1000, 2000 });
            colorizer.ColorItems.AddRange(new List<ColorizerColorItem> {
                new ColorizerColorItem(Color.Yellow),
                new ColorizerColorItem(Color.Red)
            });

            return colorizer;
        }

        private MapLegendBase CreateLegend(MapItemsLayerBase layer) {
            ColorScaleLegend legend = new ColorScaleLegend();
            legend.Header = "Area";
            legend.Layer = layer;
            return legend;
        }
    }

}
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Manual_Colorization {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
vb
Imports DevExpress.Utils
Imports DevExpress.XtraMap
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

Namespace Manual_Colorization
    Partial Public Class Form1
        Inherits Form

        Private Const areaValueAttrName As String = "AreaValue"
        Private Const polygonNameAttrName As String = "PolygonName"

        Public Sub New()
            InitializeComponent()

            InitializeMap()
        End Sub

        Private Sub InitializeMap()
            ' Create a map control and add it to the form.
            Dim map As New MapControl() With {.Dock = DockStyle.Fill}
            Me.Controls.Add(map)

            ' Create a layer to display vector data.
            Dim itemsLayer As New VectorItemsLayer()
            map.Layers.Add(itemsLayer)

            ' Generate a data storage for the layer.
            itemsLayer.Data = CreateData()

            ' Create a colorizer for the layer.
            itemsLayer.Colorizer = CreateColorizer()

            ' Create a legend for the layer.
            map.Legends.Add(CreateLegend(itemsLayer))

            ' Specify tooltips for the map.
            map.ToolTipController = New ToolTipController() With {.AllowHtmlText = True}
        End Sub

        Private Function CreateData() As MapItemStorage
            Dim storage As New MapItemStorage()

            ' Create the first triangle.
            storage.Items.Add(CreatePolygon(800, "Small triangle", New GeoPoint() { _
                New GeoPoint(0, 0), _
                New GeoPoint(0, 40), _
                New GeoPoint(40, 0), _
                New GeoPoint(0, 0) _
            }))

            ' Create the second triangle.
            storage.Items.Add(CreatePolygon(1800, "Large triangle", New GeoPoint() { _
                New GeoPoint(0, 0), _
                New GeoPoint(0, -60), _
                New GeoPoint(-60, 0), _
                New GeoPoint(0, 0) _
            }))

            Return storage
        End Function

        #Region "#CreatePolygon"
        Private Function CreatePolygon(ByVal areaValue As Double, ByVal polygonName As String, ByVal points() As GeoPoint) As MapPolygon
            Dim item As New MapPolygon()

            item.Attributes.Add(New MapItemAttribute() With {.Name = areaValueAttrName, .Type = GetType(Double), .Value = areaValue})
            item.Attributes.Add(New MapItemAttribute() With {.Name = polygonNameAttrName, .Type = GetType(String), .Value = polygonName})

            item.ToolTipPattern = "{" & polygonNameAttrName & "}=<b>{" & areaValueAttrName & "}</b>"

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

            Return item
        End Function
        #End Region ' #CreatePolygon

        Private Function CreateColorizer() As MapColorizer
            Dim colorizer As New ChoroplethColorizer()
            colorizer.ValueProvider = New ShapeAttributeValueProvider With {.AttributeName = areaValueAttrName}

            colorizer.RangeStops.AddRange(New List(Of Double) From {0, 1000, 2000})
            colorizer.ColorItems.AddRange(New List(Of ColorizerColorItem) From { _
                New ColorizerColorItem(Color.Yellow), _
                New ColorizerColorItem(Color.Red) _
            })

            Return colorizer
        End Function

        Private Function CreateLegend(ByVal layer As MapItemsLayerBase) As MapLegendBase
            Dim legend As New ColorScaleLegend()
            legend.Header = "Area"
            legend.Layer = layer
            Return legend
        End Function
    End Class

End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms

Namespace Manual_Colorization
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Colorizer property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-map-create-choropleth-map-based-on-shapefile/CS/XtraMap_ShapefileDataAdapter/Form1.cs#L21

csharp
// Create colorizer for the MapLayer.
MapLayer.Colorizer = CreateColorizer();
#endregion #ColorizerProperty

winforms-map-create-choropleth-map-based-on-shapefile/VB/XtraMap_ShapefileDataAdapter/Form1.vb#L27

vb
' Create colorizer for the MapLayer.
            Me.MapLayer.Colorizer = Me.CreateColorizer()
'#End Region ' #ColorizerProperty

See Also

VectorItemsLayer Class

VectorItemsLayer Members

DevExpress.XtraMap Namespace