首页 > 解决方案 > Excel/VBA:向圆环图添加标题可减小圆环的大小

问题描述

...但我希望我的甜甜圈是全尺寸的。因为我想将标题移动到甜甜圈的中心,所以我不需要上边距,它似乎是通过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


标签: excelvbacharts

解决方案


.ChartTitle.IncludeInLayout = False在 .后面添加一行.HasTitle = True

您可以阅读有关该属性的文档以了解有关其作用的更多详细信息,但基本上您将其设置为

如果在确定图表布局时图表标题将占用图表布局空间,则为真


推荐阅读