Class Breaks Renderer

Download Samples Repository

Description

Sample shows how to create a ClassBreaksRenderer for a graphics layer. USA cities data points are pulled from an online source and rendered using the GraphicsLayer ClassBreaksRenderer.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ClassBreaksRendererSample"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
	<Grid x:Name="layoutGrid">
		<Grid.Resources>
			<esri:SimpleMarkerSymbol x:Key="pop0to50K" Color="#FF0A57FC" Size="12" Style="Triangle" />
			<esri:SimpleMarkerSymbol x:Key="pop50Kto100K" Color="#FF1CA2A3" Size="12" Style="Triangle" />
			<esri:SimpleMarkerSymbol x:Key="pop100Kto250K" Color="#FF60AD12" Size="12" Style="Triangle" />
			<esri:SimpleMarkerSymbol x:Key="pop250Kto500K" Color="#FFFFFE0A" Size="12" Style="Triangle" />
			<esri:SimpleMarkerSymbol x:Key="pop500Kto1000K" Color="#FFFDA208" Size="12" Style="Triangle" />
			<esri:SimpleMarkerSymbol x:Key="pop1000K" Color="#FFFB5107" Size="12" Style="Triangle" />
		</Grid.Resources>

		<esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map>
				<esri:Map.InitialViewpoint>
					<esri:ViewpointExtent XMin="-15053000" YMin="2749000" XMax="-6540000" YMax="6590000" />
				</esri:Map.InitialViewpoint>
				<esri:ArcGISTiledMapServiceLayer
					ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="cities">
					<esri:GraphicsOverlay.Renderer>
						<esri:ClassBreaksRenderer Field="pop2000" >
							<esri:ClassBreaksRenderer.Infos>
								<esri:ClassBreakInfo Minimum="0" Maximum="50000" Symbol="{StaticResource pop0to50K}" />
								<esri:ClassBreakInfo Minimum="500000" Maximum="100000" Symbol="{StaticResource pop50Kto100K}" />
								<esri:ClassBreakInfo Minimum="100000" Maximum="250000" Symbol="{StaticResource pop100Kto250K}" />
								<esri:ClassBreakInfo Minimum="250000" Maximum="500000" Symbol="{StaticResource pop250Kto500K}" />
								<esri:ClassBreakInfo Minimum="500000" Maximum="1000000" Symbol="{StaticResource pop500Kto1000K}" />
								<esri:ClassBreakInfo Minimum="1000000" Maximum="5000000" Symbol="{StaticResource pop1000K}" />
							</esri:ClassBreaksRenderer.Infos>
						</esri:ClassBreaksRenderer>
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1" Margin="30" Padding="20"
				HorizontalAlignment="Right" VerticalAlignment="Top">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<StackPanel>
				<TextBlock Text="USA city points are rendered using the renderer of the graphics layer. Click 'Change Renderer' to change the renderer to a new ClassBreaksRenderer."
						   FontSize="14"  Width="400" TextAlignment="Left" TextWrapping="Wrap" />
				<Button Content="Change Renderer" HorizontalAlignment="Center" Margin="12,12,12,0"
						Click="ChangeRendererButton_Click"/>
			</StackPanel>
		</Border>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Sample shows how to create a ClassBreaksRenderer for a graphics layer. USA cities data points are pulled from an online source and rendered using the GraphicsLayer ClassBreaksRenderer.
	/// </summary>
	/// <title>Class Breaks Renderer</title>
	/// <category>Symbology</category>
	public partial class ClassBreaksRendererSample : UserControl
	{
		private Random _random = new Random();
		private GraphicsOverlay _cities;

		/// <summary>Construct Class Breaks Renderer sample control</summary>
		public ClassBreaksRendererSample()
		{
			InitializeComponent();

			_cities = MyMapView.GraphicsOverlays["cities"];

			MyMapView.ExtentChanged += MyMapView_ExtentChanged;
		}

		// Load earthquake data
		private async void MyMapView_ExtentChanged(object sender, EventArgs e)
		{
			try
			{
				MyMapView.ExtentChanged -= MyMapView_ExtentChanged;
				await LoadUSACitiesAsync();
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error loading data: " + ex.Message, "Class Breaks Renderer Sample");
			}
		}

		// Change the graphics layer renderer to a new ClassBreaksRenderer
		private void ChangeRendererButton_Click(object sender, RoutedEventArgs e)
		{
			SimpleMarkerStyle style = (SimpleMarkerStyle)_random.Next(0, 6);

			_cities.Renderer = new ClassBreaksRenderer()
			{
				Field = "pop2000",
				Infos = new ClassBreakInfoCollection() 
				{ 
					new ClassBreakInfo() { Minimum = 0, Maximum = 50000, Symbol = GetRandomSymbol(style) },
					new ClassBreakInfo() { Minimum = 50000, Maximum = 100000, Symbol = GetRandomSymbol(style) },
					new ClassBreakInfo() { Minimum = 100000, Maximum = 250000, Symbol = GetRandomSymbol(style) },
					new ClassBreakInfo() { Minimum = 250000, Maximum = 500000, Symbol = GetRandomSymbol(style) },
					new ClassBreakInfo() { Minimum = 500000, Maximum = 1000000, Symbol = GetRandomSymbol(style) },
					new ClassBreakInfo() { Minimum = 1000000, Maximum = 5000000, Symbol = GetRandomSymbol(style) },
				}
			};
		}

		// Load earthquakes from map service
		private async Task LoadUSACitiesAsync()
		{
			var queryTask = new QueryTask(
				new Uri("http://sampleserver6.arcgisonline.com/ArcGIS/rest/services/USA/MapServer/0"));

            // Get current viewpoints extent from the MapView
            var currentViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
            var viewpointExtent = currentViewpoint.TargetGeometry.Extent;

			var query = new Query(viewpointExtent)
			{
				ReturnGeometry = true,
				OutSpatialReference = MyMapView.SpatialReference,
				Where = "pop2000 > 50000",
				OutFields = new OutFields(new List<string> { "pop2000" })
			};
			var result = await queryTask.ExecuteAsync(query);

			_cities.Graphics.Clear();
			_cities.Graphics.AddRange(result.FeatureSet.Features.OfType<Graphic>());
		}

		// Utility: Generate a random simple marker symbol
		private SimpleMarkerSymbol GetRandomSymbol(SimpleMarkerStyle style)
		{
			return new SimpleMarkerSymbol()
			{
				Size = 12,
				Color = GetRandomColor(),
				Style = style
			};
		}

		// Utility function: Generate a random System.Windows.Media.Color
		private Color GetRandomColor()
		{
			var colorBytes = new byte[3];
			_random.NextBytes(colorBytes);
			return Color.FromRgb(colorBytes[0], colorBytes[1], colorBytes[2]);
		}
	}
}
Feedback on this topic?