MSDN Magazine - December 2007 - (Page 56) Figure 12 Modified XAML for Displaying a Heat-Map System.Windows.Input.MouseEventArgs) CType(sender.DataContext, MapPolygon).Region.IsSelected = False End Sub Figure 11 shows an example of this in action. Visualizing Population Data Visualizing population data on top of the map is also simple. XAML code that does this is shown in Figure 12. It modifies the XAML used to draw the map by adding a second ItemsControl named DataLayer to MasterCanvas. Because DataLayer is located inside MasterCanvas, its contents will be transformed in the same way as MapViewer’s. This allows the data bound to DataLayer to also be specified in terms of the underlying domain model rather than in terms of the user interface. Unlike MapViewer, DataLayer is not intended to inherit its data context directly from MasterCanvas. Instead, it is designed to have the data to be visualized set explicitly as its ItemsSource. The data provided should be a collection of objects defining two properties: Color and Points. The Color property should be of type Brush and defines how the polygon described by Points should be colored on the map. The Points property should be an instance of PointsCollection that defines the vertices of the polygon to be colored. This is the DataTemplate used to render DataLayer’s elements: the polygon. In particular, it references the RegionPolygonStyle resource that defines two distinct styles for rendering polygons. Those polygons whose regions have IsSelected values of true are rendered with a thick orange border, and those whose IsSelected values are false are rendered with a regular, thin black border. This event-handling code shows how such styles can be easily used to implement cool UI features like input tracking: Private Sub Polygon_MouseEnter(ByVal sender As Polygon, ByVal e As _ System.Windows.Input.MouseEventArgs) CType(sender.DataContext, MapPolygon).Region.IsSelected = True End Sub Private Sub Polygon_MouseLeave(ByVal sender As Polygon, ByVal e As _ The way it works is by simply creating a polygon and binding its Figure 13 LoadData Public Sub LoadData() Dim list As New List(Of MapRegion) LoadFile(“PolygonData\2000\states.xml”, list) Dim q = From r In list Where Not excludedStates.Contains(r.RegionName) m_Map = New Map() Dim first As Boolean = True Dim bb As Rect For Each item In q m_Map.Regions.Add(item) If (first) Then bb = item.BoundingBox first = False Else bb.Enclose(item.BoundingBox) End If Join _ county In counties _ On _ state.FipsCodes(0) Equals county.FipsCodes(0) _ Join _ pd In doc. . _ On _ CInt(pd.@CountyFipsCode) Equals county.FipsCodes(1) And _ CInt(pd.@StateFipsCode) Equals state.FipsCodes(0) _ Where _ CDate(pd.@Date).Year = 2006 _ From _ Polygon In county.Polygons _ Select _ Color = New SolidColorBrush(colorMapEntries.Interprolate( _ CDbl(pd.@Population))), Points = Polygon.Points q2 = q2.ToList() DataLayer.ItemsSource = q2 ‘Force the state boundaries to be on top of the county regions MasterCanvas.Children.Remove(MapViewer) MasterCanvas.Children.Add(MapViewer) Dim legendInfo(colorMapEntries.Length - 2) As Object For i = 0 To colorMapEntries.Length - 2 legendInfo(colorMapEntries.Length - (i + 2)) = _ New With{.StartColor = colorMapEntries(i).FillColor, _ .StopColor = colorMapEntries(i + 1).FillColor, .Value = _ If(colorMapEntries(i + 1).ShowInIndex, colorMapEntries( _ i + 1).Value.ToString(“n”), “”)} Next Legend.ItemsSource = legendInfo End Sub Next m_Map.BoundingBox = bb MasterCanvas.DataContext = m_Map m_Map.ScaleTo(New Size(MasterCanvas.ActualWidth, _ MasterCanvas.ActualHeight)) Dim counties = New List(Of MapRegion) LoadFile(“PolygonData\2000\counties.xml”, counties) Dim doc = XDocument.Load(“PopulationData\CountyPopulation.xml”) Dim q2 = _ From _ state In m_Map.Regions _ 56 msdnmagazine Map LINQ
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.