【问题标题】:How do you graph using Rows instead of Series?您如何使用行而不是系列来绘制图表?
【发布时间】:2015-10-23 03:31:07
【问题描述】:

我正在做一个 VBA Excel 代码来绘制某些数据,但是即使指定按行绘制,225 个数据系列的限制仍然出现,如何解决这个问题?

Y=1197
Set DChart = Charts.Add
   With DChart
      .ChartType = xlXYScatterSmoothNoMarkers
      .SetSourceData Source:=Sheets("Sheet1").Range("A2:" & "B" & Y), PlotBy:=xlRows
   End With

【问题讨论】:

  • 恐怕你不能。这是一个限制。但是“使用行而不是系列”是什么意思?使用您的代码,每行都有一个Series。使用PlotBy:=xlColumns,您将只有一个Series,其中A 列作为x 值,B 列作为y 值。

标签: vba excel graph charts


【解决方案1】:

问题是你正在绘制这个PlotBy:=xlRows,而你应该使用PlotBy:=xlColumns

因为使用 PlotBy:=xlRows,您将拥有 1196 系列,即使它有效,也不会与任何事情相关。使用 PlotBy:=xlColumns,您将只有 1 个 Serie 包含所有数据点。

我一般以此为基础,我为你调整了第一个块,其余的都在那里供参考:

Sub Graph()

Dim Gr As Chart, _
    Sr As Series, _
    Src_Name As String

Src_Name = "Sheet1"
Set Gr = ActiveWorkbook.Charts.Add

With Gr
    '----Source Data Definition
    .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(1197, 2)), PlotBy:=xlColumns
    '----Graph Type
    .ChartType = xlXYScatterSmoothNoMarkers
    '----Location/Placement
    .Location Where:=xlLocationAsNewSheet, Name:="NewSheetName"
    '----Title
    .HasTitle = True
    .ChartTitle.Characters.Text = "Chart Title"


    '----Data Series 1
    Set Sr = .SeriesCollection.NewSeries
    With Sr
        .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
        .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
        .AxisGroup = 1
        .Name = "MTTF"
    End With
    '----Data Series 2
    Set Sr = .SeriesCollection.NewSeries
    With Sr
        .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
        .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
        .Name = "MTTR"
        '----Placing a Serie on Second axis
        .AxisGroup = 2
    End With
    '----Series' formats
    '.SeriesCollection(i).Delete
    '----For a line type chart
    '.SeriesCollection(i).Format.Line.Weight = 1
    '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)
    '----For an area type chart
    '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)

    '----Axis parameters
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Age"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Hours"
    .PlotArea.Interior.ColorIndex = 2
    .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot
    .ChartArea.Font.Size = 14
    .Deselect

    '----Legend positioning
    With .Legend
        .Left = 350
        .Top = 75
    End With
    '----Drawing area positiong
    With .PlotArea
        .Width = 550
        .Height = 350
    End With
End With

'----Free memory
Set Gr = Nothing
Set Sr = Nothing

End Sub

【讨论】:

    猜你喜欢
    • 2014-05-28
    • 2017-04-14
    • 2013-06-06
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    相关资源
    最近更新 更多