【问题标题】:XY Scatter Plot and Dictionary problemsXY 散点图和字典问题
【发布时间】:2013-07-18 00:03:22
【问题描述】:

我有一个函数可以根据任意数量的字典(每个字典代表图表上的一条线)生成 XY 散点图,每个字典都包含一个日期键和一个数字值。到目前为止,这些值似乎在 Y 轴上有效,但日期轴 (X) 似乎已损坏。每次我将系列添加到字典中的图表时,它都会强制它成为条形图,而我特别想要一个散点图。如果我在分配后强制它返回散点图,它完全拒绝显示日期轴。

这里有一些例子。

我希望图表看起来像这样

如果我告诉它不要使用日期,图表看起来像这样

当我专门将系列的数据类型设置为 xlDate 时,图形变为此。它已经神秘地变成了条形图

如果我在将其设置为使用 xlDate 后专门将其改回散点图,它看起来像这样

任何帮助将不胜感激。这是我的 VBA 代码

Sub GenerateProgressGraph()


Dim Dictionaries(1 To 2) As New Dictionary

    Dictionaries(1).Add DateValue("1/2/2012"), 1
    Dictionaries(1).Add DateValue("2/2/2012"), 2
    Dictionaries(1).Add DateValue("3/2/2012"), 3
    Dictionaries(1).Add DateValue("4/2/2012"), 4

    Dictionaries(2).Add DateValue("1/2/2012"), 1
    Dictionaries(2).Add DateValue("2/2/2012"), 1
    Dictionaries(2).Add DateValue("3/2/2012"), 3
    Dictionaries(2).Add DateValue("4/2/2012"), 4

    Call ProcessProgressGraph(Dictionaries)
End Sub

Sub ProcessProgressGraph(Dict() As Dictionary)

    Dim Graph As Shape
    Dim GraphRange As Range

    With ActiveSheet

        'set graph area
        Set GraphRange = Application.Range("E4:P21")

        'add a new chart
        Set Graph = Shapes.AddChart(xlXYScatterLinesNoMarkers, GraphRange.Left, _
                                    GraphRange.Top, GraphRange.Width, GraphRange.Height)

        With Graph.Chart

            With .Axes(xlCategory)
                .HasTitle = True
                .AxisTitle.Characters.Text = "Dates"
            End With

            .HasTitle = True
            .ChartTitle.Text = "Chart Title"
            .ChartType = xlXYScatterLinesNoMarkers

            'clear all chart data
            '(Excel has a tendency to give us silly resultsets by default)
            For Each srs In .SeriesCollection
                srs.Delete
            Next

            For Each Dictionary In Dict
                Dim ss As Series
                Set ss = .SeriesCollection.NewSeries
                ss.Name = "Values"
                ss.XValues = Dictionary.Keys
                ss.Type = xlDate
                .ChartType = xlXYScatterLinesNoMarkers 'this forces it back into a scatter plot since it auto makes a bar graph
                ss.Values = Dictionary.Items
            Next
        End With
    End With
End Sub

【问题讨论】:

  • 问题是您的日期是以标签而不是 Excel 日期值的形式出现的(例如,“1/2/2012”而不是 40910)。

标签: vba excel office-2010


【解决方案1】:

问题在于 Excel 对 X 轴值的本机处理。老实说,我不知道为什么会这样,但我知道如何解决它:

  1. 获取您的 X 轴日期值并将其转换为 Long 类型,如下所示:

    ReDim longDates(Dictionary.Count) as Long
    For i = LBound(Dictionary.Keys) to UBound(Dictionary.Keys)
        longDates(i) = Dictionary.Keys(i)
    Next
    
  2. 使用ss.XValues = longDates将longDates 指定为X 轴值

  3. 在函数的结束处将 TickLabel 数字格式设置为日期:

    .Axes(xlCategory).TickLabels.NumberFormat = "d/mm/yyyy"
    

应该可以的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多