Python Forum
Openpyxl make LineChart - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Openpyxl make LineChart (/thread-38772.html)



Openpyxl make LineChart - SamLiu - Nov-22-2022

Hi all,
I want to use openpyxl make LineChart as Line with markers style.
when run code get new chart need to modify Horizontal axis crosses-->Axis Value to -2.2 that can show normally.
Where could get the parameter about "the Horizontal axis crosses-->Axis Value", thanks in advance!

import openpyxl
from openpyxl.chart import Reference, LineChart
from openpyxl.chart.axis import DateAxis
from random import randint


wb = openpyxl.load_workbook('offset_demo.xlsx')
sheet = wb.active


values = Reference(sheet,
                   min_col=3,
                   max_col=37,
                   min_row=1,
                   max_row=152)


chart = LineChart()
chart.height=13
chart.width=32


chart.add_data(values, titles_from_data=True)
dates=Reference(sheet,min_col=1,min_row=2,max_row=152)
chart.set_categories(dates)

chart.legend=None
chart.y_axis.crossAx=500
chart.y_axis.scaling.min=-2
chart.y_axis.scaling.max=0.5

chart.x_axis=DateAxis(crossAx=100)
chart.x_axis.number_format='dd-mm-yy'
chart.x_axis.majorTimeUnit='days'

for i in range(0,35):
    s1 = chart.series[i]
    s1.marker.symbol = "auto"
    s1.graphicalProperties.solidFill ="%06x" % randint(0,0xFFFFFF)
    s1.marker.graphicalProperties.line.solidFill ="%06x" % randint(0,0xFFFFFF)
    s1.graphicalProperties.dashStyle = "dash"
    s1.graphicalProperties.line.width = 1000

chart.title = "OffSet Trend"

chart.x_axis.title = "Date"

chart.y_axis.title = "Offset Value"

sheet.add_chart(chart,"A2")

wb.save("offset_demo.xlsx")