Unique Value Renderer

Download Samples Repository

Description

Sample shows how to create a UniqueValueRenderer for a graphics layer. US state polygons are pulled from an online source and rendered using the GraphicsLayer UniqueValueRenderer.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.UniqueValueRendererSample"
             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">
        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15053000,2749000,-6540000,6590000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="states"/>
			</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="US state polygons are rendered using the renderer of the graphics layer. The 'Change Renderer' button will change the renderer to a new UniqueValueRenderer and redraw the map."
                           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.Collections.ObjectModel;
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 UniqueValueRenderer for a graphics layer. US state polygons are pulled from an online source and rendered using the GraphicsLayer UniqueValueRenderer.
	/// </summary>
	/// <title>Unique Value Renderer</title>
	/// <category>Symbology</category>
	public partial class UniqueValueRendererSample : UserControl
	{
		private Random _random = new Random();
		private GraphicsOverlay _states;

		/// <summary>Construct Unique Value Renderer sample control</summary>
		public UniqueValueRendererSample()
		{
			InitializeComponent();

			_states = MyMapView.GraphicsOverlays["states"];

			MyMapView.ExtentChanged += MyMapView_ExtentChanged;
		}

		// Load state data - set initial renderer
		private async void MyMapView_ExtentChanged(object sender, EventArgs e)
		{
			try
			{
				MyMapView.ExtentChanged -= MyMapView_ExtentChanged;
				await LoadStatesAsync();

				ChangeRenderer();
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error loading states data: " + ex.Message, "Unique Value Renderer Sample");
			}
		}

		// Change the graphics layer renderer to a new UniqueValueRenderer
		private void ChangeRendererButton_Click(object sender, RoutedEventArgs e)
		{
			ChangeRenderer();
		}

		private void ChangeRenderer()
		{
			var renderer = new UniqueValueRenderer() 
			{ 
				Fields = new ObservableCollection<string>(new List<string> { "sub_region" }) 
			};

			renderer.Infos = new UniqueValueInfoCollection(_states.Graphics
				.Select(g => g.Attributes["sub_region"])
				.Distinct()
				.Select(obj => new UniqueValueInfo { 
					Values = new ObservableCollection<object>(new object[] { obj }), 
					Symbol = GetRandomSymbol() 
				}));

			_states.Renderer = renderer;
		}

		// Load US state data from map service
		private async Task LoadStatesAsync()
		{
			var queryTask = new QueryTask(
				new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2"));

            // 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,
				MaxAllowableOffset = MyMapView.UnitsPerPixel,
				OutSpatialReference = MyMapView.SpatialReference,
				OutFields = new OutFields(new List<string> { "sub_region" })
			};
			var result = await queryTask.ExecuteAsync(query);

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

		// Utility: Generate a random simple fill symbol
		private SimpleFillSymbol GetRandomSymbol()
		{
			var color = GetRandomColor();

			return new SimpleFillSymbol()
			{
				Color = Color.FromArgb(0x77, color.R, color.G, color.B),
				Outline = new SimpleLineSymbol() { Width = 2, Style = SimpleLineStyle.Solid, Color = color },
				Style = SimpleFillStyle.Solid
			};
		}

		// 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?