1

我正在编写一个宏来在 OpenOffice Basic 中生成饼图,但我找不到更改饼图不同部分颜色的方法。

我们可以以这个主题的宏为例: OpenOffice Calc macro to add pie chart

也就是说,我的数据是:
在此处输入图像描述

我的代码:

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

例如,我怎样才能在我的图表中获得一些绿色而不是蓝色?

谢谢您的帮助。

4

1 回答 1

0

这是一种解决方案。

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

对于这种方法,我能找到的唯一相关文档是 API 文档:https ://www.openoffice.org/api/docs/common/ref/com/sun/star/chart2/XDiagram.html 。

另一种方法是将颜色放在 C 列中。

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

然后将填充颜色的范围设置为使用列 C。如果您想查看第二种方法的代码,请发表评论,我会研究它。

另一种方法来自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

但是,最后一种方法在我尝试时并没有改变颜色。

于 2017-09-20T22:50:42.137 回答