BarChart `set_categories` shows unexpected, buggy behaviors
Hi,
Below is a code snippet from the openpyxl document that shows the problem:
from openpyxl import Workbook
from openpyxl.chart import (
Reference,
Series,
BarChart3D,
)
wb = Workbook()
ws = wb.active
rows = [
(None, 2013, 2014),
("Apples", 5, 4),
("Oranges", 6, 2),
("Pears", 8, 3)
]
for row in rows:
ws.append(row)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)
titles = Reference(ws, min_col=1, min_row=2, max_row=4)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.add_data(data=data, titles_from_data=True) // here
chart.set_categories(titles) // here
ws.add_chart(chart, "E5")
wb.save("bar3d.xlsx")
The chart should be like this:
The problem is that if you change the order of chart.add_data
and chart.set_categories
, the categories (i.e., the labels for the x-axis items) don't show up properly in the resulting chart; they are labeled as integers (1, 2, ...), not the text (Apples, Oranges, ...). So, there's an unexpected, hidden dependency between chart.add_data
and chart.set_categories
, which is highly misleading and hard to find. Or put it differently, chart.add_data
seems to override what chart.set_categories
has done.
After changing the order of two commands, the chart looks like this:
I think it's good to make it explicit and warn the users in the document.
Thanks a lot.
Best wishes, Gwonsoo