Form1.cs |
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() {
MapControl map = new MapControl() { Dock = DockStyle.Fill };
this.Controls.Add(map);
VectorItemsLayer itemsLayer = new VectorItemsLayer();
map.Layers.Add(itemsLayer);
itemsLayer.Data = CreateData();
itemsLayer.Colorizer = CreateColorizer();
map.Legends.Add(CreateLegend(itemsLayer));
map.ToolTipController = new ToolTipController() { AllowHtmlText = true };
}
private MapItemStorage CreateData() {
MapItemStorage storage = new MapItemStorage();
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)
}
)
);
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;
}
}
}
|
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
|