Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot a plot twice #184

Closed
gglanzani opened this issue Nov 17, 2014 · 6 comments

Comments

Projects
None yet
2 participants
@gglanzani
Copy link

commented Nov 17, 2014

I'm on the latest XlsxWriter (0.6.4), and I can't get this to work, i.e. plotting the same chart twice, on two different worksheets. Do I need to make a (deep)copy of the object, or something?

import xlsxwriter

print(xlsxwriter.__version__)

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

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

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

    chart.add_series({'values': ['Sheet1', 0, 1, 4, 1]})
    worksheet.insert_chart('E9', chart)
    return chart

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

chart = make_worksheet(workbook)

second_worksheet = workbook.add_worksheet()
second_worksheet.insert_chart('E9', chart)

workbook.close()
@jmcnamara

This comment has been minimized.

Copy link
Owner

commented Nov 17, 2014

Hi Giovanni,

Thanks for the detailed report.

Each chart object in the workbook needs to be created via add_chart() so unfortunately your sample code won't work.

You should restructure it to return a new chart object each time.

However,

  • This should really be documented. I thought that it was but it doesn't look like it is. I'll need to add that.
  • Also, this should probably raise an exception since the file created, with the current version, throws an error in Excel. I'll fix that in the next release.

Regards,

John.

@jmcnamara jmcnamara self-assigned this Nov 17, 2014

@gglanzani

This comment has been minimized.

Copy link
Author

commented Nov 17, 2014

So what you're saying is that, basically, the only way to make it work properly is to do:

import xlsxwriter

print(xlsxwriter.__version__)

def make_chart(workbook):
    chart = workbook.add_chart({'type': 'column'})
    chart.add_series({'values': ['Sheet1', 0, 1, 4, 1]})
    return chart

workbook = xlsxwriter.Workbook('demo.xlsx')
first_worksheet = workbook.add_worksheet()
data = [[1, 2, 3, 4, 5],
        [6, 8, 6, 4, 2]]

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

second_worksheet = workbook.add_worksheet()

first_chart = make_chart(workbook)
second_chart = make_chart(workbook)

first_worksheet.insert_chart('E9', first_chart)
second_worksheet.insert_chart('E9', second_chart)

workbook.close()

Is there really no way to "copy" chart so that you don't need to recall add_chart and add_series? (sorry if I'm insisting :)

@jmcnamara

This comment has been minimized.

Copy link
Owner

commented Nov 17, 2014

So what you're saying is that, basically, the only way to make it work properly is to do

Yes. If you need to produce a lot of similar charts then I would recommend wrapping them in a function.

Note, in your example both charts refer to 'Sheet1'. That may not be what you want.

Is there really no way to "copy" chart so that you don't need to recall add_chart and add_series

Correct, there is no copy function.

@gglanzani

This comment has been minimized.

Copy link
Author

commented Nov 17, 2014

OK. Clear. Both referring to 'Sheet1' is intended.

I'll wrap them then, there isn't much to do

@jmcnamara

This comment has been minimized.

Copy link
Owner

commented Nov 17, 2014

I'll wrap them then, there isn't much to do

Good stuff.

jmcnamara added a commit that referenced this issue Dec 31, 2014

@jmcnamara

This comment has been minimized.

Copy link
Owner

commented Dec 31, 2014

Fixed in release 0.6.5.

Thanks.

@jmcnamara jmcnamara closed this Dec 31, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.