ProSnippets Renderer - kataya/arcgis-pro-sdk GitHub Wiki
All ProSnippets listed here are also used by the following sample code: Renderer sample code
Language: C#
Subject: Map-Authoring
Contributor: ArcGIS Pro SDK Team <[email protected]>
Organization: esri, http://www.esri.com
Date: 12/11/2020
ArcGIS Pro: 2.7
Visual Studio: 2017, 2019
.NET Target Framework: 4.8
Renders a feature layer using Pie chart symbols to represent data
internal static Task PieChartRendererAsync()
{
//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");
return Task.FromResult(0);
}
return QueuedTask.Run(() =>
{
//Fields to use for the pie chart slices
var chartFields = new string[]
{
"WHITE10",
"BLACK10",
"AMERIND10",
"ASIAN10",
"HISPPOP10",
"HAMERIND10",
"HWHITE10",
"HASIAN10",
"HPACIFIC10",
"HBLACK10",
"HOTHRACE10"
};
PieChartRendererDefinition pieChartRendererDefn = new PieChartRendererDefinition()
{
ChartFields = chartFields,
ColorRamp = SDKHelpers.GetColorRamp(),
SizeOption = PieChartSizeOptions.Field,
FieldName = "BLACK10",
FixedSize = 36.89,
DisplayIn3D = true,
ShowOutline = true,
Orientation = PieChartOrientation.CounterClockwise,
};
//Creates a "Renderer"
var pieChartRenderer = featureLayer.CreateRenderer(pieChartRendererDefn);
//Sets the renderer to the feature layer
featureLayer.SetRenderer(pieChartRenderer);
});
}
Renders a feature layer using Bar chart symbols to represent data
internal static Task BarChartRendererAsync()
{
//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");
return Task.FromResult(0);
}
return QueuedTask.Run(() =>
{
var chartFields = new string[]
{
"WHITE10",
"BLACK10",
"AMERIND10",
"ASIAN10",
"HISPPOP10",
"HAMERIND10",
"HWHITE10",
"HASIAN10",
"HPACIFIC10",
"HBLACK10",
"HOTHRACE10"
};
QueuedTask.Run(() =>
{
BarChartRendererDefinition barChartRendererDefn = new BarChartRendererDefinition()
{
ChartFields = chartFields,
BarWidth = 12,
BarSpacing = 1,
MaximumBarLength = 65,
Orientation = ChartOrientation.Vertical,
DisplayIn3D = true,
ShowAxes = true,
ColorRamp = SDKHelpers.GetColorRamp()
};
//Creates a "Renderer"
var barChartChartRenderer = featureLayer.CreateRenderer(barChartRendererDefn);
//Sets the renderer to the feature layer
featureLayer.SetRenderer(barChartChartRenderer);
});
});
}
Renders a feature layer using stacked bar chart symbols to represent data
internal static Task StackedBarChartRendererAsync()
{
//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");
return Task.FromResult(0);
}
return QueuedTask.Run(() =>
{
var chartFields = new string[]
{
"WHITE10",
"BLACK10",
"AMERIND10",
"ASIAN10",
"HISPPOP10",
"HAMERIND10",
"HWHITE10",
"HASIAN10",
"HPACIFIC10",
"HBLACK10",
"HOTHRACE10"
};
QueuedTask.Run(() =>
{
StackedChartRendererDefinition barChartpieChartRendererDefn = new StackedChartRendererDefinition()
{
ChartFields = chartFields,
SizeOption = StackChartSizeOptions.SumSelectedFields,
Orientation = ChartOrientation.Horizontal,
ShowOutline = true,
DisplayIn3D = true,
StackWidth = 8,
StackLength = 25.87,
ColorRamp = SDKHelpers.GetColorRamp()
};
//Creates a "Renderer"
var stackedBarChartChartRenderer = featureLayer.CreateRenderer(barChartpieChartRendererDefn);
//Sets the renderer to the feature layer
featureLayer.SetRenderer(stackedBarChartChartRenderer);
});
});
}
Renders a feature layer using graduated colors to draw quantities.
internal static Task CBRendererGraduatedColors(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
{
ClassificationField = SDKHelpers.GetNumericField(featureLayer),
ClassificationMethod = ClassificationMethod.NaturalBreaks,
BreakCount = 5,
ColorRamp = SDKHelpers.GetColorRamp(),
};
CIMClassBreaksRenderer renderer = (CIMClassBreaksRenderer)featureLayer.CreateRenderer(gcDef);
featureLayer?.SetRenderer(renderer);
});
}
Renders a feature layer using graduated colors to draw quantities. The outline width is varied based on attributes.
internal static Task CBRendererGraduatedColorsOutlineAsync(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Gets the first numeric field of the feature layer
var firstNumericFieldOfFeatureLayer = SDKHelpers.GetNumericField(featureLayer);
//Gets the min and max value of the field
var minMax = SDKHelpers.GetFieldMinMax(featureLayer, firstNumericFieldOfFeatureLayer);
GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
{
ClassificationField = SDKHelpers.GetNumericField(featureLayer),
ClassificationMethod = ClassificationMethod.NaturalBreaks,
BreakCount = 5,
ColorRamp = SDKHelpers.GetColorRamp()
};
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;
featureLayer?.SetRenderer(renderer);
});
}
Renders a feature layer using graduated symbols and natural breaks to draw quantities.
internal static Task CBRendererGraduatedSymbols(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
GraduatedSymbolsRendererDefinition gsDef = new GraduatedSymbolsRendererDefinition()
{
ClassificationField = SDKHelpers.GetNumericField(featureLayer), //getting the first numeric field
ClassificationMethod = ClassificationMethod.NaturalBreaks,
MinimumSymbolSize = 4,
MaximumSymbolSize = 50,
BreakCount = 5,
ColorRamp = SDKHelpers.GetColorRamp(), //getting a color ramp
};
CIMClassBreaksRenderer renderer = (CIMClassBreaksRenderer)featureLayer.CreateRenderer(gsDef);
featureLayer?.SetRenderer(renderer);
});
}
Renders a feature layer using an unclassed color gradient.
internal static Task UnclassedRenderer(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Gets the first numeric field of the feature layer
var firstNumericFieldOfFeatureLayer = SDKHelpers.GetNumericField(featureLayer);
//Gets the min and max value of the field
var labels = SDKHelpers.GetFieldMinMax(featureLayer, firstNumericFieldOfFeatureLayer);
UnclassedColorsRendererDefinition ucDef = new UnclassedColorsRendererDefinition()
{
Field = firstNumericFieldOfFeatureLayer,
ColorRamp = SDKHelpers.GetColorRamp(),
LowerColorStop = Convert.ToDouble(labels.Item1),
UpperColorStop = Convert.ToDouble(labels.Item2),
UpperLabel = labels.Item2,
LowerLabel = labels.Item1,
};
CIMClassBreaksRenderer renderer = (CIMClassBreaksRenderer)featureLayer.CreateRenderer(ucDef);
featureLayer?.SetRenderer(renderer);
});
}
Renders a feature layer using graduated colors and manual intervals to draw quantities.
internal static Task CBGraduatedColorsManualBreaks(FeatureLayer featureLayer)
{
//Change these class breaks to be appropriate for your data. These class breaks defined below apply to the US States feature class
List<CIMClassBreak> listClassBreaks = new List<CIMClassBreak>
{
new CIMClassBreak
{
Symbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.RedRGB).MakeSymbolReference(),
UpperBound = 24228
},
new CIMClassBreak
{
Symbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.GreenRGB).MakeSymbolReference(),
UpperBound = 67290
},
new CIMClassBreak
{
Symbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.BlueRGB).MakeSymbolReference(),
UpperBound = 121757
},
new CIMClassBreak
{
Symbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.GreyRGB).MakeSymbolReference(),
UpperBound = 264435
},
new CIMClassBreak
{
Symbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.WhiteRGB).MakeSymbolReference(),
UpperBound = 576594
}
};
return QueuedTask.Run(() =>
{
CIMClassBreaksRenderer cimClassBreakRenderer = new CIMClassBreaksRenderer
{
ClassBreakType = ClassBreakType.GraduatedColor,
ClassificationMethod = ClassificationMethod.Manual,
Field = SDKHelpers.GetNumericField(featureLayer),
//Important to add the Minimum break for your data to be classified correctly.
//This is vital especially if you have data with negative values.
//MinimumBreak =
Breaks = listClassBreaks.ToArray()
};
featureLayer?.SetRenderer(cimClassBreakRenderer);
});
}
Renders a point feature layer using a continuous color gradient to represent density of points.
internal static Task HeatMapRenderersAsync(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//defining a heatmap renderer that uses values from Population field as the weights
HeatMapRendererDefinition heatMapDef = new HeatMapRendererDefinition()
{
Radius = 20,
WeightField = SDKHelpers.GetNumericField(featureLayer),
ColorRamp = SDKHelpers.GetColorRamp(),
RendereringQuality = 8,
UpperLabel = "High Density",
LowerLabel = "Low Density"
};
CIMHeatMapRenderer heatMapRndr = (CIMHeatMapRenderer)featureLayer.CreateRenderer(heatMapDef);
featureLayer.SetRenderer(heatMapRndr);
});
}
Renders a feature layer using proportional symbols to draw quantities.
internal static Task ProportionalRendererAsync(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Gets the first numeric field of the feature layer
var firstNumericFieldOfFeatureLayer = SDKHelpers.GetNumericField(featureLayer);
//Gets the min and max value of the field
var sizes = SDKHelpers.GetFieldMinMax(featureLayer, firstNumericFieldOfFeatureLayer);
ProportionalRendererDefinition prDef = new ProportionalRendererDefinition()
{
Field = firstNumericFieldOfFeatureLayer,
MinimumSymbolSize = 4,
MaximumSymbolSize = 50,
LowerSizeStop = Convert.ToDouble(sizes.Item1),
UpperSizeStop = Convert.ToDouble(sizes.Item2)
};
CIMProportionalRenderer propRndr = (CIMProportionalRenderer)featureLayer.CreateRenderer(prDef);
featureLayer.SetRenderer(propRndr);
});
}
Renders a Polygon feature layer using a single symbol.
internal static Task SimpleRendererPolygon(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Creating a polygon with a red fill and blue outline.
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.Solid);
CIMPolygonSymbol fillWithOutline = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.CreateRGBColor(255, 190, 190), SimpleFillStyle.Solid, outline);
//Get the layer's current renderer
CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
//Update the symbol of the current simple renderer
renderer.Symbol = fillWithOutline.MakeSymbolReference();
//Update the feature layer renderer
featureLayer.SetRenderer(renderer);
});
}
Renders a Point feature layer using a single symbol.
internal static Task SimpleRendererPoint(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Create a circle marker
var pointSymbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.RedRGB, 8, SimpleMarkerStyle.Circle);
//Get the layer's current renderer
CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
//Update the symbol of the current simple renderer
renderer.Symbol = pointSymbol.MakeSymbolReference();
//Update the feature layer renderer
featureLayer.SetRenderer(renderer);
});
}
Renders a Line feature layer using a single symbol.
internal static Task SimpleRendererLine(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Create a circle marker
var lineSymbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.RedRGB, 2, SimpleLineStyle.DashDotDot);
//();
//Get the layer's current renderer
CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
//Update the symbol of the current simple renderer
renderer.Symbol = lineSymbol.MakeSymbolReference();
//Update the feature layer renderer
featureLayer.SetRenderer(renderer);
});
}
Renders a feature layer using unique values from one or multiple fields
internal static Task UniqueValueRendererAsync(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//construct unique value renderer definition
UniqueValueRendererDefinition uvr = new
UniqueValueRendererDefinition()
{
ValueFields = new string[] { SDKHelpers.GetDisplayField(featureLayer) }, //multiple fields in the array if needed.
ColorRamp = SDKHelpers.GetColorRamp(), //Specify color ramp
};
//Creates a "Renderer"
var cimRenderer = featureLayer.CreateRenderer(uvr);
//Sets the renderer to the feature layer
featureLayer.SetRenderer(cimRenderer);
});
}