// 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.
const string xField = "minimum_nights";
const string yField = "price";
var lyrsScatter = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
var lyrScatter = lyrsScatter.First();
var lyrDefScatter = lyrScatter.GetDefinition();
// Define scatter plot CIM properties
var scatterPlot = 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 names
Fields = new string[] { xField , yField },
// Turn on trend line
ShowTrendLine = true
}
}
};
// Add new chart to layer's existing list of charts (if any exist)
var newChartsScatter = new CIMChart[] { scatterPlot };
var allChartsScatter = (lyrDefScatter == null) ? newChartsScatter : lyrDefScatter.Charts.Concat(newChartsScatter);
// Add CIM chart to layer defintion
lyrDefScatter.Charts = allChartsScatter.ToArray();
lyrScatter.SetDefinition(lyrDefScatter);