在 Visual Basic 中绘制时,XY 散点图绘制不正确的 X 轴?

XYScatter Plot Incorrect XAxis when Drawn in Visual Basic?

我正在尝试使用 visual basic 创建散点图,y 轴是数值,x 轴是日期。目的是让情节包含多个系列。这是相关代码:

ActiveWorkbook.Charts.Add
ActiveChart.ChartArea.Select
With ActiveChart
    .ChartType = xlXYScatter
    .HasTitle = True
    .ChartTitle.Text = "Time Trend of Data"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Dates"
    .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "m/d/yy;@"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Total Time"
    .Legend.Position = xlLegendPositionBottom
End With

ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.count)
ActiveSheet.Name = "Time Trend " + CStr(currTT) ' This is just to make sure the new sheet does not have the same name

生成一些数据后,我尝试用循环绘制它。我使用的数组是 chartLabels - 这是每个系列的名称, chartData - a 3d array with several data points for each series and xval and yval - arrays built from the chartData array which are plotted against other:

For j = 0 To UBound(chartLabels)
    If IsEmpty(chartLabels(j)) Then Exit For
        Erase xval
        Erase yval
        ReDim Preserve xval(0 To 0)
        ReDim Preserve yval(0 To 0)
        xval(0) = chartData(1, j, 0)
        yval(0) = chartData(2, j, 0)

    For i = 0 To UBound(chartData, 3) - 1
        If Not IsEmpty(chartData(2, j, i + 1)) Then
            ReDim Preserve xval(0 To i + 1)
            ReDim Preserve yval(0 To i + 1)
            xval(i + 1) = chartData(1, j, i + 1)
            yval(i + 1) = chartData(2, j, i + 1)
        End If
    Next

    MsgBox (Join(xval, " || "))
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(j + 1).XValues = xval
    ActiveChart.SeriesCollection(j + 1).Values = yval
    ActiveChart.SeriesCollection(j + 1).Name = main.chartLabels(j)
Next

包含 MsgBox 语句以查看我作为 XValues 传递到我的散点图的数组。 第一个系列的数组输出如下:

所有这些值都是日期格式。创建的系列数量各不相同。此外,每个系列中的数据点数量取决于用户选择的选项。但是,在散点图上生成的输出如下所示:

图表上的所有内容都是正确的,除了 x 轴被缩放到其各自系列中每个数据点的位置,而不是实际日期(即 1/0/00 在日期格式中实际上是 0 而 3/10/ 00 是 70,因为大约有 70 个数据点)。

我试过使用 xlCategoryScale 和 xlAutomaticScale 作为我的类别类型。我已经尝试在我的每个 xval 和 CStr() 上使用 CDate()。我试过为 XValues 输出不同的数组。没有任何效果。

我怀疑问题与我试图绘制多个数据系列的图形有关。但是,如果有人能告诉我实际问题 and/or 解决此问题的方法,我将不胜感激。提前致谢!

我不确定为什么我没有想到这一点...在我 post 编辑了两个小时后,我发现尽管在我的 XValues 上使用 CStr() 和 CDate() 是无用的, CDbl() 确实有效。我不会只是删除我的问题,因为我认为这不是很直观。我不确定为什么会这样,因为我仍在将 xaxis 格式化为 .CategoryType = xlTimeScale。我在上面的代码中添加了这段代码(这是我之前用CStr()和CDate()测试过的代码,但我没有把它包括在问题中):

For k = 0 To UBound(xval)
    xval(k) = CDbl(xval(k))
Next

这是在问题代码中的 MsgBox 之前添加的。这是集合中第一个系列的 XValues 的 MsgBox 的新输出:

注意:这些值都在 42000 的范围内,因为每个整数等于一天,并且此数据来自 2015 年。

2015 - 1900 = 115 years
115 years * 365.25 days/year = 42003.75
'In reality 2015 = 42005 (because of what years were leap years, etc.)

最后,这里是散点图中 "Numerical" 日期作为 xvalues 的实际输出:

我想这里的教训是 xaxis 喜欢数字,使用其他任何东西都可能是一种皇家痛苦。如果有人遇到这个 post,希望对您有所帮助!