I'm trying to create a chart in openpyxl with a secondary y-axis and an DateAxis
for the x-values.
For this MWE, I've adapted the secondary axis example with the DateAxis example.
from datetime import datetime
from openpyxl import Workbook, chart# set to True to fail/create an invalid document
# set to False to generate a valid, but ugly/useless chart
DATES_ON_2ND = Truewb = Workbook()
ws = wb.activexvals = ['date', *[datetime(2018, 11, d, d+12) for d in range(1, 7)]]
avals = ['aliens', 6, 3, 4, 3, 6, 7]
hvals = ['humans', 10, 40, 50, 20, 10, 50]for row in zip(xvals, avals, hvals):ws.append(row)dates = chart.Reference(ws, min_row=2, max_row=7, min_col=1, max_col=1)
aliens = chart.Reference(ws, min_row=1, max_row=7, min_col=2, max_col=2)
humans = chart.Reference(ws, min_row=1, max_row=7, min_col=3, max_col=3)c1 = chart.LineChart()
c1.x_axis = chart.axis.DateAxis(crossAx=100)
c1.x_axis.title = "Date"
c1.x_axis.crosses = "min"
c1.x_axis.majorTickMark = "out"
c1.x_axis.number_format = "yyyy-mmm-dd"c1.add_data(aliens, titles_from_data=True)
c1.set_categories(dates)
c1.y_axis.title = 'Aliens'# Create a second chart
c2 = chart.LineChart()
if DATES_ON_2ND:c2.x_axis = chart.axis.DateAxis(crossAx=100)c2.x_axis.number_format = "yyyy-mmm-dd"c2.x_axis.crosses = "min"
c2.add_data(humans, titles_from_data=True)
c2.set_categories(dates)# c2.y_axis.axId = 200
c2.y_axis.title = "Humans"# Display y-axis of the second chart on the right
# by setting it to cross the x-axis at its maximum
c1.y_axis.crosses = "max"
c1 += c2ws.add_chart(c1, "E4")
wb.save("secondary.xlsx")
When I leave the secondary x axis as a categorical axis, a valid Excel document is created, even if the chart isn't what I want. But setting the secondary axis as a DateAxis the same way as the primary axis generates an invalid corrupted file that fails to show any chart.
Is there a trick to this that I'm missing?