R2D2
R2D2

Reputation: 298

XlsxWriter not writing to multiple columns simultaneously

I'm trying to generate simple pie chart for better visualization of some data, but XlsxWriter wont write data to two columns simultaneously. Where as other example is working fine.

I'm clueless where I might be going wrong

Following is the data :

{'core2': [10.3], 'core3': [4.17], 'core0': [58.68], 'core1': [24.42], 'core6': [0.02], 'core7': [0.0], 'core4': [2.31], 'core5': [0.12]})

Actual data is passed as list -> [10.3, 4.17, 58.68, 24.42, 0.02, 0.0, 2.31, 0.12] to below function

Please find below code :

def draw_simultaneously_busy_cores(type_of_chart,data,workbook):
    print data
    worksheet = workbook.add_worksheet()#'simultaneously_busy_cores')
    bold = workbook.add_format({'bold': 1})
    headings = [0, 1, 2, 3, 4, 5, 6, 7]
    worksheet.write_column('$A$1', headings, bold)
    worksheet.write_column('$B$1',headings)

    chart1 = workbook.add_chart({'type': type_of_chart})

    chart1.add_series({
         'name':       'Simultaneous Busy Cores',
         'categories': ['simultaneously_busy_cores', '=simultaneously_busy_cores!$A$1:$A$8'],
         'values':     ['simultaneously_busy_cores', '=simultaneously_busy_cores!$B$1:$B$8'],
         #'data_labels': {'percentage': True, }
         })
     #Add a title.
    chart1.set_title({'name': 'Simultaneous Busy Cores'})
     #Set an Excel chart style. Colors with white outline and shadow.
    chart1.set_style(10)
     #Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})

thanks in advance. Image shows the outpput : enter image description here

Upvotes: 1

Views: 1458

Answers (1)

jmcnamara
jmcnamara

Reputation: 41644

It should work. Here is an example with sample data:

import xlsxwriter

def draw_simultaneously_busy_cores(type_of_chart, data, workbook):

    worksheet = workbook.add_worksheet('simultaneously_busy_cores')
    bold = workbook.add_format({'bold': 1})
    worksheet.write_column('A1', data, bold)
    worksheet.write_column('B1', data)

    chart1 = workbook.add_chart({'type': type_of_chart})

    chart1.add_series({
         'name':       'Simultaneous Busy Cores',
         'categories': '=simultaneously_busy_cores!$A$1:$A$8',
         'values':     '=simultaneously_busy_cores!$B$1:$B$8',
         })

    #Add a title.
    chart1.set_title({'name': 'Simultaneous Busy Cores'})

    #Set an Excel chart style. Colors with white outline and shadow.
    chart1.set_style(10)

    #Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})


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

data = [0, 1, 2, 3, 4, 5, 6, 7]    
draw_simultaneously_busy_cores('line', data, workbook)

workbook.close()

Output: enter image description here

The chart categories and values syntax in your example is incorrect. You are mixing the list and string syntaxes. Read through the documentation and examples again.

Upvotes: 1

Related Questions