-
Notifications
You must be signed in to change notification settings - Fork 117
ProSnippets Charts
UmaHarano edited this page May 6, 2024
·
3 revisions
Language: C#
Subject: Charts
Contributor: ArcGIS Pro SDK Team <arcgisprosdk@esri.com>
Organization: esri, http://www.esri.com
Date: 4/22/2024
ArcGIS Pro: 3.3
Visual Studio: 2022
.NET Target Framework: .Net 6
// 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);
// 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 dateField = "last_review";
const string numericField = "price";
var lyrsLine = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
var lyrLine = lyrsLine.First();
var lyrDefLine = lyrLine.GetDefinition();
// Define line chart CIM properties
var lineChart = 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 field
Fields = new string[] { dateField, numericField },
// Specify aggregation type
FieldAggregation = new string[] { string.Empty, "SUM" },
// Specify custom time bin of 6 months
TimeAggregationType = ChartTimeAggregationType.EqualIntervalsFromStartTime,
TimeIntervalSize = 6.0,
TimeIntervalUnits = esriTimeUnits.esriTimeUnitsMonths,
// NOTE: When setting custom time binning, be sure to set CalculateAutomaticTimeInterval = false
CalculateAutomaticTimeInterval = false,
// Define custom line color
ColorType = 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)
var newChartsLine = new CIMChart[] { lineChart };
var allChartsLine = (lyrDefLine == null) ? newChartsLine : lyrDefLine.Charts.Concat(newChartsLine);
// Add CIM chart to layer defintion
lyrDefLine.Charts = allChartsLine.ToArray();
lyrLine.SetDefinition(lyrDefLine);
// 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}",
UseAutomaticTitle = false
},
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);
// 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 categoryField = "neighbourhood_group";
const string splitByField = "room_type";
var lyrsBar = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
var lyrBar = lyrsBar.First();
var lyrDefBar = lyrBar.GetDefinition();
// Get unique values for `splitByField`
var values = new List<string>();
using (RowCursor cursor = lyrBar.Search())
{
while (cursor.MoveNext())
{
var value = Convert.ToString(cursor.Current[splitByField]);
values.Add(value);
}
};
var uniqueValues = values.Distinct().ToList();
// Define bar chart CIM properties
var barChart = new CIMChart
{
Name = "barChart",
GeneralProperties = new CIMChartGeneralProperties
{
Title = $"{categoryField} grouped by {splitByField}",
UseAutomaticTitle = false
},
};
// Create list to store the info for each chart series
var allSeries = new List<CIMChartSeries>();
// Create a series for each unique category
foreach (var value in uniqueValues)
{
var series = new CIMChartBarSeries
{
UniqueName = value,
Name = value,
// Specify the category field
Fields = new string[] { categoryField, string.Empty },
// Specify the WhereClause to filter a series by unique value
WhereClause = $"{splitByField} = '{value}'",
GroupFields = new[] { categoryField },
// Specify aggregation type
FieldAggregation = new string[] { string.Empty, "COUNT" }
};
allSeries.Add(series);
}
barChart.Series = allSeries.ToArray();
// Add new chart to layer's existing list of charts (if any exist)
var newChartsBar = new CIMChart[] { barChart };
var allChartsBar = (lyrDefBar == null) ? newChartsBar : lyrDefBar.Charts.Concat(newChartsBar);
// Add CIM chart to layer defintion
lyrDefBar.Charts = allChartsBar.ToArray();
lyrBar.SetDefinition(lyrDefBar);
Home | API Reference | Requirements | Download | Samples