Summary
Bar charts summarize and compare categorical data by using proportional bar lengths to represent values.
Discussion
When creating a chart object, optional arguments for the class constructor must be specified using the argument name; they cannot be specified by argument position. See the code sample section for an example of how to specify arguments using keywords.
Syntax
Bar (x, {y}, {aggregation}, {splitCategory}, {multiSeriesDisplay}, {showMovingAverage}, {movingAveragePeriod}, {rotated}, {title}, {description}, {xTitle}, {yTitle}, {dataSource}, {displaySize})
Parameter | Explanation | Data Type |
x | The field name for the x-axis variable. The field must be a category or a date field. | String |
y | The field names for the numeric fields. Specify a single field name or a list of field names. | String |
aggregation | The statistical calculation applied to values that occur at the same value along the x-axis. Supported statistics are COUNT, SUM, MEAN, MEDIAN, MIN, and MAX. No aggregation means that each value will be plotted on the chart regardless of overlap or recurring values. | String |
splitCategory | A second categorical field that adds a separate series or bar for each unique value in the field. | String |
multiSeriesDisplay | The display type for a bar chart with multiple series. Valid options are sideBySide, stacked, and stacked100. | String |
showMovingAverage | Indicates whether the moving average is displayed for temporal bar charts. (The default value is False) | Boolean |
movingAveragePeriod | The size of the moving average period. | Integer |
rotated | Indicates whether bars are viewed vertically or horizontally. Bars display vertically by default. True rotates bars horizontally. False displays bars vertically. (The default value is False) | Boolean |
title |
Sets the title of the chart. The title text appears at the top of the chart view and is used as the label for the chart in the Contents pane. | String |
description | Sets the description of the chart. The description text appears at the bottom of the chart view. | String |
xTitle | Sets the title for the x-axis of the chart. | String |
yTitle | Sets the title for the y-axis of the chart. | String |
dataSource | 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. | Object |
displaySize [displaySize,...] | Sets the size of the chart when exported using the exportToSVG method or displayed in an ArcGIS Notebook. The value must be specified as a two-item list, where the first item is the width of the chart and the second item is the height of the chart. | List |
Properties
Property | Explanation | Data Type |
aggregation (Read and Write) | The statistical calculation applied to values that occur at the same value along the x-axis. Supported statistics are COUNT, SUM, MEAN, MEDIAN, MIN, and MAX. No aggregation means that each value will be plotted on the chart regardless of overlap or recurring values. | String |
dataSource (Read and Write) | 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. | Object |
description (Read and Write) | Sets the description of the chart. The description text appears at the bottom of the chart view. | String |
displaySize (Read and Write) | Sets the size of the chart when exported using the exportToSVG method or displayed in an ArcGIS Notebook. The value must be specified as a two-item list, where the first item is the width of the chart and the second item is the height of the chart. | List |
legend (Read and Write) | Sets the properties of the chart legend.
| Object |
movingAveragePeriod (Read and Write) | The size of the moving average period. | Integer |
multiSeriesDisplay (Read and Write) | The display type for a bar chart with multiple series. Valid options are sideBySide, stacked, and stacked100. | Boolean |
rotated (Read and Write) | Indicates whether the chart is viewed vertically or horizontally. The chart displays vertically by default. True rotates the chart horizontally. False displays the chart vertically. | Boolean |
showMovingAverage (Read and Write) | Indicates whether the moving average is displayed for temporal bar charts. | Boolean |
splitCategory (Read and Write) | A second categorical field that adds a separate series or bar for each unique value in the field. | String |
title (Read and Write) | 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 . | String |
type (Read Only) | The string value indicating the chart type. | String |
x (Read and Write) | The field name for the x-axis variable. The field must be a category or a date field. | String |
xAxis (Read and Write) | Sets the properties of the x-axis.
| Object |
y (Read and Write) | The field name or names for the numeric field or fields. Must specify a single field name or a list of field names. | String |
yAxis (Read and Write) | Sets the properties of the y-axis.
| Object |
Method Overview
Method | Explanation |
addToLayer (layer_or_layerfile) | Adds the chart object to a layer or stand-alone table. |
exportToSVG (path, width, height) | Exports the chart to SVG format. |
updateChart () | Updates chart properties to sync changes between the object and the chart previously added to a layer. |
Methods
addToLayer (layer_or_layerfile)
Parameter | Explanation | Data Type |
layer_or_layerfile | The chart will be added to the target object. The layer_or_layerfile argument can be a Layer or a Table object. | Object |
Often the final step after defining chart properties is to add the chart object to a layer or table using the addToLayer method.
Add a chart to an existing layer.
import arcpy
aprx = arcpy.mp.ArcGISProject("current")
map = aprx.listMaps()[0]
censusLayer = map.listLayers('Census Block Groups')[0]
# Add chart object to a layer
chart.addToLayer(censusLayer)
exportToSVG (path, width, height)
Parameter | Explanation | Data Type |
path | The path where the chart will be exported in SVG format. | String |
width | The width of the output graphic. | Integer |
height | The height of the output graphic. | Integer |
In some cases, you may want 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.
Export a chart that has a project layer data source to an .svg file.
import arcpy
aprx = arcpy.mp.ArcGISProject('current')
censusLayer = aprx.listMaps()[0].listLayers('Census Block Groups')[0]
# Set data source of chart object to a layer within current project
chart.dataSource = censusLayer
# Save the chart to file with dimensions width=500, height=500
chart.exportToSVG('populationByState.svg', 500, 500)
Export a chart that has a feature service data source to an .svg file.
featureServiceURL = r'https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0'
# Set data source of chart object to a feature service URL
chart.dataSource = featureServiceURL
# Save the chart to file with dimensions width=800, height=600
chart.exportToSVG('theftsPerBeat.svg', 800, 600)
updateChart ()
Often the final step after defining chart properties is to add the chart object to a layer using the addToLayer method.
To further modify the chart properties, you can modify the properties of the original chart instead of starting from scratch with a new chart. You can then use the updateChart method to synchronize any changes into the chart that was added to the layer. This will allow the changes you make to be presented in the Chart properties pane and chart view.
Use the updateChart method to synchronize chart property changes into a layer.
chart.addToLayer(myLayer)
# Further modification is necessary
chart.description = "Data from the U.S. Census Bureau"
chart.updateChart()
Code sample
Create a bar chart using a feature service and export the chart to an .svg file.
import arcpy
url = r"https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0"
bar = arcpy.charts.Bar(x="BEAT", aggregation="count", title="Chicago Thefts by Beat", dataSource=url)
bar.exportToSVG('bar.svg', width=800, height=500)