// 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=newCIMChart{Name="scatterPlot",GeneralProperties=newCIMChartGeneralProperties{Title=$"{xField} vs. {yField}",UseAutomaticTitle=false},Series=newCIMChartSeries[]{newCIMChartScatterSeries{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=newCIMChart[]{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=newCIMChart{Name="lineChart",GeneralProperties=newCIMChartGeneralProperties{Title=$"Line chart for {dateField} summarized by {numericField}",UseAutomaticTitle=false},Series=newCIMChartSeries[]{newCIMChartLineSeries{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=newCIMChartLineSymbolProperties{Style=ChartLineDashStyle.DashDot,Color=newCIMRGBColor{R=0,G=150,B=20},},MarkerSymbolProperties=newCIMChartMarkerSymbolProperties{Color=newCIMRGBColor{R=0,G=150,B=20}}},}};// Add new chart to layer's existing list of charts (if any exist)varnewChartsLine=newCIMChart[]{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(varfieldindoubleFields){// Define histogram CIM propertiesvarhistogram=newCIMChart{Name=$"histogram_{field}",GeneralProperties=newCIMChartGeneralProperties{Title=$"Histogram for {field}",UseAutomaticTitle=false},Series=newCIMChartSeries[]{newCIMChartHistogramSeries{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=newCIMChart{Name="barChart",GeneralProperties=newCIMChartGeneralProperties{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(varvalueinuniqueValues){varseries=newCIMChartBarSeries{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=newCIMChart[]{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=newCIMChart{Name="pieChart",GeneralProperties=newCIMChartGeneralProperties{Title="Pie chart with custom legend formatting",UseAutomaticTitle=true},Legend=newCIMChartLegend{LegendText=newCIMChartTextProperties{FontFillColor=newCIMRGBColor{R=0,G=250,B=20},// Specify new font colorFontSize=6.0,// Specify new font size}},Series=newCIMChartSeries[]{newCIMChartPieSeries{UniqueName="pieSeries",Name="pieSeries",Fields=newstring[]{fieldCategory,string.Empty}}}};// Add new chart to layer's existing list of charts (if any exist)varnewCharts=newCIMChart[]{pieChart};varallCharts=(lyrDef?.Charts==null)?newCharts:lyrDef.Charts.Concat(newCharts);// Add CIM chart to layer defintion lyrDef.Charts=allCharts.ToArray();lyr.SetDefinition(lyrDef);