Calc basic set graph legend

In a calc spreadsheet, I want to graph the data in ranges M17:CN17 and M19:CN19.
The labels for that data are in column D, D17 and D19 respectively.
How might I set those labels as graph legends in basic?



Sample file as per Zizi64’s request , the addresses above are wrong since I stripped much out of the original.


If I graph SCHO and VGSH in the same graph for comparison, how do I get ‘SCHO’ and ‘VGSH’ as graph legends instead if the Row numbers?

Thanks, Mike

Please upload your sample file here.


Initially I thought creating an array based upon the label location and data location would work. Unfortunately after a few hours of testing, when indifferent locations the settings for labels in first column get confused.

May look into this further in near future but initial reaction is it is not something simple.

Do have an optional workaround. Found this post → Controlling Chart Legend Entries

Instead of creating using strings, got the data from the cells:

oData2 = Array(oSheet.getCellRangeByName( "D3" ).String,_
               oSheet.getCellRangeByName( "D4" ).String,_
               oSheet.getCellRangeByName( "D5" ).String)

Your noted labels would be:

oData2 = Array(oSheet.getCellRangeByName( "D17" ).String,_
               oSheet.getCellRangeByName( "D19" ).String)

I believe oData2 is a “sequence”. And based upon code from Graph in calc with star basic (noted in a previous post of yours) this will insert the information into the legend:

oChartDoc.HasLegend = 1


Edit 2021-03-15:

While the above works (kludge in my estimation) what I had been trying to accomplish was to use an array of the sequences. Originally with the title and data separated I seemed to have no issue. The problem arose when the rows were not contiguous as in the stated question. After much back & forth, it turns out the confusion within is because of the placement of where DataSourceLabelsInFirstColumn is set. By placing this at the end of the routine, and clearing DataSourceLabelsInFirstRow there also, the displays worked as originally expected.

Will attach your original sample with the macro enclosed. Did run the chart 3x’s on the first sheet. Once with SCHO, once with VGSH and finally with both. Intentionally did this as with both the graph lines appear straight because of the small amount of deviation. May be other ways to address this but I think this is my end to this post.

Returned sample ------- SplitLegend.ods

I tried something similar to what you tried first, I think, copying the pertinent stock symbols into adjacent cells in a scratch row to place that rangeCellAddress in front of the data range addresses in oData, but it didn’t work.
Forget now where I saw something that made me think it might. I’ve read a bunch of postings about graphs in the last few days trying to get a handle on this.
Your suggested solution works quite well, so again I say, ‘Thank you’.

Be well,