ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.CIM Namespace / CIMVisualVariable Class
Members Example

In This Topic
    CIMVisualVariable Class
    In This Topic
    Represents a visual variable.
    Object Model
    CIMVisualVariable ClassCIMVisualVariableAuthoringInfo Class
    Syntax
    Example
    Evaluating Arcade Visual Variable Expressions on a Renderer
    {
      //Consult https://github.com/Esri/arcade-expressions/ and
      //https://developers.arcgis.com/arcade/ for more examples
      //and arcade reference
    
      // Note: the following should be embedded in a QueuedTask.Run() statement
      {
        //Assume we have a layer that is using Visual Variable
        //expressions that we want to evaluate interactively...
        var def = featureLayer.GetDefinition() as CIMFeatureLayer;
    
        //Most all feature renderers have a VisualVariable collection
        var renderer = def.Renderer as CIMUniqueValueRenderer;
        List<CIMVisualVariable> vis_variables = renderer.VisualVariables?.ToList() ??
                              [];
        if (vis_variables.Count == 0)
          return;//there are none
        var vis_var_with_expr = new Dictionary<string, string>();
        //see if any are using expressions
        foreach (var vv in vis_variables)
        {
          if (vv is CIMColorVisualVariable cvv)
          {
            if (!string.IsNullOrEmpty(cvv.ValueExpressionInfo?.Expression))
              vis_var_with_expr.Add("Color", cvv.ValueExpressionInfo?.Expression);
          }
          else if (vv is CIMTransparencyVisualVariable tvv)
          {
            if (!string.IsNullOrEmpty(tvv.ValueExpressionInfo?.Expression))
              vis_var_with_expr.Add("Transparency", tvv.ValueExpressionInfo?.Expression);
          }
          else if (vv is CIMSizeVisualVariable svv)
          {
            if (!string.IsNullOrEmpty(svv.ValueExpressionInfo?.Expression))
              vis_var_with_expr.Add("Outline", svv.ValueExpressionInfo?.Expression);
          }
        }
        if (vis_var_with_expr.Count == 0)
          return;//there aren't any with expressions
    
        //loop through the features (outer)
        //per feature evaluate each visual variable.... (inner)
        //....
        //the converse is to loop through the expressions (outer)
        //then per feature evaluate the expression (inner)
        using RowCursor rc = featureLayer.Search();
        while (rc.MoveNext())
        {
          foreach (var kvp in vis_var_with_expr)
          {
            var expr_info = new CIMExpressionInfo()
            {
              Expression = kvp.Value,
              ReturnType = ExpressionReturnType.Default
            };
            //per feature eval each expression...
            using ArcadeEvaluator arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
                                        expr_info, ArcadeProfile.Visualization);
    
            var variables = new List<KeyValuePair<string, object>>() {
                new("$feature", rc.Current)
              };
            //note 2D maps can also have view scale...
            //...if necessary...
            if (mv.ViewingMode == MapViewingMode.Map)
            {
              variables.Add(new("$view.scale", mv.Camera.Scale));
            }
            var result = arcade.Evaluate(variables).GetResult().ToString();
            //output
            System.Diagnostics.Debug.WriteLine(
               $"[{rc.Current.GetObjectID()}] '{kvp.Key}': {result}");
          }
        }
      }
    }
    Class Breaks renderer with graduated colors and outline
    /// <summary>
    /// Renders a feature layer using graduated colors to draw quantities. The outline width is varied based on attributes.
    /// ![graduatedColorOutline.png](https://ArcGIS.github.io/arcgis-pro-sdk/images/Renderers/graduatedColorOutline.png "Graduated colors with natural breaks renderer.") 
    /// </summary>
    {
      //Check feature layer name
      //Code works with the USDemographics feature layer available with the ArcGIS Pro SDK Sample data
      var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(f => f.Name == "USDemographics");
      if (featureLayer == null)
      {
        MessageBox.Show("This renderer works with the USDemographics feature layer available with the ArcGIS Pro SDK Sample data", "Data missing");
      }
      //Gets the first numeric field of the feature layer
      var firstNumericFieldOfFeatureLayer = "NumericFieldInFeatureLayer";
      //Gets the min and max value of the field
      var minMax = GetFieldMinMax(featureLayer, firstNumericFieldOfFeatureLayer);
      GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
      {
        ClassificationField = "NumericFieldInFeatureLayer",
        ClassificationMethod = ClassificationMethod.NaturalBreaks,
        BreakCount = 5,
        ColorRamp = colorRamp
      };
      CIMClassBreaksRenderer renderer = (CIMClassBreaksRenderer)featureLayer.CreateRenderer(gcDef);
      //Create array of CIMVisualVariables to hold the outline information.
      var visualVariables = new CIMVisualVariable[] {
                  new CIMSizeVisualVariable
                  {
                      ValueExpressionInfo = new CIMExpressionInfo
                      {
                         Title = "Custom",
                         Expression = "$feature.AREA",
                         ReturnType = ExpressionReturnType.Default
                      },
                      AuthoringInfo = new CIMVisualVariableAuthoringInfo
                      {
                          MinSliderValue = Convert.ToDouble(minMax.Item1),
                          MaxSliderValue = Convert.ToDouble(minMax.Item2),
                          ShowLegend = false,
                          Heading = firstNumericFieldOfFeatureLayer
                      },
                      VariableType = SizeVisualVariableType.Graduated,
                      Target = "outline",
                      MinSize = 1,
                      MaxSize = 13,
                      MinValue = Convert.ToDouble(minMax.Item1),
                      MaxValue = Convert.ToDouble(minMax.Item2)
                  },
    
              };
      renderer.VisualVariables = visualVariables;
      //Note: Run within QueuedTask
      featureLayer?.SetRenderer(renderer);
    }
    Inheritance Hierarchy
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also