ms-access - Access 2007 : Hide Data Labels on Chart Object via vba with 0 values?

标签 ms-access charts vba ms-access-2007

我在 Access 2007 中有一个带有堆叠条形图对象的表单,该对象根据当前日期动态生成并输出图表的 PDF。

一切都会生成并工作正常,但正在发生的情况是,即使对于具有 Null 或 0 值的系列,也会应用数据标签。这会导致各个地方出现困惑的文本。

我正在寻找一种通过 VBA 删除属于没有值的系列的任何标签的方法。

我尝试从 SQL 查询中排除空值,并设置格式选项,这样 0 值就不会显示。我尝试过循环遍历该系列并在值 > 0 时应用标签,但如果我将其设置为应用系列名称,它仍然会将其设置为空白值。

enter image description here

编辑当前代码:

Option Compare Database

Private Sub Form_Load()
Dim tstChart As Graph.Chart

   On Error GoTo Form_Load_Error

Set tstChart = [Forms]!testing!barEquip.Object
With tstChart
    .HasTitle = True
    .ChartTitle.Font.Size = 14
    .ChartTitle.Text = VBA.Strings.MonthName(VBA.DatePart("m", VBA.Date()) - 1) & " " &     VBA.DatePart("yyyy", VBA.Date()) & _
                        " Test Title"

    For Each srs In .SeriesCollection
        For Each pt In srs.Points
            pt.DataLabel.Text = "Y"
        Next
    Next
End With

   On Error GoTo 0
   Exit Sub

Form_Load_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_testing"

End Sub

我可以更改每个标签,但我似乎无法找到一种方法来检查系列点中的每个点。

编辑: 已解决 (简单,但效果很好)

Sub AdjustDataLabels(cht As Chart)

Dim srs As Series
Dim pt As Point
Dim vals As Variant

For Each srs In cht.SeriesCollection
    'Apply Value labels
    srs.ApplyDataLabels (xlDataLabelsShowValue)

    For Each pt In srs.Points
        'Check for empty labels
        If pt.DataLabel.Text = "" Then
           'Do nothing
        Else
           'Add Series Name then remove Value
           pt.DataLabel.ShowSeriesName = True
           pt.DataLabel.ShowValue = False
        End If
    Next
Next
End Sub

最佳答案

您正在使用Graph.Chart而不是Chart。它们的用途更加有限,这正是我所担心的。但也许这无论如何都会有帮助。

这个想法是首先确保显示系列数据标签。

一旦我们知道它们已显示,就迭代这些点并根据点的 DataLabel.Text 属性有选择地操作点的 DataLabel.Text 属性。我假设此处显示的值是 0,并且您只想隐藏标签(如果它是 0),而不对其他标签执行任何操作。

在您的程序中,我们将调用另一个子程序来执行此操作:

Set tstChart = [Forms]!testing!barEquip.Object
With tstChart
    .HasTitle = True
    .ChartTitle.Font.Size = 14
    .ChartTitle.Text = VBA.Strings.MonthName(VBA.DatePart("m", VBA.Date()) - 1) & " " &     VBA.DatePart("yyyy", VBA.Date()) & _
                        " Test Title"

    Call AdjustDataLabels(tstChart) 'Call a procedure to modify the labels as needed

End With

因此该代码现在将调用另一个子过程:

Sub AdjustDataLabels(cht As Graph.Chart)

Dim srs As Graph.Series
Dim pt As Graph.Point
Dim vals As Variant

For Each srs In cht.SeriesCollection
    'First, ensure the dataLabels are ON
    srs.ApplyDataLabels
    For Each pt In srs.Points
        'Now, check the datalabels one by one, testing for your criteria
        If pt.DataLabel.Text = " some condition " Then
            'Criteria met, so blank out this datalabel
            'pt.HasDataLabel = False
            'OR:
             pt.DataLabel.Text = vbNullString

        Else
            'If you need to make any adjustments to other labels, 
            ' you can do that here.
            ' For example you could simply append the series name.
            ' Modify as needed.
            pt.DataLabel.Text = pt.DataLabel.Text & " -- " & srs.Name


        End If
    Next
Next
End Sub

关于ms-access - Access 2007 : Hide Data Labels on Chart Object via vba with 0 values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939169/

相关文章:

sql - 微软 Access : setting table column Caption or Description in DDL?

excel - VBA点击IE中的元素

VBA 编辑器中的 VBA 注释

sql-server - 从 SQL Management Studio 查询 Access 数据库,无需使用链接服务器

mysql - 如何检查字段值是否与组合框值相同?

javascript - (d3) 单轴、可缩放时间轴的动态合并?

php - Highcharts 中柱形图的 3 级向下钻取?

vba - 为什么我不能在 VBA 中使用 "Any"作为名称?

vba - 获取当前登录的 Windows 用户

javascript - Meteor 和 ChartJS 动态创建图表