...但是我希望我的甜甜圈要全尺寸。当我想将标题移动到甜甜圈的中心时,我不需要顶部边距,好像是通过Chart.HasTitle = True
添加的。我该如何解决?
示例代码
Sub createChart()
If ActiveSheet.ChartObjects.Count > 0 Then ActiveSheet.ChartObjects.Delete
Dim chrt As ChartObject
Dim dataRng As Range
Dim lft As Integer
lft = ActiveSheet.Range("D2").Left
Dim wdth As Integer
wdth = 500
Dim hgt As Integer
hgt = 300
Dim tp As Integer
tp = ActiveSheet.Range("D2").Top
Set chrt = ActiveSheet.ChartObjects.Add(Left:=lft, Width:=wdth, Height:=hgt, Top:=tp)
Dim i As Integer
For i = 1 To 10
ActiveSheet.Cells(i, 1).Value = "A" & i
With ActiveSheet.Cells(i, 2)
.Value = i / 55
.NumberFormat = "0.00%"
End With
Next i
Set dataRng = Range("A1:B10")
With chrt.Chart
.ChartType = xlDoughnut
.SetSourceData Source:=dataRng
' comment out from here
.HasTitle = True
With .ChartTitle
.Text = "Test"
.Top = hgt / 2
.Left = wdth / 2 - 20
End With
' to here
.HasLegend = False
End With
End Sub
答案 0 :(得分:2)
在.ChartTitle.IncludeInLayout = False
之后添加.HasTitle = True
行。
您可以阅读documentation on the property以获得有关其功能的更多详细信息,但基本上您将其设置为
确定图表布局时图表标题是否会占用图表布局空间
答案 1 :(得分:0)
除了添加标题之外,您还可以添加一个叠加元素,如下所示:
Sub testCreateChart()
ActiveSheet.ChartObjects.Delete
Dim chrt As ChartObject
Dim dataRng As Range
Dim lft, wdth, hgt, tp As Integer
lft = ActiveSheet.Range("D2").Left
wdth = 500
hgt = 300
tp = ActiveSheet.Range("D2").Top
Set chrt = ActiveSheet.ChartObjects.Add(Left:=lft, Width:=wdth, Height:=hgt, Top:=tp)
Dim i As Integer
For i = 1 To 10
ActiveSheet.Cells(i, 1).Value = "A" & i
With ActiveSheet.Cells(i, 2)
.Value = i / 55
.NumberFormat = "0.00%"
End With
Next i
Set dataRng = Range("A1:B10")
With chrt.Chart
.ChartType = xlDoughnut
.SetSourceData Source:=dataRng
' comment out from here
.SetElement msoElementChartTitleCenteredOverlay
With .ChartTitle
.Text = "Test"
.Top = hgt / 2 - 20
.Left = wdth / 2 - 20
End With
' to here
.HasLegend = False
End With
End Sub