Based on my understanding, setting the rotated property of a bar chart to True should draw the bars horizontally. Adding c.rotated to the code below still generates verticle bars. Is this functionality broken?
c = arcpy.Chart("chart1")
c.title = "Land cover in Gettysburg Battlefield"
c.rotated = True
c.type = "bar"
c.xAxis.field = "COVER"
c.bar.aggregation = "COUNT"
c.xAxis.title = "Land cover"
c.yAxis.title = "Number of polygons"
prj = arcpy.mp.ArcGISProject("CURRENT")
mxd = prj.listMaps()[0]
thelayer = mxd.listLayers()[0]
c.dataSource = thelayer
c
From this documentation page, here's the description of the bar chart rotated property:
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.
Solved! Go to Solution.
Okay, thanks. Your suggestions gave me some ideas that eventually led me to the solution. My first 2 tries were failures:
1) c = arcpy.Chart("chart1", rotated = True) gave me the following error:
TypeError Traceback (most recent call last)
In [25]:
Line 1: c = arcpy.Chart("chart1", rotated=True)
TypeError: __init__() got an unexpected keyword argument 'rotated'
(BTW, I also tested using rotate = True in case it's a typo in the documentation) But I guess the keyword arguments only work if you are calling it on the Bar chart object.
2) I added a c.updateChart() before the last line and got this exception:
Exception Traceback (most recent call last)
In [28]:
Line 15: c.updateChart()
File c:\program files\arcgis\pro\Resources\arcpy\arcpy\_chart.py, in updateChart:
Line 522: return convertArcObjectToPythonObject(self._arc_object.updateChart(*gp_fixargs((), True)))
Exception: Invalid layer or table.
---------------------------------------------------------------------------
3) Here's what worked: After setting type to bar, set the bar chart object to rotated (c.bar.rotated = True)
c = arcpy.Chart("chart1")
c.title = "Land cover in Gettysburg Battlefield"
c.type = "bar"
c.bar.rotated = True # Rotated is a bar property, not a chart property.
c.xAxis.field = "COVER"
c.bar.aggregation = "COUNT"
c.xAxis.title = "Land cover"
c.yAxis.title = "Number of polygons"
prj = arcpy.mp.ArcGISProject("CURRENT")
mxd = prj.listMaps()[0]
thelayer = mxd.listLayers()[0]
c.dataSource = thelayer
c
Bottom line? rotated is a bar property, not a chart property (since not all chart types can be rotated). This was confusing since all the other properties I used are both chart and bar properties.
By the way, I tried code similar to that example at the bottom of the page and got an error. This code generated the error you see below it.
bar = arcpy.chart.Bar(x="COVER",
aggregation="COUNT",
title="Land cover in Gettysburg Battlefield",
xTitle = "Land cover",
yTitle = "Number of polygons",
rotated=True,
dataSource=theLayer)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
In [42]:
Line 6: bar = arcpy.chart.Bar(x="COVER",
AttributeError: module 'arcpy' has no attribute 'chart'
---------------------------------------------------------------------------
Hi @LauraTateosian ,
Thanks for the great question! You are correct, since the `rotated` property is specific to bar charts, you set this property similar to how you set the `aggregation` property (with `c.bar.rotated` rather than `c.rotated`).
I also wanted to address the error you are seeing in the final code example from your follow-up post. In your initial post, you used the original arcpy charting interface to create the chart (documentation page). However, in the final code example of your follow-up post, you used the newer `arcpy.charts` module that was introduced in version 2.8 of Pro (documentation page). The module is called `arcpy.charts` rather than `arcpy.chart`, so it appears that this might have caused the error you are seeing. For instance, here's a bar chart created with the `arcpy.charts` module:
lyr = arcpy.mp.ArcGISProject("current").listMaps()[0].listLayers()[0]
# Note: using `arcpy.charts.Bar`
c = arcpy.charts.Bar(x="status", aggregation="COUNT", rotated=True,
title="Street Tree Status", xTitle="Tree Status",
yTitle="Number of Trees", dataSource=lyr)
c
For more information on the newer `arcpy.charts` module and how it differs from the original arcpy charting interface, please see the following blog post:
Take care,
Chris
Thank you for the reply, @ChristopherAllen. I look forward to exploring "arcpy.charts", not to be confused with the old-fashioned "arcpy.Chart" class.
from the code example way at the bottom
chart.updateChart()
I think if you don't create the chart using argument name (eg rotated=True) when creating it, it uses the default.
The other thought which I hope isn't true, is that it changes the page orientation.... but that would be just plain bad
Okay, thanks. Your suggestions gave me some ideas that eventually led me to the solution. My first 2 tries were failures:
1) c = arcpy.Chart("chart1", rotated = True) gave me the following error:
TypeError Traceback (most recent call last)
In [25]:
Line 1: c = arcpy.Chart("chart1", rotated=True)
TypeError: __init__() got an unexpected keyword argument 'rotated'
(BTW, I also tested using rotate = True in case it's a typo in the documentation) But I guess the keyword arguments only work if you are calling it on the Bar chart object.
2) I added a c.updateChart() before the last line and got this exception:
Exception Traceback (most recent call last)
In [28]:
Line 15: c.updateChart()
File c:\program files\arcgis\pro\Resources\arcpy\arcpy\_chart.py, in updateChart:
Line 522: return convertArcObjectToPythonObject(self._arc_object.updateChart(*gp_fixargs((), True)))
Exception: Invalid layer or table.
---------------------------------------------------------------------------
3) Here's what worked: After setting type to bar, set the bar chart object to rotated (c.bar.rotated = True)
c = arcpy.Chart("chart1")
c.title = "Land cover in Gettysburg Battlefield"
c.type = "bar"
c.bar.rotated = True # Rotated is a bar property, not a chart property.
c.xAxis.field = "COVER"
c.bar.aggregation = "COUNT"
c.xAxis.title = "Land cover"
c.yAxis.title = "Number of polygons"
prj = arcpy.mp.ArcGISProject("CURRENT")
mxd = prj.listMaps()[0]
thelayer = mxd.listLayers()[0]
c.dataSource = thelayer
c
Bottom line? rotated is a bar property, not a chart property (since not all chart types can be rotated). This was confusing since all the other properties I used are both chart and bar properties.
By the way, I tried code similar to that example at the bottom of the page and got an error. This code generated the error you see below it.
bar = arcpy.chart.Bar(x="COVER",
aggregation="COUNT",
title="Land cover in Gettysburg Battlefield",
xTitle = "Land cover",
yTitle = "Number of polygons",
rotated=True,
dataSource=theLayer)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
In [42]:
Line 6: bar = arcpy.chart.Bar(x="COVER",
AttributeError: module 'arcpy' has no attribute 'chart'
---------------------------------------------------------------------------
Hi @LauraTateosian ,
Thanks for the great question! You are correct, since the `rotated` property is specific to bar charts, you set this property similar to how you set the `aggregation` property (with `c.bar.rotated` rather than `c.rotated`).
I also wanted to address the error you are seeing in the final code example from your follow-up post. In your initial post, you used the original arcpy charting interface to create the chart (documentation page). However, in the final code example of your follow-up post, you used the newer `arcpy.charts` module that was introduced in version 2.8 of Pro (documentation page). The module is called `arcpy.charts` rather than `arcpy.chart`, so it appears that this might have caused the error you are seeing. For instance, here's a bar chart created with the `arcpy.charts` module:
lyr = arcpy.mp.ArcGISProject("current").listMaps()[0].listLayers()[0]
# Note: using `arcpy.charts.Bar`
c = arcpy.charts.Bar(x="status", aggregation="COUNT", rotated=True,
title="Street Tree Status", xTitle="Tree Status",
yTitle="Number of Trees", dataSource=lyr)
c
For more information on the newer `arcpy.charts` module and how it differs from the original arcpy charting interface, please see the following blog post:
Take care,
Chris
Thank you for the reply, @ChristopherAllen. I look forward to exploring "arcpy.charts", not to be confused with the old-fashioned "arcpy.Chart" class.