Python Forum
Storing chart objects in a dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Storing chart objects in a dictionary (/thread-14099.html)



Storing chart objects in a dictionary - bizzy - Nov-14-2018

Hello,

I am using openpyxl to plot some data in Excel. However, there can sometimes be more than 256 series which is the maximum limit in Excel.

I am able to create a single plot of up to 256 series, but I am unable to make a new plot if I need to make more plots.

So far, what I have done:

-determined the where the data starts and stops
-calculate how many charts I need
-create a dictionary of different charts

nSeries = last_row - start_Row

if (nSeries % 256)  == 0:
    nPolts = nSeries/256
else:
    nPlots = int(nSeries/256) + 1

d = {}  ##dictionary to create variables in a loop
for i in range(0,nPlots):
    d['chart' + str(i)] = ScatterChart()    
-next I want to loop through the data
-once I reach 256 series, I want to insert that chart and start updating the next chart

###GET PLOTTING
nSeries = 0
rowIndex = 2
columnIndex = 2
xvalues = Reference(sheet1, min_col=start_Column, max_col=end_Column, min_row=vds_Row, max_row=vds_Row)
chart_Index = 0

for n in d:
    chart = n
    for i in range(start_Row, last_row):
        values = Reference(sheet1, min_col=start_Column, max_col=end_Column, min_row=i, max_row=i)
        series = Series(values, xvalues, title = sheet.cell(i,1).value)    
        if nSeries <= 256:       
            chart.series.append(series)         
        if nSeries == 256 or i == last_row-1:        
            cellReference =  get_column_letter(columnIndex) + str(rowIndex)        
            sheet2.add_chart(chart, cellReference)     
            columnIndex = columnIndex + 10
            nSeries = 0 
            break
        
    nSeries = nSeries + 1
When I run the last black of code, I get the following error: AttributeError: 'str' object has no attribute 'series' regarding the line

chart.series.append(series)
So my question is, how can I create new charts using for loops? Is it possible to do this with a dictionary?


RE: Storing chart objects in a dictionary - heras - Nov-14-2018

chart = n
will make chart point to the key(s) of the dictionary which is a string, but you want the value associated with that key. Try this:
chart = d[n]



RE: Storing chart objects in a dictionary - bizzy - Nov-15-2018

Thank you! It worked :)