The Chart class defines an ArcGIS Pro chart. The class allows you to create different types of charts, including bar charts, line charts, scatter plots, scatter plot matrices, QQ plots, histograms, box plots, and data clocks. The class can also be used to define the chart title, axes, and other properties.
Charts assist in presenting information about map features and the relationship between them in a visual manner. Charts may also be created for nonspatial tables and can show additional information about the features on the map or the same information in a different way. Charts are complementary to maps in that they visually convey information that would generally be summarized numerically or explored from tables. With a chart, you can quickly compare features to gain insight into the functional relationship between the features and thereby visualize the distribution, trends, and patterns in the data that would otherwise be difficult to see.
Charts can be created using a Chart object and added to a layer or table in an ArcGIS Pro project. To add a chart to a layer in a project, create a Chart object, configure its properties, and associate the Chart object with an arcpy.mp.Layer from an arcpy.mp.ArcGISProject. A chart made in this way can be opened from the Charts section under the layer in the Contents pane when that project is opened in ArcGIS Pro.
The Chart object has a dataSource property that you can use to create a chart for data sources including various feature class and table formats and web layers (feature services). A chart can be exported to an .svg file (.svg) for visualization. Chart objects support rich representation in Notebooks and can be visualized graphically.
The name of the chart. A chart must have a unique name per layer. This name is only used for identification; it is not displayed.
Sets additional properties that apply to bar charts.
Sets additional properties that apply to box plots.
Sets additional properties that apply to calendar heat charts.
Sets additional properties that apply to data clocks.
Sets the data source of the chart. When a chart is exported using the exportToSVG method or displayed in an ArcGIS Notebook, the data source is read and rendered on the chart. Valid data sources include paths to datasets, including local datasets, UNC paths, and service URLs, and arcpy.mp Layer objects.
Sets the description of the chart. The description text appears at the bottom of the chart view.
Sets additional properties that apply to histograms.
Sets properties of the chart legend.
Sets additional properties that apply to line charts.
Sets additional properties that apply to QQ plots.
Sets additional properties that apply to scatter plots.
Sets additional properties that apply to scatter plot matrices.
Sets the title of the chart. The title text appears at the top of the chart view and is used as the label in the Contents pane on the List By Drawing Order tab .
Sets the type of chart to create. Valid options include the following:
Sets properties of the x-axis.
Sets properties of the y-axis.
Adds the chart class to a layer.
|exportToSVG (path, width, height)|
Exports the chart to SVG format.
Updates chart properties to sync changes between the class and the chart previously added to a layer.
The chart will be added to this target Layer object.
The Chart class will be unchanged, but the class' properties will be set into the layer definition and a new chart will be created.
After defining chart properties in the Chart class, the final step typically is to add the chart to a layer. Use the addToLayer method to accomplish this.
Set chart properties and add the chart to an existing layer.
import arcpy aprx = arcpy.mp.ArcGISProject("current") map = aprx.listMaps() censusLayer = map.listLayers('Census Block Groups') c = arcpy.Chart('MyChart') c.type = 'bar' c.title = 'Population by State' c.xAxis.field = 'StateName' c.yAxis.field = 'Population' c.bar.aggregation = 'SUM' c.xAxis.title = 'State' c.yAxis.title = 'Total Population' c.addToLayer(censusLayer)
exportToSVG (path, width, height)
The path where the chart will be exported in SVG format.
The width of the output graphic.
The height of the output graphic.
In some cases, you may wish to save the chart as a graphic that can be shared and viewed outside of ArcGIS Pro. Exporting to the SVG graphic format is beneficial, as the chart elements and text are stored as vector elements that can be independently modified in a vector graphics software. An SVG graphic can also be resized to any scale without pixelation or loss in quality.
Create a chart based on a project layer and export to a local disk.
import arcpy aprx = arcpy.mp.ArcGISProject('current') censusLayer = aprx.listMaps().listLayers('Census Block Groups') c = arcpy.Chart('Population By State') c.type = 'bar' c.title = 'Population by State' c.xAxis.field = 'StateName' c.xAxis.title = 'State' c.yAxis.field = 'Population' c.yAxis.title = 'Total Population' c.bar.aggregation = 'SUM' # Set data source to a layer within current project c.dataSource = censusLayer # Save the chart to file with dimensions width=500, height=500 c.exportToSVG('populationByState.svg', 500, 500)
Create a chart based on a feature service layer and export to a local disk.
import arcpy featureServiceURL = r'https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0' c = arcpy.Chart('Chicago Thefts') c.type = 'bar' c.title = 'Chicago Thefts by Police Beat' c.xAxis.field = 'BEAT' c.bar.aggregation = 'COUNT' # Set data source to a feature service URL c.dataSource = featureServiceURL # Save the chart to file with dimensions width=800, height=600 c.exportToSVG('theftsPerBeat.svg', 800, 600)
The Chart class is not modified in any way. Rather the settings from the class are synced to the chart previously added to a layer.
After defining chart properties in the Chart class, the final step is typically to add the chart to a layer. Use the addToLayer method to accomplish this. However, you may wish to further modify the chart properties. Instead of starting from scratch with a new chart, you can modify the properties of the original Chart class, then use updateChart to sync any changes into the chart added to the layer. This will allow the changes you make to be presented in the Chart properties pane and chart view.
Use updateChart to sync chart property changes into a layer.
chart.addToLayer(myLayer) # Further modification is necessary chart.description = "Data from the U.S. Census Bureau" chart.updateChart()
Add a Chart object to an arcpy.mp Layer object to add the chart to the layer.
import arcpy aprx = arcpy.mp.ArcGISProject("current") map = aprx.listMaps() censusLayer = map.listLayers('Census Block Groups') c = arcpy.Chart('MyChart') c.type = 'scatter' c.title = 'Relationship between Percent Vacant (Housing) and Population Density' c.description = 'This chart examines the relationship between housing vacancy and population density.' c.xAxis.field = 'Per_Vacant' c.yAxis.field = 'Pop_Density' c.xAxis.title = 'Vacant Housing %' c.yAxis.title = 'Population Density (per Sq. Mile)' c.addToLayer(censusLayer)