chartsopenoffice.orgopenoffice-basic

OOo Basic: PieChart, how to change the colour of the graph


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.


Solution

  • 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.