使用图例
本示例内容可以参考CaseStudy目录下的文件"地图.Table"的示例十五。
本节的任务是在上一节的基础上加上图例:

提示:本节的代码使用了lambda过程,如果你还不熟悉,请参考:使用lambda过程
示例
1、将Map控件的"地图来源"属性设置为"None"
2、将窗口的AfterLoad事件代码设置为:
Dim
map
As
GeoMap = e.Form.Controls("Map1").GeoMap
Dim
layer
As
New
VectorLayer()
map.Layers.Add(layer)
layer.Style.Stroke.Color = Color.Gray
Dim
minValue
As
Double
= DataTables("市").Compute("Min(人均GDP)")
Dim
maxValue
As
Double
= DataTables("市").Compute("Max(人均GDP)")
Dim
minColor
As
Color = Color.FromArgb(230, 247, 255)
'
浅蓝
Dim
maxColor
As
Color = Color.FromArgb(74, 0, 114)
'深紫
'定义一个lambda过程,用于根据GDP计算背景颜色
Dim
getValueColor =
Function(tValue
As
Double)
Dim
radio
As
Double
tValue = Math.Min(tValue, maxValue)
radio = tValue / maxValue
Dim
red
As
Byte
= minColor.R + (CInt(maxColor.R)
-
CInt(minColor.R))
* radio
Dim
green
As
Byte
= minColor.G +
CInt((maxColor.G)
-
CInt(minColor.G))
* radio
Dim
blue
As
Byte
= minColor.B + (CInt(maxColor.B)
-
CInt(minColor.B))
* radio
Return
Color.FromArgb(red, green, blue)
End
Function
'绘制市级行政区
For
Each
dr
As
DataRow
In
DataTables("行政区域").Select("level
= 2 or (level = 3 and parentLevel = 1)")
Dim
polygon
As
VectorPolygon = map.CreatePolygon(dr("geometry"),
5)
layer.Items.Add(Polygon)
Dim
sr
As
DataRow = DataTables("市").Find("市代码
= '"
& dr("adcode")
&
"'")
If
sr
IsNot
Nothing
Then
Dim
gdp
As
Double
= sr("人均GDP")
polygon.Style.BackColor = getValueColor.Invoke(gdp)
polygon.Tag = dr("name")
&
":"
& sr("人均GDP")
Else
polygon.Tag = dr("name")
End
If
Next
'增加图例
Dim
legend
As
New
MapLegend
'定义图例
map.Legends.Add(legend)
'增加图例
legend.Alignment = ContentAlignment.MiddleRight
'图例位置为中间靠右
legend.Layout = MapLegendLayout.Column
'垂直显示
legend.Style.BackColor = Color.White
'设置背景颜色为白色,避免和地图重叠的时候难以识别
legend.Margin =
New
Padding(10)
'边距设置为10,避免图例紧贴在窗口边框
For
value
As
Integer
= 0
To
maxvalue
Step
5
'从0开始,以5(万)为间隔增加一个图例项目
Dim
item
As
New
MapLegendItem
item.Size =
New
Size(20, 20)
item.Kind = C1.FlexMap.MapLegendItemKind.Rectangle
item.Style.BackColor = getValueColor.Invoke(value)
'如果不想使用渐变,可以不设置BackColor2
item.Style.BackColor2 = getValueColor.Invoke(value + 5)
item.Style.Stroke.Color = Color.LightGray
item.Style.Alignment = ContentAlignment.MiddleLeft
item.Label = value.ToString()
legend.Items.Add(item)
Next