0
votes

I would like to know how to be able to assign a name just to a serie of data (using a scatterchart), without getting the same serie's name as title of the chart. I would like to get just the series name as legend and NOT chart title at all.

I realized that autoTitleDeleted needs to have a value of 1.

So, consulting documentation I found that ´chartContainer´ is the class to implement the aforementioned option. Therefore, I import the class and apply it as next:

from openpyxl import Workbook
from openpyxl.chart import (
        ScatterChart,
        Reference,
        Series,
)
from openpyxl.chart.chartspace import ChartContainer

wb = Workbook()
ws = wb.active

rows = [
        ["Size", "Batch 1", "Batch 2"],
        [2, 40, 30],
        [3, 40, 25],
        [4, 50, 30],
        [5, 30, 25],
        [6, 25, 35],
        [7, 20, 40],
]

for row in rows:
    ws.append(row)    

chart2 = ScatterChart()

chart2.x_axis.title = "Size"
chart2.y_axis.title = "Percentage"

xvalues = Reference(ws, min_col = 1, min_row = 2, max_row = 7)
values = Reference(ws, min_col = 3, min_row = 1, max_row = 7)
series1 = Series(values, xvalues)
chart2.series.append(series1)

chart2 = ChartContainer(autoTitleDeleted = 1)

ws.add_chart(chart2, "J10")

wb.save("Ex1.xlsx")

However, next error comes up:

`runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py", line 46, in wb.save("Ex1.xlsx")

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save save_workbook(self, filename)

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook writer.save()

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 276, in save self.write_data()

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data self._write_worksheets()

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 219, in _write_worksheets self._write_drawing(ws._drawing)

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 142, in _write_drawing self._archive.writestr(drawing.path[1:], tostring(drawing._write()))

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 296, in _write self._rels.append(rel)

UnboundLocalError: local variable 'rel' referenced before assignment`

I do not really understand this error. If you could help would be grateful!

1
You need to include the complete traceback with the error, though I don't think it is related to what you're doing here.Charlie Clark
Thank you for your help @CharlieClark. I have edited the question with traceback. Hope this could help to understand where the problem is.FJSJ

1 Answers

1
votes

chart2 = ChartContainer(autoTitleDeleted = 1) This the problem: you must use openpyxl chart objects so that the library can manage the plumbing between objects: charts are very complicated objects and we try and hide some of the complexity Your idea is correct - to set the value to True – but that won't work like this because a ChartContainer doesn't know how to add itself to the XLSX package.

As a workaround it's probably easiest create a title with an empty string. Otherwise you could submit a PR that allows the mapping of the autoTitleDeleted attribute from chartContainers to charts and vice versa.