Generate Renderer Task

Download Samples Repository

Description

This sample uses an ArcGIS Server Task to generate a Class Breaks Renderer with five classes. Note that this functionality requires an ArcGIS Server 10.1 instance. The generate renderer task can also generate unique value renderers and can also generate color ramp(s) from two or more colors.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.GenerateRenderer"
             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="-14131917.758914,5680423.82721854,-12669734.6418583,6341452.4447208">
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer" />
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Reference_Overlay/MapServer" />
				<esri:FeatureLayer x:Name="featureLayer" Opacity="0.8">
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable 
                            Mode="Snapshot"
                            ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2"
                            Where="STATE_NAME = 'Washington'"
                            OutFields="POP2007,POP07_SQMI,WHITE,BLACK,AMERI_ES,ASIAN,HAWN_PI,OTHER,MULT_RACE,HISPANIC,STATE_NAME,NAME" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
            </esri:Map>
        </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="Select a field to use to create a renderer for the counties in Washington state."
                           FontSize="14" Width="300" TextAlignment="Left" TextWrapping="Wrap" />

                <StackPanel HorizontalAlignment="Center">
                    <TextBlock x:Name="txtLegendTitle" Text="{Binding}" TextAlignment="Center" 
                               Margin="12,18" />
                    <ListView x:Name="listLegend" HorizontalAlignment="Center">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{Binding SymbolImage}" Margin="8,0" />
                                    <TextBlock Text="{Binding Label}" FontSize="14" VerticalAlignment="Center" />
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>

                <StackPanel Margin="12,24,12,0" HorizontalAlignment="Center">
                    <TextBlock Text="Current Attribute:" Margin="4" />
                    <ComboBox x:Name="comboField" Width="250" HorizontalAlignment="Center"
                          SelectedValuePath="Content" SelectionChanged="comboField_SelectionChanged">
                        <ComboBoxItem Tag="POP2007">Population(2007)</ComboBoxItem>
                        <ComboBoxItem Tag="POP07_SQMI">Population/Square Mile(2007)</ComboBoxItem>
                        <ComboBoxItem Tag="WHITE">White</ComboBoxItem>
                        <ComboBoxItem Tag="BLACK">Black</ComboBoxItem>
                        <ComboBoxItem Tag="AMERI_ES">Native Americans</ComboBoxItem>
                        <ComboBoxItem Tag="HISPANIC">Hispanic</ComboBoxItem>
                        <ComboBoxItem Tag="ASIAN">Asian</ComboBoxItem>
                        <ComboBoxItem Tag="HAWN_PI">Native Hawaiian/Pacific Islander</ComboBoxItem>
                        <ComboBoxItem Tag="MULT_RACE">Multiple Races</ComboBoxItem>
                        <ComboBoxItem Tag="OTHER">Other</ComboBoxItem>
                    </ComboBox>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
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>
    /// This sample uses an ArcGIS Server Task to generate a Class Breaks Renderer with five classes. Note that this functionality requires an ArcGIS Server 10.1 instance. The generate renderer task can also generate unique value renderers and can also generate color ramp(s) from two or more colors.
    /// </summary>
    /// <title>Generate Renderer Task</title>
    /// <category>Symbology</category>
	public partial class GenerateRenderer : UserControl
    {
        private SimpleFillSymbol _baseSymbol;

        /// <summary>Construct Generate Renderer sample control</summary>
        public GenerateRenderer()
        {
            InitializeComponent();

            var lineSymbol = new SimpleLineSymbol() { Color = Colors.Black, Width = 0.5 };
            _baseSymbol = new SimpleFillSymbol() { Color = Colors.Transparent, Outline = lineSymbol, Style = SimpleFillStyle.Solid };

            MyMapView.ExtentChanged += MyMapView_ExtentChanged;
        }

        // Load data - set initial renderer after the map has an extent and feature layer loaded
        private async void MyMapView_ExtentChanged(object sender, EventArgs e)
        {
            try
            {
				MyMapView.ExtentChanged -= MyMapView_ExtentChanged;

				var table = featureLayer.FeatureTable as ServiceFeatureTable;
				table.MaxAllowableOffset = MyMapView.UnitsPerPixel;

				await MyMapView.LayersLoadedAsync();

				comboField.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Generate Renderer Task Sample");
            }
        }

        // When field to summarize on changes, generate a new renderer from the map service
        private async void comboField_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                // Generate a new renderer
                GenerateRendererTask generateRenderer = new GenerateRendererTask(
                    new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2"));

                var colorRamp = new ColorRamp()
                {
                    Algorithm = Algorithm.Hsv,
                    From = Color.FromRgb(0x99, 0x8E, 0xC3),
                    To = Color.FromRgb(0xF1, 0xA3, 0x40)
                };

                var classBreaksDef = new ClassBreaksDefinition()
                {
                    BreakCount = 5,
                    ClassificationField = ((ComboBoxItem)comboField.SelectedItem).Tag as string,
                    ClassificationMethod = ClassificationMethod.Quantile,
                    BaseSymbol = _baseSymbol,
                    ColorRamps = new ObservableCollection<ColorRamp>() { colorRamp }
                };

                var param = new GenerateRendererParameters()
                {
                    ClassificationDefinition = classBreaksDef,
                    Where = ((ServiceFeatureTable)featureLayer.FeatureTable).Where
                };

                var result = await generateRenderer.GenerateRendererAsync(param);

                featureLayer.Renderer = result.Renderer;

                // Reset the legend
                txtLegendTitle.DataContext = comboField.SelectedValue.ToString();
                await CreateLegend((ClassBreaksRenderer)result.Renderer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Generate Renderer Task Sample");
            }
        }

        // Create a legend from the class breaks renderer
        private async Task CreateLegend(ClassBreaksRenderer renderer)
        {
            var tasks = renderer.Infos.Select(info => info.Symbol.CreateSwatchAsync());
            var images = await Task.WhenAll(tasks);

            listLegend.ItemsSource = renderer.Infos
                .Select((info, idx) => new ClassBreakLegendItem() { SymbolImage = images[idx], Label = info.Label });
        }
    }

    // class for the legend items
    internal class ClassBreakLegendItem
    {
        public ImageSource SymbolImage { get; set; }
        public string Label { get; set; }
    }
}
Feedback on this topic?