简体   繁体   中英

Line chart in openpyxl

I'm having problems creating a line chart in openpyxl. I'm using the following code:

from datetime import date 
from openpyxl import Workbook 
from openpyxl.chart import LineChart, Reference, Series

list_of_num = ['Batch 1', 'Batch 2', 'Batch 3']

ms_lastc = 3

rows = [
    ['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
    [date(2015, 9, 1), 40, 30, 25],
    [date(2015, 9, 2), 40, 25, 30],
    [date(2015, 9, 3), 50, 30, 45],
    [date(2015, 9, 4), 30, 25, 40],
    [date(2015, 9, 5), 25, 35, 30],
    [date(2015, 9, 6), 20, 40, 35]]


if __name__ == '__main__':
    wb = Workbook()
    ms = wb.active
    for row in rows:
        ms.append(row)
    lc = LineChart()
    x = Reference(ms,
                  min_col=1,
                  min_row=2,
                  max_col=1,
                  max_row=7)
    for i, name in enumerate(list_of_num):
        values = Reference(ms,
                           min_col=i+1,
                           min_row=2,
                           max_col=i+1,
                           max_row=7)
        serie = Series(values, xvalues=x, title=name)
        lc.append(serie)
    ms.add_chart(lc, 'A9')
    wb.save('bla.xlsx')

The following code shows this result: 结果

And when I open the Serie in the chart the Series values are empty.

Can someone please help me?

Use a scatter chart instead:

from openpyxl.chart import ScatterChart

ls = ScatterChart()

The default is for scatter chart points to be connected by lines, which is probably what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM