vba 如何使用VBA为饼图着色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17385213/
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
How to use VBA to colour pie chart
提问by Timon Heinomann
I have the following code in which the code tries to create a bubble chart with pie charts as the bubbles. As in this version colour themes are used to create a different colour in each pie chart (bubble) in the function part I have the problem that it works depending on the paths to the colour palettes.
我有以下代码,其中代码尝试创建一个以饼图作为气泡的气泡图。由于在此版本中,颜色主题用于在功能部分的每个饼图(气泡)中创建不同的颜色,我遇到的问题是它的工作取决于调色板的路径。
Is there an easy way to make the function in a way that it works independently of those paths either by coding a colour for each pie chart segment or by using standardize paths (probably not possible, not preferable).
是否有一种简单的方法可以通过为每个饼图段编码颜色或使用标准化路径(可能不可能,不可取)来使函数独立于这些路径工作。
Sub PieMarkers()
Dim chtMarker As Chart
Dim chtMain As Chart
Dim intPoint As Integer
Dim rngRow As Range
Dim lngPointIndex As Long
Dim thmColor As Long
Dim myTheme As String
Application.ScreenUpdating = False
Set chtMarker = ActiveSheet.ChartObjects("chtMarker").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set rngRow = Range(ThisWorkbook.Names("PieChartValues").RefersTo)
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
Next
lngPointIndex = 0
Application.ScreenUpdating = True
End Sub
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
The code copies a single chart again and again on the bubbles. So I would like to alter the Function (now called Get colourscheme) into a function that assigns a a unique RGB colour to each segment of each pie chart. A similar issue is discussed here Change the Point Color in chart excel VBAbut the code apparently did not work for the person who was asking. Could anybody give me any advice on how to rewrite the function part of the code
代码在气泡上一次又一次地复制单个图表。所以我想将函数(现在称为获取颜色方案)更改为一个函数,该函数为每个饼图的每个部分分配一个唯一的 RGB 颜色。此处讨论了类似的问题更改图表 excel VBA 中的点颜色,但该代码显然对询问的人不起作用。任何人都可以就如何重写代码的功能部分给我任何建议
My rough approach would be:
我粗略的做法是:
- select the worksheet and then grab each graph after it is copied
- change the colour of each segment with a unique RGB code
- 选择工作表,然后在复制后抓取每个图形
- 使用唯一的 RGB 代码更改每个段的颜色
But how I would implement it into VBA is not clear to me. I would really appreciate ANY comments on the issue.
但是我不清楚如何将它实现到 VBA 中。我真的很感激关于这个问题的任何评论。
采纳答案by Tim Williams
Here's how to set the colors of each slice in a pie chart. Not sure how you want to determine which slice gets what color.
以下是如何设置饼图中每个切片的颜色。不确定您想如何确定哪个切片获得什么颜色。
Dim clr As Long, x As Long
For x = 1 To 30
clr = RGB(0, x * 8, 0)
With ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points(x)
.Format.Fill.ForeColor.RGB = clr
End With
Next x