使用 VBA 将数据点添加到 Excel 图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1458662/
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
Adding Datapoint to Excel Chart with VBA
提问by imfrancisd
I have a chart object, with 3 series. The series are getting the Y-values from ranges C1:C10, D1:D10 and E1:E10. The value depends on the values in A1:A10 (eg C1 = A1+6); but I'm charting the values against the values in B1:10 (its a Log-Normal graph).
我有一个图表对象,有 3 个系列。该系列从范围 C1:C10、D1:D10 和 E1:E10 中获取 Y 值。该值取决于 A1:A10 中的值(例如 C1 = A1+6);但我正在根据 B1:10 中的值绘制值(它是一个对数正态图)。
I'm calculating the values in VBA. Since there is only a discrete number of points in A1:A10, I'd like to add some extra points of interest to the chart. So if A1:A10 contains the integers 1 to 10, I'd like to plot a decimal number like 3.5, without having to add any new rows to the worksheet.
我正在计算 VBA 中的值。由于 A1:A10 中只有离散数量的点,我想在图表中添加一些额外的兴趣点。因此,如果 A1:A10 包含整数 1 到 10,我想绘制一个十进制数,如 3.5,而不必向工作表添加任何新行。
From looking around, I'd assume it would be something with the Extendmethod ( MSDN - Extend Method) but I'm not sure how to:
环顾四周,我认为它与Extend方法(MSDN - Extend Method)有关,但我不确定如何:
- Extend a specific series (like only add a point to series C1:C10 and D1:D10
- How to add a data point without requiring to add a cell to the worksheet.
- 扩展特定系列(例如只向系列 C1:C10 和 D1:D10 添加一个点
- 如何在不需要向工作表中添加单元格的情况下添加数据点。
Any help would be appreciated. Thanks
任何帮助,将不胜感激。谢谢
回答by imfrancisd
Question 2
问题2
You can set the values on an individual series by using the value property on the series object.
您可以使用系列对象上的 value 属性设置单个系列的值。
However, in Help, it states that the values in a series can either be
但是,在帮助中,它指出系列中的值可以是
a range on a worksheetor an array of constant values,
工作表上的范围或常量值数组,
but not both.
但不是两者兼而有之。
This means that if you want to specify the series values as a range such as C1:C10, then I think you'll have to add cells if you want to add data points in the series.
这意味着如果您想将系列值指定为诸如 C1:C10 之类的范围,那么如果您想在系列中添加数据点,我认为您必须添加单元格。
If you don't want to add a cell, then you have to specify all values as an array constant.
如果不想添加单元格,则必须将所有值指定为数组常量。
Question 1
问题 1
To add data points to a specific series, I believe you would have to select the series, and modify the Valuesand XValuesproperties.
要将数据点添加到特定系列,我相信您必须选择该系列,然后修改Values和XValues属性。
Example:
例子:
Put this data in the "Sheet1" of Excel and graph it as "Chart1". y1 will be series 1, y2 will be series2 and y3 will be series 3.
将此数据放入 Excel 的“Sheet1”并将其绘制为“Chart1”。y1 将是系列 1,y2 将是系列 2,y3 将是系列 3。
A B C D
1 x y1 y2 y3
2 1 10 100 400
3 2 20 200 500
4 3 30 300 600
Now, lets add a data point to y2.
现在,让我们向 y2 添加一个数据点。
A B C D
1 x y1 y2 y3
2 1 10 100 400
3 2 20 200 500
4 3 30 300 600
5 4 1000
We have to select the series (by number or by name, in this case, 2 or "y2") and set the Value property to "C2:C5"
我们必须选择系列(按编号或按名称,在本例中为 2 或“y2”)并将 Value 属性设置为“C2:C5”
'using ranges
Charts("chart1").SeriesCollection("y2").Values = Worksheets("Sheet1").Range("C2:C5")
'using array constant
Charts("chart1").SeriesCollection("y2").Values = Array(100, 200, 300, 1000)
We'll also change the XValues property so that every Value has an XValue
我们还将更改 XValues 属性,以便每个 Value 都有一个 XValue
'using ranges
Charts("chart1").SeriesCollection("y2").XValues = Worksheets("Sheet1").Range("A2:A5")
'using array constant
Charts("chart1").SeriesCollection("y2").XValues = Array(1, 2, 3, 4)
Note:
笔记:
We can have Values as a range and XValues as an array constant or vice versa.
我们可以将 Values 作为范围,将 XValues 作为数组常量,反之亦然。
We can also have both Values and XValues as ranges or both as array constants.
我们还可以将 Values 和 XValues 都作为范围或两者都作为数组常量。
We cannothave Values as a range and an array constant.
我们不能将 Values 作为范围和数组常量。
We cannothave XValues as a range and an array constant.
我们不能将 XValues 作为范围和数组常量。