1

I am writing a macro to generate pie chart in OpenOffice Basic but I can't find the method to change the colour of the different part of the pie.

We can take as example the macro of this subject: OpenOffice Calc macro to add pie chart

That is, my data are:
enter image description here

And my code:

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

How can I get some green in my chart, instead of blue, for example?

Thank you for your help.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Xavier
  • 25
  • 4
  • Show some code, please. And you might want to do a search on "openoffice basic pie chart different colors". If that doesn't help, we'll need to know why none of those techniques worked. –  Sep 20 '17 at 16:21
  • Updated with code. I did some long search on Google but didn't find anything. I have no techniques to try since I don't know the name of the method. – Xavier Sep 20 '17 at 16:34
  • Wow, OpenOffice docs are terrible. Start with researching FillColor property on various objects. –  Sep 20 '17 at 16:45

1 Answers1

0

Here is one solution.

Sub Macro1
    ...
    oFirstDiagram = oChart.getFirstDiagram()
    oColorScheme = CreateUnoListener("XColorScheme_", "com.sun.star.chart2.XColorScheme")
    oFirstDiagram.setDefaultColorScheme(oColorScheme)
End Sub

Function XColorScheme_getColorByIndex(index As Integer) As Long
    Dim result As Long
    result = &H0000FF  ' blue
    If index = 0 Then
        result = &H00FF00  ' green
    ElseIf index = 1 Then
        result = &HFFFF00  ' yellow
    End If
    XColorScheme_getColorByIndex = result
End Function

The only relevant documentation I could find for this approach is the API docs: https://www.openoffice.org/api/docs/common/ref/com/sun/star/chart2/XDiagram.html.

Another way is to put the colors in column C.

Status      Count   Color
Unfinished  20      =COLOR(0,255,0)
Finished    30      =COLOR(255,0,0)

Then set the Range for Fill Color to use column C. If you want to see code for this second approach, post a comment and I'll look into it.

Yet another way is from https://forum.openoffice.org/en/forum/viewtopic.php?t=36001.

oChart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS
oChart.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0).Color = &H00FF00

However, this last approach did not change the color when I tried it.

Jim K
  • 12,824
  • 2
  • 22
  • 51