Add a trendline to a barchart with xlswriter

Viewed 205

I am trying to create a bar chart with a trendline. I can do this in excel and would like to automate the process. xlswriter is pretty easy to use and I have replicated the bar chart it is just the trend line that does not work for me. It seems to add 2 elements the line and an additional bar on the top of each stack.

charts

This is the code to create the chart on the left

import xlsxwriter

# create worbook, workseet and chart
workbook = xlsxwriter.Workbook("Example.xlsx")
worksheet = workbook.add_worksheet()
chart1 = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})

# Add the worksheet data
headings = ['Model 1', 'Model 2', 'Capacity']
data = [
    [10, 40, 50, 20, 10, 50],
    [30, 60, 70, 50, 40, 30],
    [20, 30, 40, 40, 30, 30]
]

worksheet.write_row('A1', headings)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])

# Configure the first series.
chart1.add_series({
    'name':       '=Sheet1!$A$1',
    'values':     '=Sheet1!$A$2:$A$7',
})

# Configure the first series.
chart1.add_series({
    'name':       '=Sheet1!$B$1',
    'values':     '=Sheet1!$B$2:$B$7',
})

chart1.add_series({
    'name':      '=Sheet1!$C$1',
    'values':    '=Sheet1!$C$2:$C$7',
    'trendline': {'type': 'linear'},
})

# Set an Excel chart style.
chart1.set_style(11)

# Add a chart title
chart1.set_title ({'name': 'xlsxwriter chart'})

# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('F1', chart1)

# Finally, close the Excel file
workbook.close()

The bars select the data I am trying to insert as a trend line. Any help would be appreciated.

1 Answers

It looks like what you are trying to do is to add a secondary line chart rather than a trendline. You can do this with the XlsxWriter chart.combine() method.

Like this:

import xlsxwriter

# create worbook, workseet and chart
workbook = xlsxwriter.Workbook("Example.xlsx")
worksheet = workbook.add_worksheet()
chart1 = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})

# Add the worksheet data
headings = ['Model 1', 'Model 2', 'Capacity']
data = [
    [10, 40, 50, 20, 10, 50],
    [30, 60, 70, 50, 40, 30],
    [20, 30, 40, 40, 30, 30]
]

worksheet.write_row('A1', headings)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])

# Configure the first series.
chart1.add_series({
    'name':       '=Sheet1!$A$1',
    'values':     '=Sheet1!$A$2:$A$7',
})

# Configure the first series.
chart1.add_series({
    'name':       '=Sheet1!$B$1',
    'values':     '=Sheet1!$B$2:$B$7',
})


# Add a chart title
chart1.set_title ({'name': 'xlsxwriter chart'})

# Create a second line chart.
chart2 = workbook.add_chart({'type': 'line'})

chart2.add_series({
    'name':      '=Sheet1!$C$1',
    'values':    '=Sheet1!$C$2:$C$7',
})


# Combine the charts.
chart1.combine(chart2)

# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('F1', chart1)

# Finally, close the Excel file
workbook.close()

Output:

enter image description here

Related