// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.md// Define fields names used in chart parameters.conststringxField="minimum_nights";conststringyField="price";varlyrsScatter= MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();varlyrScatter= lyrsScatter.First();varlyrDefScatter= lyrScatter.GetDefinition();// Define scatter plot CIM propertiesvarscatterPlot=new CIMChart
{Name="scatterPlot",GeneralProperties=new CIMChartGeneralProperties
{Title=$"{xField} vs. {yField}",UseAutomaticTitle=false},Series=new CIMChartSeries[]{new CIMChartScatterSeries {UniqueName="scatterPlotSeries",Name="scatterPlotSeries",// Specify the X and Y field namesFields=newstring[]{ xField , yField },// Turn on trend lineShowTrendLine=true}}};// Add new chart to layer's existing list of charts (if any exist)varnewChartsScatter=new CIMChart[]{ scatterPlot };varallChartsScatter=(lyrDefScatter==null)?newChartsScatter: lyrDefScatter.Charts.Concat(newChartsScatter);// Add CIM chart to layer defintion
lyrDefScatter.Charts = allChartsScatter.ToArray();
lyrScatter.SetDefinition(lyrDefScatter);
Create a line chart with custom time binning and style
// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.md// Define fields names used in chart parameters.conststringdateField="last_review";conststringnumericField="price";varlyrsLine= MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();varlyrLine= lyrsLine.First();varlyrDefLine= lyrLine.GetDefinition();// Define line chart CIM propertiesvarlineChart=new CIMChart
{Name="lineChart",GeneralProperties=new CIMChartGeneralProperties
{Title=$"Line chart for {dateField} summarized by {numericField}",UseAutomaticTitle=false},Series=new CIMChartSeries[]{new CIMChartLineSeries {UniqueName="lineChartSeries",Name=$"Sum({numericField})",// Specify date field and numeric fieldFields=newstring[]{ dateField, numericField },// Specify aggregation typeFieldAggregation=newstring[]{string.Empty,"SUM"},// Specify custom time bin of 6 monthsTimeAggregationType= ChartTimeAggregationType.EqualIntervalsFromStartTime,TimeIntervalSize=6.0,TimeIntervalUnits= esriTimeUnits.esriTimeUnitsMonths,// NOTE: When setting custom time binning, be sure to set CalculateAutomaticTimeInterval = falseCalculateAutomaticTimeInterval=false,// Define custom line colorColorType= ChartColorType.CustomColor,LineSymbolProperties=new CIMChartLineSymbolProperties {Style= ChartLineDashStyle.DashDot,Color=new CIMRGBColor {R=0,G=150,B=20},},MarkerSymbolProperties=new CIMChartMarkerSymbolProperties
{Color=new CIMRGBColor {R=0,G=150,B=20}}},}};// Add new chart to layer's existing list of charts (if any exist)varnewChartsLine=new CIMChart[]{ lineChart };varallChartsLine=(lyrDefLine==null)?newChartsLine: lyrDefLine.Charts.Concat(newChartsLine);// Add CIM chart to layer defintion
lyrDefLine.Charts = allChartsLine.ToArray();
lyrLine.SetDefinition(lyrDefLine);
Create a histogram for every field of type Double
// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.mdvarlyrsHistogram= MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();varlyrHistogram= lyrsHistogram.First();varlyrDefHistogram= lyrHistogram.GetDefinition();// Get list names for fields of type DoublevardoubleFields= lyrHistogram.GetFieldDescriptions().Where(f => f.Type == FieldType.Double).Select(f => f.Name);// Create list that will contain all histogramsvarhistograms=newList<CIMChart>();// Create histogram for each Double fieldforeach(var field in doubleFields){// Define histogram CIM propertiesvarhistogram=new CIMChart
{Name=$"histogram_{field}",GeneralProperties=new CIMChartGeneralProperties
{Title=$"Histogram for {field}",UseAutomaticTitle=false},Series=new CIMChartSeries[]{new CIMChartHistogramSeries {UniqueName="histogramSeries",Name=$"histogram_{field}",BinCount=15,// Specify the number fieldFields=newstring[]{ field },}}};
histograms.Add(histogram);};// Add new chart to layer's existing list of charts (if any exist)varallChartsHistogram=(lyrDefHistogram==null)?histograms: lyrDefHistogram.Charts.Concat(histograms);// Add CIM chart to layer defintion
lyrDefHistogram.Charts = allChartsHistogram.ToArray();
lyrHistogram.SetDefinition(lyrDefHistogram);
Create a multiseries bar chart
// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.md// Define fields names used in chart parameters.conststringcategoryField="neighbourhood_group";conststringsplitByField="room_type";varlyrsBar= MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();varlyrBar= lyrsBar.First();varlyrDefBar= lyrBar.GetDefinition();// Get unique values for `splitByField`varvalues=newList<string>();using(RowCursorcursor= lyrBar.Search()){while(cursor.MoveNext()){varvalue= Convert.ToString(cursor.Current[splitByField]);
values.Add(value);}};varuniqueValues= values.Distinct().ToList();// Define bar chart CIM propertiesvarbarChart=new CIMChart
{Name="barChart",GeneralProperties=new CIMChartGeneralProperties
{Title=$"{categoryField} grouped by {splitByField}",UseAutomaticTitle=false},};// Create list to store the info for each chart seriesvarallSeries=newList<CIMChartSeries>();// Create a series for each unique categoryforeach(var value in uniqueValues){varseries=new CIMChartBarSeries
{UniqueName=value,Name=value,// Specify the category fieldFields=newstring[]{ categoryField,string.Empty },// Specify the WhereClause to filter a series by unique valueWhereClause=$"{splitByField} = '{value}'",GroupFields=new[]{ categoryField },// Specify aggregation typeFieldAggregation=newstring[]{string.Empty,"COUNT"}};
allSeries.Add(series);}
barChart.Series = allSeries.ToArray();// Add new chart to layer's existing list of charts (if any exist)varnewChartsBar=new CIMChart[]{ barChart };varallChartsBar=(lyrDefBar==null)?newChartsBar: lyrDefBar.Charts.Concat(newChartsBar);// Add CIM chart to layer defintion
lyrDefBar.Charts = allChartsBar.ToArray();
lyrBar.SetDefinition(lyrDefBar);
Create a pie chart with custom legend formatting
// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.md// Define fields names used in chart parameters.conststringfieldCategory="neighbourhood_group";varlyrs= MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();varlyr= lyrs.First();varlyrDef= lyr.GetDefinition();// Define pie chart CIM propertiesvarpieChart=new CIMChart
{Name="pieChart",GeneralProperties=new CIMChartGeneralProperties
{Title="Pie chart with custom legend formatting",UseAutomaticTitle=true},Legend=new CIMChartLegend
{LegendText=new CIMChartTextProperties
{FontFillColor=new CIMRGBColor {R=0,G=250,B=20},// Specify new font colorFontSize=6.0,// Specify new font size}},Series=new CIMChartSeries[]{new CIMChartPieSeries
{UniqueName="pieSeries",Name="pieSeries",Fields=newstring[]{ fieldCategory,string.Empty }}}};// Add new chart to layer's existing list of charts (if any exist)varnewCharts=new CIMChart[]{ pieChart };varallCharts=(lyrDef?.Charts ==null)?newCharts: lyrDef.Charts.Concat(newCharts);// Add CIM chart to layer defintion
lyrDef.Charts = allCharts.ToArray();
lyr.SetDefinition(lyrDef);