Hi to All,
Can you please suggest me how to add colors to shapes in a map randomly?
Hi to All,
Can you please suggest me how to add colors to shapes in a map randomly?
Hello Rajesh,
Thank you for your post, we recommend to use ValueStyle to set different color for the lines. This sample code might help. Please let me know if you have more questions.
wpfMap1.MapUnit = GeographyUnit.DecimalDegree
Dim worldMapKitOverlay As New WorldMapKitWmsWpfOverlay()
wpfMap1.Overlays.Add(worldMapKitOverlay)
Dim valueStyle As New ValueStyle()
valueStyle.ColumnName = "Index"
Dim valueItem1 As New ValueItem()
valueItem1.DefaultLineStyle = LineStyles.Highway1
valueItem1.Value = 0
valueStyle.ValueItems.Add(valueItem1)
Dim valueItem2 As New ValueItem()
valueItem2.DefaultLineStyle = LineStyles.DegreeLine1
valueItem2.Value = 1
valueStyle.ValueItems.Add(valueItem2)
Dim valueItem3 As New ValueItem()
valueItem3.DefaultLineStyle = LineStyles.Canal1
valueItem3.Value = 2
valueStyle.ValueItems.Add(valueItem3)
Dim inMemoryFeatureLayer As New InMemoryFeatureLayer()
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle)
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
inMemoryFeatureLayer.Open()
inMemoryFeatureLayer.Columns.Add(New FeatureSourceColumn("Index"))
inMemoryFeatureLayer.Close()
Dim random As New Random()
For index = 0 To 30
Dim x1 As Double = random.Next(-180, 180)
Dim x2 As Double = random.Next(-180, 180)
Dim y1 As Double = random.Next(-90, 90)
Dim y2 As Double = random.Next(-90, 90)
Dim lineShape = New LineShape()
lineShape.Vertices.Add(New Vertex(x1, y1))
lineShape.Vertices.Add(New Vertex(x2, y2))
Dim newFeature As Feature = New Feature(lineShape)
newFeature.ColumnValues.Add("Index", index Mod 3)
inMemoryFeatureLayer.InternalFeatures.Add(newFeature)
Next
Dim layerOverlay As New LayerOverlay()
layerOverlay.TransitionEffect = TransitionEffect.None
layerOverlay.Layers.Add("WorldLayer", inMemoryFeatureLayer)
wpfMap1.Overlays.Add("WorldOverlay", layerOverlay)
wpfMap1.CurrentExtent = New RectangleShape(-133.2515625, 89.2484375, 126.9046875, -88.290625)
wpfMap1.Refresh()
Regards,
Gary
I do something similar, but load up an array with various colors I want to use.... then loop through them. Code is something like (untested, but pulled from working code):
Private Gcolor() As GeoColor = New GeoColor() { _
GeoColor.SimpleColors.Black, _
GeoColor.SimpleColors.Blue, _
GeoColor.SimpleColors.BrightBlue, _
GeoColor.SimpleColors.BrightGreen, _
GeoColor.SimpleColors.BrightOrange, _
GeoColor.SimpleColors.BrightRed, _
GeoColor.SimpleColors.BrightYellow, _
GeoColor.SimpleColors.Copper, _
GeoColor.SimpleColors.DarkBlue, _
GeoColor.SimpleColors.DarkGreen, _
GeoColor.SimpleColors.DarkOrange, _
GeoColor.SimpleColors.DarkRed, _
GeoColor.SimpleColors.DarkYellow, _
GeoColor.SimpleColors.Gold, _
GeoColor.SimpleColors.Green, _
GeoColor.SimpleColors.LightBlue, _
GeoColor.SimpleColors.LightGreen, _
GeoColor.SimpleColors.LightOrange, _
GeoColor.SimpleColors.LightRed, _
GeoColor.SimpleColors.LightYellow, _
GeoColor.SimpleColors.Orange, _
GeoColor.SimpleColors.PaleBlue, _
GeoColor.SimpleColors.PaleGreen, _
GeoColor.SimpleColors.PaleOrange, _
GeoColor.SimpleColors.PaleRed, _
GeoColor.SimpleColors.PaleYellow, _
GeoColor.SimpleColors.PastelBlue, _
GeoColor.SimpleColors.PastelGreen, _
GeoColor.SimpleColors.PastelOrange, _
GeoColor.SimpleColors.PastelRed, _
GeoColor.SimpleColors.PastelYellow, _
GeoColor.SimpleColors.Red, _
GeoColor.SimpleColors.Silver, _
GeoColor.SimpleColors.Yellow}
Private Sub CreateVariousColorLines()
Dim lineLayer As New InMemoryFeatureLayer()
Dim valueStyle As New ValueStyle()
valueStyle.ColumnName = "Index"
For i As Integer = 0 To Gcolor.Count - 1 'Load up all the colors for the lines
Dim valueItem1 As New ValueItem()
valueItem1.DefaultLineStyle = New LineStyle(New GeoPen(GeoColor.FromArgb(150, Gcolor(i)), 3))
valueItem1.Value = i
valueStyle.ValueItems.Add(valueItem1)
Next
lineLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle)
lineLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
lineLayer.Open()
lineLayer.Columns.Add(New FeatureSourceColumn("Index"))
lineLayer.Close()
MyLineOverlay.Layers.Add(lineLayer)
For i As Integer = 1 To MyLatLonClassList.Count - 2
Dim lShape As New LineShape
lShape.Vertices.Add(New Vertex(MyLatLonClassList(i).LON, MyLatLonClassList(i).LAT))
lShape.Vertices.Add(New Vertex(MyLatLonClassList(i + 1).LON, MyLatLonClassList(i + 1).LAT))
Dim f As New Feature(lShape)
f.ColumnValues.Add("Index", GetNextColor())
lineLayer.InternalFeatures.Add(f)
Next
End Sub
Private _ColorIdx As Integer = 0
Public Function GetNextColor() As Integer
Dim idx As Integer = _ColorIdx
If _ColorIdx = Gcolor.Count - 1 Then
_ColorIdx = 0
Else
_ColorIdx += 1
End If
Return idx
End Function
Thinking about it, instead of using the array, you could just do something like below to get a random color (untested):
Private _Rnd As New Random
Private _uniqueColorList As New List(Of GeoColor)
Private Function NextRandomColor() As GeoColor
If _uniqueColorList.Count > 9999 Then _uniqueColorList.Clear() 'I just picked 9999 as some arbitrary max list of colors
Dim gc As New GeoColor
gc = GeoColor.FromArgb(255, _Rnd.Next(0, 255), _Rnd.Next(0, 255), _Rnd.Next(0, 255))
While Not _uniqueColorList.Contains(gc)
gc = GeoColor.FromArgb(255, _Rnd.Next(0, 255), _Rnd.Next(0, 255), _Rnd.Next(0, 255))
End While
_uniqueColorList.Add(gc)
Return gc
End Function
Hello Michael,
Thank you very much.
Regards,
Gary
Guys,
I see that you are using existing styles, another way would be to create a custom random color line style. The style itself would randomly pick a color. It would pick a different color each time it draws the same feature or the color, once chose, could stick to the feature. The custom style would be the most elegant way to do this. If anyone is interested let me know and I can whip up a sample of this. Custom styles are pretty easy and encapsulate the style logic neatly.
David