I am writing a macro to generate pie chart in OpenOffice Basic and I want to display the percentage of the different parts on the graph. For example, I want a result as in the following link:
Here is my reproducible code:
my data are:
https://i.sstatic.net/hwdDz.png
And the Macro:
Sub Macro1
Dim oRange as Object
Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
Dim oRect As New com.sun.star.awt.Rectangle
Dim cTitle as String
oRange = thisComponent.getCurrentSelection.getRangeAddress
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCharts = oSheet.Charts
oRect.Width = 10000
oRect.Height = 10000
oRect.X = 8000
oRect.Y = 1000
oRangeAddress(0).Sheet = oRange.Sheet
oRangeAddress(0).StartColumn = 0
oRangeAddress(0).StartRow = 0
oRangeAddress(0).EndColumn = 1
oRangeAddress(0).EndRow = 2
cTitle = "Test Results"
oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
oChart = oCharts.getByName(cTitle).embeddedObject
oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
oChart.HasMainTitle = True
oChart.Title.String = cTitle
End Sub
Thank you a lot for your help! I find the documentation for OOo basic very difficult to understand and rather poor.
Starting from https://forum.openoffice.org/en/forum/viewtopic.php?t=44377, I eventually came up with this code that does what you need.
rowProps = oChart.Diagram.getDataRowProperties(0)
rowProps.DataCaption = com.sun.star.chart.ChartDataCaption.PERCENT
Dim oLocale As New com.sun.star.lang.Locale
rowProps.PercentageNumberFormat = ThisComponent.getNumberFormats().queryKey(_
"0%", oLocale, True)
Documentation on number formats, such as there is, can be found at https://wiki.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Managing_Number_Formats. Many common tasks in Basic are pretty well documented, but what you are doing is somewhat unusual, hence the rather obscure documentation.
Feel free to keep asking for help -- your questions have been well written. With practice, working with the UNO API becomes easier.