vba 在excel VBA中更改饼图的切片颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17387926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:58:03  来源:igfitidea点击:

changing slice colour of a pie chart in excel VBA

excel-vbachartspie-chartvbaexcel

提问by Timon Heinomann

Initially I wrote a function which changes the appearance of a series of pie-charts according to predefined colour themes

最初我写了一个函数,它根据预定义的颜色主题改变一系列饼图的外观

    Function GetColorScheme(i As Long) As String
Const thmColor1 As String = "C:\Program Files\Microsoft Office\Document Themes 14\Theme Colors\Blue Green.xml"
Const thmColor2 As String = "C:\Program Files\Microsoft Office\Document Themes 14\Theme Colors\Orange Red.xml"
    Select Case i Mod 2
        Case 0
            GetColorScheme = thmColor1
        Case 1
            GetColorScheme = thmColor2
    End Select
End Function

However, the paths are not constant and I would like to define each Pie chart slice on its own by an rgb colour. I found here on stackoverflow in a previosu topic (How to use VBA to colour pie chart) a way to change the colour of each slice of a pie chart

但是,路径不是恒定的,我想通过 rgb 颜色单独定义每个饼图切片。我在 stackoverflow 上的 previosu 主题(如何使用 VBA 为饼图着色)中找到了一种更改饼图每个切片颜色的方法

but I don't knwo how to implement the code into the function mentioned above. Could I potentially write

但是我不知道如何将代码实现到上面提到的函数中。我可以写吗

    Function GetColorScheme(i As Long) As String

    Select Case i Mod 2
        Case 0
            Dim clr As Long, x As Long

For x = 1 To 3
    clr = RGB(0, x * 8, 0)
    With ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points(x)
        .Format.Fill.ForeColor.RGB = clr
    End With
Next x
        Case 1
            Dim clr As Long, x As Long

For x = 1 To 3
    clr = RGB(0, x * 8, 0)
    With ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points(x)
        .Format.Fill.ForeColor.RGB = clr
    End With
Next x
    End Select
End Function

The function is linked to the main part of the script (which is)

该函数链接到脚本的主要部分(即)

For Each rngRow In Range("PieChartValues").Rows
chtMarker.SeriesCollection(1).Values = rngRow
ThisWorkbook.Theme.ThemeColorScheme.Load GetColorScheme(thmColor)
chtMarker.Parent.CopyPicture xlScreen, xlPicture
lngPointIndex = lngPointIndex + 1
chtMain.SeriesCollection(1).Points(lngPointIndex).Paste
thmColor = thmColor + 1

where the line

线在哪里

 ThisWorkbook.Theme.ThemeColorScheme.Load GetColorScheme(thmColor) 

gets the value of the function (see first bit of code - the original function) but now I don#t longer have the thmColor variable defined and don't knwo how to best implement the code into the function part

获取函数的值(参见代码的第一部分 - 原始函数)但现在我不再定义 thmColor 变量并且不知道如何最好地将代码实现到函数部分

回答by Tim Williams

Something like this (you'll need to adjust the colors to suit your needs)

像这样的东西(您需要调整颜色以满足您的需要)

http://www.rapidtables.com/web/color/RGB_Color.htm

http://www.rapidtables.com/web/color/RGB_Color.htm

Sub ApplyColorScheme(cht As Chart, i As Long)

    Dim arrColors

    Select Case i Mod 2
        Case 0
            arrColors = Array(RGB(50, 50, 50), _
                              RGB(100, 100, 100), _
                              RGB(200, 200, 200))
        Case 1
            arrColors = Array(RGB(150, 50, 50), _
                              RGB(150, 100, 100), _
                              RGB(250, 200, 200))
    End Select

    With cht.SeriesCollection(1)
        .Points(1).Format.Fill.ForeColor.RGB = arrColors(0)
        .Points(2).Format.Fill.ForeColor.RGB = arrColors(1)
        .Points(3).Format.Fill.ForeColor.RGB = arrColors(2)
    End With

End Sub

Example usage:

用法示例:

chtMarker.SeriesCollection(1).Values = rngRow
ApplyColorScheme chtMarker, thmColor
chtMarker.Parent.CopyPicture xlScreen, xlPicture