// For more information on the chart CIM specification:
// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.md
var lyrsHistogram = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
var lyrHistogram = lyrsHistogram.First();
var lyrDefHistogram = lyrHistogram.GetDefinition();
// Get list names for fields of type Double
var doubleFields = lyrHistogram.GetFieldDescriptions().Where(f => f.Type == FieldType.Double).Select(f => f.Name);
// Create list that will contain all histograms
var histograms = new List<CIMChart>();
// Create histogram for each Double field
foreach (var field in doubleFields)
{
// Define histogram CIM properties
var histogram = new CIMChart
{
Name = $"histogram_{field}",
GeneralProperties = new CIMChartGeneralProperties
{
Title = $"Histogram for {field}",
},
Series = new CIMChartSeries[]
{
new CIMChartHistogramSeries {
UniqueName = "histogramSeries",
Name = $"histogram_{field}",
BinCount = 15,
// Specify the number field
Fields = new string[] { field },
}
}
};
histograms.Add(histogram);
};
// Add new chart to layer's existing list of charts (if any exist)
var allChartsHistogram = (lyrDefHistogram == null) ? histograms : lyrDefHistogram.Charts.Concat(histograms);
// Add CIM chart to layer defintion
lyrDefHistogram.Charts = allChartsHistogram.ToArray();
lyrHistogram.SetDefinition(lyrDefHistogram);