Python Forum
Storing chart objects in a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing chart objects in a dictionary
#1
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?
Reply
#2
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]
Reply
#3
Thank you! It worked :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 37,369 Mar-27-2017, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020