Ryan
Ryan

Reputation: 354

Appending data sets to an OpenPyxl Chart using a For-Loop

enter image description here

In Python, I have the ability to add series data to my chart object to plot as a line graph.

I'm using the following lines:

overall_stats_sheet2            = current_book.worksheets[0] 
overall_chart_sheet             = current_book.worksheets[1]
chart_object                    = charts.LineChart()
for x in top_down_reference_points[0]:
    chart_object.append(charts.Series(charts.Reference(overall_stats_sheet, (x,1),  (x, overall_stats_sheet2.get_highest_column()+1)), title = 'Erasure Decodes'))
chart_object.drawing.top        = 0
chart_object.drawing.left       = 400
chart_object.drawing.width      = 650
chart_object.drawing.height     = 400

overall_chart_sheet.add_chart(chart_object)

top_down_reference_points[0] contains all of the row numbers that erasure decode exists on. In the example picture, the numbers are row 19 and row 39.

My for loop code currently iterates through those and appends them to the graph, but it creates a new legend label and line for each erasure-decode set. I want to combine all that data from the sheet and graph one line associated with all the erasure decode data. Is this possible?

Upvotes: 0

Views: 894

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19507

It's not entirely clear from your code which cells you want in your chart and how. It may be as simple as creating a single series that refers to multiple cells. At the moment you're creating multiple series which is why you're seeing multiple items in the legend.

BTW. I strongly recommend you start using the 2.3 beta of openpyxl which has much better chart support.

Upvotes: 1

Related Questions