//Note: QueuedTask is required to access the feature layer{// For more information on the chart CIM specification:// https://github.com/Esri/cim-spec/blob/main/docs/v3/CIMCharts.mdvarlyrDefScatter=featureLayer.GetDefinition();// Define scatter plot CIM propertiesvarscatterPlot=newCIMChart{Name="scatterPlot",GeneralProperties=newCIMChartGeneralProperties{Title=$"{xField} vs. {yField}",UseAutomaticTitle=false},Series=[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 definition lyrDefScatter.Charts=allChartsScatter.ToArray();featureLayer.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// Note: QueuedTask is required to access the feature layer{varlyrDefLine=featureLayer.GetDefinition();// Define line chart CIM propertiesvarlineChart=newCIMChart{Name="lineChart",GeneralProperties=newCIMChartGeneralProperties{Title=$"Line chart for {dateField} summarized by {numericField}",UseAutomaticTitle=false},Series=[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 definitionlyrDefLine.Charts=allChartsLine.ToArray();featureLayer.SetDefinition(lyrDefLine);}
Create a bar chart
// Note: QueuedTask is required to access the feature layer{varlayerDefinition=featureLayer.GetDefinition();stringfieldXAxis="textField";stringfieldYAxis="NumericField";varmyBarChart=newCIMChart{Name="Bar chart",GeneralProperties=newCIMChartGeneralProperties{Title=$"{fieldXAxis} vs. {fieldYAxis}",UseAutomaticTitle=false},Series=[newCIMChartBarSeries{Name="Bar chart",UniqueName="Bar chart",Fields=newstring[]{fieldXAxis,fieldYAxis},//Create green fill symbolsFillSymbolProperties=newCIMChartFillSymbolProperties{Color=CIMColor.CreateRGBColor(38,115,0,70)}}]};// Add new chart to layer's existing list of charts (if any exist)varnewBarCharts=newCIMChart[]{myBarChart};varallChartsBar=(layerDefinition.Charts==null)?newBarCharts:layerDefinition.Charts.Concat(newBarCharts);// Add CIM chart to layer definition layerDefinition.Charts=allChartsBar.ToArray();featureLayer.SetDefinition(layerDefinition);}
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.mdvarlyrDefHistogram=featureLayer.GetDefinition();// Get list names for fields of type DoublevardoubleFields=featureLayer.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=[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 definition lyrDefHistogram.Charts=allChartsHistogram.ToArray();featureLayer.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.mdvarlyrDefBar=featureLayer.GetDefinition();// Get unique values for `splitByField`varvalues=newList<string>();using(RowCursorcursor=featureLayer.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};varallBarCharts=(lyrDefBar==null)?newChartsBar:lyrDefBar.Charts.Concat(newChartsBar);// Add CIM chart to layer definition lyrDefBar.Charts=allBarCharts.ToArray();featureLayer.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.mdvarlyrDef=featureLayer.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=[newCIMChartPieSeries{UniqueName="pieSeries",Name="pieSeries",Fields=newstring[]{categoryField,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 definition lyrDef.Charts=allCharts.ToArray();featureLayer.SetDefinition(lyrDef);