ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / FeatureLayer Class / SetRenderer Method / SetRenderer(CIMRenderer) Method
Your custom renderer that will be used to draw features of the feature layer.
Example

In This Topic
    SetRenderer(CIMRenderer) Method
    In This Topic
    Specifies the feature layer's renderer object which determines how the layer draws. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetRenderer( 
       CIMRenderer renderer
    )
    Public Overloads Sub SetRenderer( _
       ByVal renderer As CIMRenderer _
    ) 

    Parameters

    renderer
    Your custom renderer that will be used to draw features of the feature layer.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Cannot set renderer for layer.
    Remarks
    Use CanSetRenderer to determine if the renderer is valid for the layer before updating it.
    Use FeatureLayer.CreateRenderer to create a new renderer.
    Example
    Modify renderer using Arcade
    var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(f => f.ShapeType == esriGeometryType.esriGeometryPolygon);
    if (lyr == null) return;
    QueuedTask.Run(() =>
    {
      // GetRenderer from Layer (assumes it is a unique value renderer)
      var uvRenderer = lyr.GetRenderer() as CIMUniqueValueRenderer;
      if (uvRenderer == null) return;
      //layer has STATE_NAME field
      //community sample Data\Admin\AdminSample.aprx
      string expression = "if ($view.scale > 21000000) { return $feature.STATE_NAME } else { return 'All' }";
      CIMExpressionInfo updatedExpressionInfo = new CIMExpressionInfo
      {
        Expression = expression,
        Title = "Custom" // can be any string used for UI purpose.
      };
      //set the renderer's expression
      uvRenderer.ValueExpressionInfo = updatedExpressionInfo;
    
      //SetRenderer on Layer
      lyr.SetRenderer(uvRenderer);
    });
    Modify label expression using Arcade
    var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(f => f.ShapeType == esriGeometryType.esriGeometryPolygon);
    if (lyr == null) return;
    QueuedTask.Run(() =>
    {
      //Get the layer's definition
      //community sample Data\Admin\AdminSample.aprx
      var lyrDefn = lyr.GetDefinition() as CIMFeatureLayer;
      if (lyrDefn == null) return;
      //Get the label classes - we need the first one
      var listLabelClasses = lyrDefn.LabelClasses.ToList();
      var theLabelClass = listLabelClasses.FirstOrDefault();
      //set the label class Expression to use the Arcade expression
      theLabelClass.Expression = "return $feature.STATE_NAME + TextFormatting.NewLine + $feature.POP2000;";
      //Set the label definition back to the layer.
      lyr.SetDefinition(lyrDefn);
    });
    
    How to set symbol for a feature layer symbolized with simple renderer
    public Task SetFeatureLayerSymbolAsync(FeatureLayer ftrLayer, CIMSymbol symbolToApply)
    {
      if (ftrLayer == null || symbolToApply == null)
        throw new System.ArgumentNullException();
    
      return QueuedTask.Run(() =>
      {
    
        //Get simple renderer from the feature layer
        CIMSimpleRenderer currentRenderer = ftrLayer.GetRenderer() as CIMSimpleRenderer;
        if (currentRenderer == null)
          return;
    
        //Set symbol's real world setting to be the same as that of the feature layer
        symbolToApply.SetRealWorldUnits(ftrLayer.UsesRealWorldSymbolSizes);
    
        //Update the symbol of the current simple renderer
        currentRenderer.Symbol = symbolToApply.MakeSymbolReference();
        //Update the feature layer renderer
        ftrLayer.SetRenderer(currentRenderer);
      });
    }
    
    How to apply a symbol from style to a feature layer
    public Task SetFeatureLayerSymbolFromStyleItemAsync(
               FeatureLayer ftrLayer, SymbolStyleItem symbolItem)
    {
      if (ftrLayer == null || symbolItem == null)
        throw new System.ArgumentNullException();
    
      return QueuedTask.Run(() =>
      {
        //Get simple renderer from the feature layer
        CIMSimpleRenderer currentRenderer = ftrLayer.GetRenderer() as CIMSimpleRenderer;
        if (currentRenderer == null)
          return;
        //Get symbol from the SymbolStyleItem
        CIMSymbol symbol = symbolItem.Symbol;
    
        //Set symbol's real world setting to be the same as that of the feature layer
        symbol.SetRealWorldUnits(ftrLayer.UsesRealWorldSymbolSizes);
    
        //Update the symbol of the current simple renderer
        currentRenderer.Symbol = symbol.MakeSymbolReference();
        //Update the feature layer renderer
        ftrLayer.SetRenderer(currentRenderer);
      });
    }
    
    How to apply a point symbol from a style to a feature layer
      // var map = MapView.Active.Map;
      // if (map == null)
      //        return;
      // var pointFeatureLayer =
      //       map.GetLayersAsFlattenedList()
      //          .OfType<FeatureLayer>()
      //         .Where(fl => fl.ShapeType == esriGeometryType.esriGeometryPoint);
      //   await ApplySymbolToFeatureLayerAsync(pointFeatureLayer.FirstOrDefault(), "Fire Station");
    
      public Task ApplySymbolToFeatureLayerAsync(FeatureLayer featureLayer, string symbolName)
      {
        return QueuedTask.Run(async () =>
        {
          //Get the ArcGIS 2D System style from the Project
          var arcGIS2DStyle =
    Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS 2D");
    
          //Search for the symbolName style items within the ArcGIS 2D style project item.
          var items = await QueuedTask.Run(() =>
          arcGIS2DStyle.SearchSymbols(StyleItemType.PointSymbol, symbolName));
    
          //Gets the CIMSymbol
          CIMSymbol symbol = items.FirstOrDefault().Symbol;
    
          //Get the renderer of the point feature layer
          CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
    
          //Set symbol's real world setting to be the same as that of the feature layer
          symbol.SetRealWorldUnits(featureLayer.UsesRealWorldSymbolSizes);
    
          //Apply the symbol to the feature layer's current renderer
          renderer.Symbol = symbol.MakeSymbolReference();
    
          //Appy the renderer to the feature layer
          featureLayer.SetRenderer(renderer);
        });
      }
    How to apply a color ramp from a style to a feature layer
    public async Task ApplyColorRampAsync(FeatureLayer featureLayer, List<string> fields)
    {
    
        StyleProjectItem style =
            Project.Current.GetItems<StyleProjectItem>()
                .FirstOrDefault(s => s.Name == "ColorBrewer Schemes (RGB)");
        if (style == null) return;
        var colorRampList = await QueuedTask.Run(() => 
                    style.SearchColorRamps("Red-Gray (10 Classes)"));
        if (colorRampList == null || colorRampList.Count == 0) return;
        CIMColorRamp cimColorRamp = null;
        CIMRenderer renderer = null;
        await QueuedTask.Run(() =>
        {
            cimColorRamp = colorRampList[0].ColorRamp;
            var rendererDef = new UniqueValueRendererDefinition(fields, null, cimColorRamp);
            renderer = featureLayer?.CreateRenderer(rendererDef);
            featureLayer?.SetRenderer(renderer);
        });
    
    }
    Apply Symbology to a layer from a Layer file
    //Note: Run within QueuedTask.Run
    //Get the Layer Document from the lyrx file
    var lyrDocFromLyrxFile = new LayerDocument(layerFile);
    var cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument();
    
    //Get the renderer from the layer file
    var rendererFromLayerFile = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMUniqueValueRenderer;
    
    //Apply the renderer to the feature layer
    //Note: If working with a raster layer, use the SetColorizer method.
    featureLayer?.SetRenderer(rendererFromLayerFile);
    Set unique value renderer to the selected feature layer of the active map
    await QueuedTask.Run(() =>
    {
      var fields = new List<string> { "Type" }; //field to be used to retrieve unique values
      CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(
          ColorFactory.Instance.GreenRGB, 16.0, SimpleMarkerStyle.Pushpin);  //constructing a point symbol as a template symbol
      CIMSymbolReference symbolPointTemplate = pointSym.MakeSymbolReference();
    
      //constructing renderer definition for unique value renderer
      UniqueValueRendererDefinition uniqueValueRendererDef =
    new UniqueValueRendererDefinition(fields, symbolPointTemplate);
    
      //creating a unique value renderer
      var flyr = MapView.Active.GetSelectedLayers()[0] as FeatureLayer;
      CIMUniqueValueRenderer uniqueValueRenderer = flyr.CreateRenderer(uniqueValueRendererDef) as CIMUniqueValueRenderer;
    
      //setting the renderer to the feature layer
      flyr.SetRenderer(uniqueValueRenderer);
    });
    Create a UniqueValueRenderer to specify symbols to values
    return QueuedTask.Run(() =>
    {
      //The goal is to construct the CIMUniqueValueRenderer which will be applied to the feature layer.
      // To do this, the following are the objects we need to set the renderer up with the fields and symbols.
      // As a reference, this is the USCities dataset. Snippet will create a unique value renderer that applies 
      // specific symbols to all the cities in California and Alabama.  The rest of the cities will use a default symbol.
    
      // First create a "CIMUniqueValueClass" for the cities in Alabama.
      List<CIMUniqueValue> listUniqueValuesAlabama = new List<CIMUniqueValue> { new CIMUniqueValue { FieldValues = new string[] { "Alabama" } } };
      CIMUniqueValueClass alabamaUniqueValueClass = new CIMUniqueValueClass
      {
        Editable = true,
        Label = "Alabama",
        Patch = PatchShape.Default,
        Symbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.RedRGB).MakeSymbolReference(),
        Visible = true,
        Values = listUniqueValuesAlabama.ToArray()
    
      };
      // Create a "CIMUniqueValueClass" for the cities in California.
      List<CIMUniqueValue> listUniqueValuescalifornia = new List<CIMUniqueValue> { new CIMUniqueValue { FieldValues = new string[] { "California" } } };
      CIMUniqueValueClass californiaUniqueValueClass = new CIMUniqueValueClass
      {
        Editable = true,
        Label = "California",
        Patch = PatchShape.Default,
        Symbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.BlueRGB).MakeSymbolReference(),
        Visible = true,
        Values = listUniqueValuescalifornia.ToArray()
      };
      //Create a list of the above two CIMUniqueValueClasses
      List<CIMUniqueValueClass> listUniqueValueClasses = new List<CIMUniqueValueClass>
        {
                      alabamaUniqueValueClass, californiaUniqueValueClass
        };
      //Create a list of CIMUniqueValueGroup
      CIMUniqueValueGroup uvg = new CIMUniqueValueGroup
      {
        Classes = listUniqueValueClasses.ToArray(),
      };
      List<CIMUniqueValueGroup> listUniqueValueGroups = new List<CIMUniqueValueGroup> { uvg };
      //Create the CIMUniqueValueRenderer
      CIMUniqueValueRenderer uvr = new CIMUniqueValueRenderer
      {
        UseDefaultSymbol = true,
        DefaultLabel = "all other values",
        DefaultSymbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreyRGB).MakeSymbolReference(),
        Groups = listUniqueValueGroups.ToArray(),
        Fields = new string[] { "STATE_NAME" }
      };
      //Set the feature layer's renderer.
      featureLayer.SetRenderer(uvr);
    });
    Create a Heatmap Renderer
    string colorBrewerSchemesName = "ArcGIS Colors";
    StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
    string colorRampName = "Heat Map 4 - Semitransparent";
    IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
    {
      return style.SearchColorRamps(colorRampName);
    });
    ColorRampStyleItem colorRamp = colorRampList[0];
    
    await QueuedTask.Run(() =>
    {
      //defining a heatmap renderer that uses values from Population field as the weights
      HeatMapRendererDefinition heatMapDef = new HeatMapRendererDefinition()
      {
        Radius = 20,
        WeightField = "Population",
        ColorRamp = colorRamp.ColorRamp,
        RendereringQuality = 8,
        UpperLabel = "High Density",
        LowerLabel = "Low Density"
      };
    
      FeatureLayer flyr = MapView.Active.Map.Layers[0] as FeatureLayer;
      CIMHeatMapRenderer heatMapRndr = flyr.CreateRenderer(heatMapDef) as CIMHeatMapRenderer;
      flyr.SetRenderer(heatMapRndr);
    });
    Create an Unclassed Renderer
    string colorBrewerSchemesName = "ArcGIS Colors";
    StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
    string colorRampName = "Heat Map 4 - Semitransparent";
    IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
    {
      return style.SearchColorRamps(colorRampName);
    });
    ColorRampStyleItem colorRamp = colorRampList[0];
    
    await QueuedTask.Run(() =>
    {
      CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreenRGB, 16.0, SimpleMarkerStyle.Diamond);
      CIMSymbolReference symbolPointTemplate = pointSym.MakeSymbolReference();
    
      //defining an unclassed renderer with custom upper and lower stops
      //all features with value >= 5,000,000 will be drawn with the upper color from the color ramp
      //all features with value <= 50,000 will be drawn with the lower color from the color ramp
      UnclassedColorsRendererDefinition unclassRndrDef = new UnclassedColorsRendererDefinition
                            ("Population", symbolPointTemplate, colorRamp.ColorRamp, "Highest", "Lowest", 5000000, 50000)
      {
    
        //drawing features with null values with a different symbol
        ShowNullValues = true,
        NullValueLabel = "Unknown"
      };
      CIMPointSymbol nullSym = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.RedRGB, 16.0, SimpleMarkerStyle.Circle);
      unclassRndrDef.NullValueSymbol = nullSym.MakeSymbolReference();
      FeatureLayer flyr = MapView.Active.Map.Layers[0] as FeatureLayer;
      CIMClassBreaksRenderer cbRndr = flyr.CreateRenderer(unclassRndrDef) as CIMClassBreaksRenderer;
      flyr.SetRenderer(cbRndr);
    });
    Create a Proportion Renderer with max and min symbol size capped
    string colorBrewerSchemesName = "ArcGIS Colors";
    StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
    string colorRampName = "Heat Map 4 - Semitransparent";
    IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
    {
      return style.SearchColorRamps(colorRampName);
    });
    ColorRampStyleItem colorRamp = colorRampList[0];
    
    await QueuedTask.Run(() =>
    {
      CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreenRGB, 1.0, SimpleMarkerStyle.Circle);
      CIMSymbolReference symbolPointTemplate = pointSym.MakeSymbolReference();
    
      //minimum symbol size is capped to 4 point while the maximum symbol size is set to 50 point
      ProportionalRendererDefinition prDef = new ProportionalRendererDefinition("POPULATION", symbolPointTemplate, 4, 50, true)
      {
    
        //setting upper and lower size stops to stop symbols growing or shrinking beyond those thresholds
        UpperSizeStop = 5000000,  //features with values >= 5,000,000 will be drawn with maximum symbol size
        LowerSizeStop = 50000    //features with values <= 50,000 will be drawn with minimum symbol size
      };
      FeatureLayer flyr = MapView.Active.Map.Layers[0] as FeatureLayer;
      CIMProportionalRenderer propRndr = flyr.CreateRenderer(prDef) as CIMProportionalRenderer;
      flyr.SetRenderer(propRndr);
    
    });
    Create a True Proportion Renderer
    string colorBrewerSchemesName = "ArcGIS Colors";
    StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
    string colorRampName = "Heat Map 4 - Semitransparent";
    IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
    {
      return style.SearchColorRamps(colorRampName);
    });
    ColorRampStyleItem colorRamp = colorRampList[0];
    
    await QueuedTask.Run(() =>
    {
      CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreenRGB, 1.0, SimpleMarkerStyle.Circle);
      CIMSymbolReference symbolPointTemplate = pointSym.MakeSymbolReference();
    
      //Defining proportional renderer where size of symbol will be same as its value in field used in the renderer.
      ProportionalRendererDefinition prDef = new ProportionalRendererDefinition("POPULATION", esriUnits.esriMeters, symbolPointTemplate, SymbolShapes.Square, ValueRepresentations.Radius);
    
      FeatureLayer flyr = MapView.Active.Map.Layers[0] as FeatureLayer;
      CIMProportionalRenderer propRndr = flyr.CreateRenderer(prDef) as CIMProportionalRenderer;
      flyr.SetRenderer(propRndr);
    
    });
    Setting a unique value renderer for latest observations
    var url = @"https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer";
    var uri = new Uri(url, UriKind.Absolute);
    //Must be on QueuedTask!
    
    var createParams = new FeatureLayerCreationParams(uri)
    {
      IsVisible = false
    };
    var streamLayer = LayerFactory.Instance.CreateLayer<StreamLayer>(
                        createParams, map);
    //Define the unique values by hand
    var uvr = new CIMUniqueValueRenderer()
    {
      Fields = new string[] { "ACTYPE" },
      UseDefaultSymbol = true,
      DefaultLabel = "Others",
      DefaultSymbol = SymbolFactory.Instance.ConstructPointSymbol(
                  CIMColor.CreateRGBColor(185, 185, 185), 8, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
    };
    
    var classes = new List<CIMUniqueValueClass>();
    //add in classes - one for ACTYPE of 727, one for DC 9
    classes.Add(
      new CIMUniqueValueClass() {
            Values = new CIMUniqueValue[] {
                  new CIMUniqueValue() { FieldValues = new string[] { "B727" } } },
            Visible = true,
            Label = "Boeing 727",
            Symbol = SymbolFactory.Instance.ConstructPointSymbol(
                  ColorFactory.Instance.RedRGB, 10, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
    });
    classes.Add(
      new CIMUniqueValueClass()
      {
        Values = new CIMUniqueValue[] {
                  new CIMUniqueValue() { FieldValues = new string[] { "DC9" } } },
        Visible = true,
        Label = "DC 9",
        Symbol = SymbolFactory.Instance.ConstructPointSymbol(
                  ColorFactory.Instance.GreenRGB, 10, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
      });
    //add the classes to a group
    var groups = new List<CIMUniqueValueGroup>()
    {
      new CIMUniqueValueGroup() {
         Classes = classes.ToArray()
      }
    };
    //add the groups to the renderer
    uvr.Groups = groups.ToArray();
    //Apply the renderer (for current observations)
    streamLayer.SetRenderer(uvr);
    streamLayer.SetVisibility(true);//turn on the layer
    
    Defining a unique value renderer definition
    var uvrDef = new UniqueValueRendererDefinition()
    {
      ValueFields = new List<string> { "ACTYPE" },
      SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol(
        ColorFactory.Instance.RedRGB, 10, SimpleMarkerStyle.Hexagon)
          .MakeSymbolReference(),
      ValuesLimit = 5
    };
    //Note: CreateRenderer can only create value classes based on
    //the current events it has received
    streamLayer.SetRenderer(streamLayer.CreateRenderer(uvrDef));
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also