COCO
COCO

Reputation: 158

XLSXWRITER changing color for individual column

I'm trying to change the fill color for a specific bar in a column chart that I'm designing through the spreadsheet using xlsxwriter. Here is a snippet from my code, below:

chartsheet=wb.add_chartsheet('ACCURACY')
chart=wb.add_chart({'type': 'column'})
chart.add_series({
'categories': '=SHEET1!$C$1:$O$1',
'values':     '=SHEET1!$C$15:$O$15',
'fill':   {'color': 'red'}
})
chartsheet.set_chart(chart)

This works but it sets all the bar fills to red, I just need the last bar would represents category at cell O1 and value at cell O15 to appear red.

Thanks

Upvotes: 1

Views: 2425

Answers (1)

jmcnamara
jmcnamara

Reputation: 41574

The way that Excel does this is to treat each of the columns as a data point and apply a format to the point or none to have the series default.

You can do the same in XlsxWriter by using the points series property and just fill in formats for the columns as required, like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

data = [
    [1, 2, 3, 4, 5],
    [2, 4, 6, 8, 10],
]

worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])

chart.add_series({
    'categories': '=Sheet1!$A$1:$A$5',
    'values': '=Sheet1!$B$1:$B$5',
    'points': [None, None, None, None, {'fill': {'color': 'red'}}],
})

worksheet.insert_chart('D3', chart)

workbook.close()

Output:

enter image description here

Upvotes: 3

Related Questions