VBA 饼图图例 - 如何设置图例文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20087537/
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
VBA Pie Chart Legend - How to set legend text?
提问by John
A similar question was asked (Set Legend Text Pie Chart) but the solution doesn't work for me, maybe I'm missing something. I'm fairly new to working with VBA/Excel/Access and reports and I'm on a limited deadline so I am hoping somebody can enlighten me.
有人问了一个类似的问题(设置图例文本饼图),但该解决方案对我不起作用,也许我遗漏了一些东西。我对使用 VBA/Excel/Access 和报告还很陌生,而且我的截止日期有限,所以我希望有人能启发我。
What I am doing is populating a range of cells with values pulled from an access database and then programatically generating pie charts based on these cell values. The pie charts are fairly simple and contain 2 pieces of data. I am generating 1-4 of them based on the users' selections. So for example, the user can choose A, B, C And/Or D and each letter corresponds to 2 cell columns that contain # of Correct & # of Incorrect for each chart
我正在做的是使用从访问数据库中提取的值填充一系列单元格,然后根据这些单元格值以编程方式生成饼图。饼图相当简单,包含 2 条数据。我正在根据用户的选择生成其中的 1-4 个。例如,用户可以选择 A、B、C 和/或 D,每个字母对应 2 个单元格列,其中包含每个图表的正确数量和不正确数量
The reports are being generated inside of a loop (1 to 4) corresponding to A/B/C/D The Code I'm using to create the charts looks like this:
报告是在对应于 A/B/C/D 的循环(1 到 4)内生成的。我用来创建图表的代码如下所示:
Charts.Add
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range(Cells(20,shift(shiftIndex)), Cells(21, shift(shiftIndex))
ActiveChart.HasLegend = True
Which is saying to use the range B20:B21, C20:C21, etc for the Correct/Incorrect values. The problem is that the legend is showing "1" & "2" as labels and I want it to show "Correct" & "Incorrect"
也就是说使用范围 B20:B21、C20:C21 等作为正确/不正确的值。问题是图例将“1”和“2”显示为标签,我希望它显示“正确”和“不正确”
In the other question, the person suggested using the syntax:
在另一个问题中,该人建议使用以下语法:
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("b6:c6," & "b" & x & ":c" & x)
Where b6 would contain "Correct" and c6 would contain "Incorrect" in my case for my labels but when I use this:
在我的标签中,b6 将包含“正确”,而 c6 将包含“不正确”,但是当我使用它时:
ActiveChart.SetSourceData Source:=Sheets("Sheet 1").Range("a26:a27, a20:a21")
Where a26 contains "Correct", a27 contains "Incorrect" and a20 & a21 contain my correct and incorrect values - it tries to use all 4 values in my pie chart. Am I missing something here? Separating the parameters by a comma should indicate the first range as my legend and the second as my data source?
其中 a26 包含“正确”,a27 包含“不正确”,a20 和 a21 包含我的正确和不正确值 - 它尝试使用我的饼图中的所有 4 个值。我在这里错过了什么吗?用逗号分隔参数应该将第一个范围指示为我的图例,将第二个范围指示为我的数据源?
回答by Cool Blue
you can directly control this using the XValues property of the Series object...
您可以使用 Series 对象的 XValues 属性直接控制它...
Assuming you have only one series in the chart try:
假设您在图表中只有一个系列,请尝试:
Activechart.Seriescollection(1).XValues=Activesheet.Range("a26:a27")
a further tip would be to do this:
另一个提示是这样做:
Dim chMyChart as Chart
Dim chMySeries as Series
Set chMyChart = Activesheet.Activechart
Set chMySeries = chMyChart.Seriescollection(1)
and then use chMyChart
in place of Active chart and chMySeries
in place of ActiveChart
etc.
然后使用chMyChart
代替活动图表和chMySeries
代替ActiveChart
等。
This is more efficient, but it also activates vba's Intellisensethat shows you a list of all of the properties available for these objects after you enter the ".".
For me anyway, this doesn't seem to be exposed on objects like Active???
or indexed objects like chMyChart.SeriesCollection(1)
这更有效,但它也会激活 vba 的智能感知,在您输入“.”后,它会向您显示可用于这些对象的所有属性的列表。无论如何,对我来说,这似乎并没有暴露在像这样的对象Active???
或索引对象上chMyChart.SeriesCollection(1)