我目前正在尝试使用预先构建的数组在Excel中创建堆积的柱形图。数组不引用图纸上的任何范围,而是表示该数据的计算。
我遇到的问题是,在创建堆叠式柱形图时,数据没有垂直堆叠在同一列上,而是第二个数据集垂直于第一个数据集垂直堆叠。我将在下方附加一个图像,但现在让我向您展示我的代码,请不要在该子例程中实际创建4个不同的图表,但是其中只有一个需要堆积的列,因此我将引用堆积的列下面的代码:
Sub buildCharts()
Dim myChart as Shape
Set myChart = wsRawData.Shapes.AddChart2(Left:=792, Top:=0, Width:=264, Height:=192)
With myChart.Chart
'load the data
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = myArray1
.SeriesCollection.NewSeries
.SeriesCollection(2).Values = myArray2
.SeriesCollection.NewSeries
.SeriesCollection(3).Values = myArray3
'x-axis
.SeriesCollection(1).XValues = Array("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D")
'set the chart type
.FullSeriesCollection(1).ChartType = xlColumnStacked
.FullSeriesCollection(1).AxisGroup = 1
.FullSeriesCollection(2).ChartType = xlColumnStacked
.FullSeriesCollection(2).AxisGroup = 1
.FullSeriesCollection(3).ChartType = xlLine
.FullSeriesCollection(3).AxisGroup = 1
.FullSeriesCollection(3).Format.Line.Weight = 1.25
'edit
.ChartStyle = 209
.HasTitle = True
.chartTitle.Text = "My Chart"
.ChartArea.Font.Color = vbWhite
.ChartArea.Interior.ColorIndex = 1
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
.Axes(xlCategory).MajorGridlines.Delete
End With
End Sub
这是上面代码输出的图像:
您可以看到列的堆积方式不正确。
现在,当我在“开发人员”标签下使用“记录宏”功能时,我得到以下代码:
ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
ActiveChart.FullSeriesCollection(1).AxisGroup = 1
ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
ActiveChart.FullSeriesCollection(2).AxisGroup = 1
ActiveChart.FullSeriesCollection(3).ChartType = xlLine
ActiveChart.FullSeriesCollection(3).AxisGroup = 1
ActiveChart.FullSeriesCollection(1).ChartType = xlColumnStacked
当使用数据手动创建图表时,它会完美地堆叠图表。
所以我不确定这是我所缺少的。我已经在网络上进行了一些挖掘,但是一直找不到任何东西,希望这里的人可以给我更好的理解!
谢谢。
答案 0 :(得分:2)
我稍有改动,您的代码(我正在Excel 2007中工作)
Dim myChart As Shape
'changed addchart2 to addchart and sheet name, left etc
Set myChart = Sheet1.Shapes.AddChart(Left:=92, Top:=0, Width:=264, Height:=192)
With myChart.Chart
'load the data
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = Array(100, 70, 120, 80, 40, 150, 200, 140, 150, 90, 110, 50)
.SeriesCollection.NewSeries
.SeriesCollection(2).Values = Array(100, 70, 120, 80, 40, 150, 200, 140, 150, 90, 110, 50)
.SeriesCollection.NewSeries
.SeriesCollection(3).Values = Array(150, 120, 150, 120, 80, 180, 280, 180, 195, 130, 160, 150)
'x-axis
.SeriesCollection(1).XValues = Array("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D")
'set the chart type
' used .SeriesCollection instead of .FullSeriesCollection
.SeriesCollection(1).ChartType = xlColumnStacked
.SeriesCollection(1).AxisGroup = 1
.SeriesCollection(2).ChartType = xlColumnStacked
.SeriesCollection(2).AxisGroup = 1
.SeriesCollection(3).ChartType = xlLine
.SeriesCollection(3).AxisGroup = 1
.SeriesCollection(3).Format.Line.Weight = 2.25
'edit
'.ChartStyle =209 ' commented out chart style
.HasTitle = True
.ChartTitle.Text = "My Chart"
.ChartArea.Font.Color = vbWhite
.ChartArea.Interior.ColorIndex = 1
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
.Axes(xlCategory).MajorGridlines.Delete
End With
End Sub
如果对您有帮助,会很高兴