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

Issue: Adding a chart to worksheet after creating an array of charts #200

Closed
rlecover opened this Issue Dec 17, 2014 · 4 comments

Comments

2 participants
@rlecover

rlecover commented Dec 17, 2014

I am using Python 3.4 and xlsxwriter 0.6.4. I am trying to add a chart to worksheet after creating an array of charts. The following code works fine:

import xlsxwriter

print(xlsxwriter.__version__)


def createChart(workbook):
    chart = workbook.add_chart({'type': 'scatter','subtype': 'straight_with_markers'})
    print("Created new chart " + str(chart))
    return chart

def make_worksheet(workbook):
    worksheet = workbook.add_worksheet()
    charts = list()

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

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

    catstring = '=Sheet1!$A$1:$A$5'
    valstring = '=Sheet1!$B$1:$B$5'


    newchart = createChart(workbook)
    newchart.add_series({'values': '=Sheet1!$A$1:$A$5', 'categories': '=Sheet1!$B$1:$B$5'})
    print("Series Added")
    worksheet.insert_chart('E9', newchart)


    return workbook


def main():
    workbook = xlsxwriter.Workbook("demo.xlsx")
    workbook = make_worksheet(workbook)
    workbook.close()
main()  

This code also runs without error

import xlsxwriter

print(xlsxwriter.__version__)


def createChart(workbook):
    chart = workbook.add_chart({'type': 'scatter','subtype': 'straight_with_markers'})
    print("Created new chart " + str(chart))
    return chart

def make_worksheet(workbook):
    worksheet = workbook.add_worksheet()
    charts = list()

    charts.append(createChart(workbook))
    charts.append(createChart(workbook))
    charts.append(createChart(workbook))

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

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

    catstring = '=Sheet1!$A$1:$A$5'
    valstring = '=Sheet1!$B$1:$B$5'

    return workbook


def main():
    workbook = xlsxwriter.Workbook("demo.xlsx")
    workbook = make_worksheet(workbook)
    workbook.close()
main()  

This code, whose only alteration is adding some charts to an array, does not work.

import xlsxwriter

print(xlsxwriter.__version__)


def createChart(workbook):
    chart = workbook.add_chart({'type': 'scatter','subtype': 'straight_with_markers'})
    print("Created new chart " + str(chart))
    return chart

def make_worksheet(workbook):
    worksheet = workbook.add_worksheet()
    charts = list()

    charts.append(createChart(workbook))
    charts.append(createChart(workbook))
    charts.append(createChart(workbook))

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

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

    catstring = '=Sheet1!$A$1:$A$5'
    valstring = '=Sheet1!$B$1:$B$5'


    newchart = createChart(workbook)
    newchart.add_series({'values': '=Sheet1!$A$1:$A$5', 'categories': '=Sheet1!$B$1:$B$5'})
    print("Series Added")
    worksheet.insert_chart('E9', newchart)


    return workbook


def main():
    workbook = xlsxwriter.Workbook("demo.xlsx")
    workbook = make_worksheet(workbook)
    workbook.close()
main()  

The error I get is:

0.6.4
Created new chart <xlsxwriter.chart_scatter.ChartScatter object at 0x0222B410>
Created new chart <xlsxwriter.chart_scatter.ChartScatter object at 0x0222B4D0>
Created new chart <xlsxwriter.chart_scatter.ChartScatter object at 0x0222B550>
Created new chart <xlsxwriter.chart_scatter.ChartScatter object at 0x0281BEB0>
Series Added
Traceback (most recent call last):
  File "demo.py", line 46, in <module>
    main()
  File "demo.py", line 45, in main
    workbook.close()
  File "C:\Python34\lib\site-packages\xlsxwriter\workbook.py", line 286, in clos
e
    self._store_workbook()
  File "C:\Python34\lib\site-packages\xlsxwriter\workbook.py", line 497, in _sto
re_workbook
    self._prepare_drawings()
  File "C:\Python34\lib\site-packages\xlsxwriter\workbook.py", line 965, in _pre
pare_drawings
    self.charts = sorted(self.charts, key=lambda chart: chart.id)
TypeError: unorderable types: int() < str()

@jmcnamara jmcnamara added the bug label Dec 17, 2014

@jmcnamara jmcnamara self-assigned this Dec 17, 2014

@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Dec 17, 2014

Owner

Hi @rlecover,

The issue is here:

    charts.append(createChart(workbook))
    charts.append(createChart(workbook))
    charts.append(createChart(workbook))

You can't create charts without adding them to a worksheet using insert_chart().

Probably there should be a warning for this but I think you also need to make more of an effort at debugging these issues yourself. Read the Chart documentation and start with one of the working examples in the distro. Then add complexity as you go.

Regards,

John

Owner

jmcnamara commented Dec 17, 2014

Hi @rlecover,

The issue is here:

    charts.append(createChart(workbook))
    charts.append(createChart(workbook))
    charts.append(createChart(workbook))

You can't create charts without adding them to a worksheet using insert_chart().

Probably there should be a warning for this but I think you also need to make more of an effort at debugging these issues yourself. Read the Chart documentation and start with one of the working examples in the distro. Then add complexity as you go.

Regards,

John

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

@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Dec 17, 2014

Owner

Hi @rlecover,

I've pushed a fix to master that removes unused charts and doesn't raise an error.

John

Owner

jmcnamara commented Dec 17, 2014

Hi @rlecover,

I've pushed a fix to master that removes unused charts and doesn't raise an error.

John

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

@rlecover

This comment has been minimized.

Show comment
Hide comment
@rlecover

rlecover Dec 18, 2014

Thank you!

rlecover commented Dec 18, 2014

Thank you!

@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Dec 31, 2014

Owner

Fixed in release 0.6.5.

Thanks.

Owner

jmcnamara 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