Search code examples
libreoffice

libreoffice basic hide/delete some legend entries - possible?


I search for a way to manipulate the legend entries of a chart via a basic macro.

Manually this can be done by clicking the data line in a line diagram and activate "Hide legend entry" under the Options tab.

Could you present a code line therefore?

We can get the legend by

oChart = ThisComponent.getSheets().getByIndex(0).getCharts().getByIndex(0)
oDiagram = oChart.getEmbeddedObject().getFirstDiagram()
oLegend = oDiagram.Legend

and we can hide or show the whole legend by

rem true=1, false=0
oLegend.show = 1

Assume we have a chart with 3 data lines. How can we hide the legend of the second only?

Thx!


Solution

  • Yes, you are right - repeated property names can be confusing. In this case, the "Legend" you got from the .getFirstDiagram() is just the legend display, a rectangle with colored marks and labels. If you start to explore the object Chart, then the word "Legend" will meet in different places and will mean different things.

    Let's think: if you use the series property to turn an element on and off, then you need to look for the desired element in the series, right?

      oCoordinateSystems = oFirstDiagram.getCoordinateSystems()
      oXCoordinateSystem = oCoordinateSystems(0) ` first and only coordinate system
      
      oChartTypes = oXCoordinateSystem.getChartTypes()
      oXChartType = oChartTypes(0) ` first and only ChartType
      oDataSeries = oXChartType.getDataSeries()
      
      oXDataSeries = oDataSeries(1)  ' As you want - second Series
      oXDataSeries.ShowLegendEntry = False ' Set "not show in Legend"