在同一图表上绘制多个数据

时间:2020-03-24 15:00:34

标签: excel vba excel-charts

当前,我的代码仅绘制从A列到E列的具有相同x值(表示为工作周)的y个“均值”的替代列的图表。但是现在,如果我想将来自另一个区域的数据(例如y的整个替代列“理想平均值”)也包括与图1中突出显示的x值相同的x值包含在同一张图表上,那么我也应该如何将该数据包括在内在VBA中进行绘图?

图1

enter image description here

当前

enter image description here

预期

enter image description here

电流输出 enter image description here

预期产量

enter image description here

当前代码

{{1}}

2 个答案:

答案 0 :(得分:2)

尝试一下。您的图表比分布式图表更适合线性图表。

Sub plotgraphs()

Call meangraph

End Sub

Private Sub meangraph()
    Dim i As Long, c As Long
    Dim r As Integer, n As Integer
    Dim k As Integer
    Dim Shp As Shape
    Dim Cht As Chart, co As Shape
    Dim rngDB As Range, rngX As Range
    Dim rngY() As Range, rngY2() As Range
    Dim rng As Range
    Dim Srs As Series
    Dim Ws As Worksheet
    Dim rngShp As Range



    Set Ws = Sheets("Data")


    With Ws
        Set rngDB = .Range("A1", .Cells(1, Columns.Count).End(xlToLeft))
        Set rngX = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
        r = rngX.Rows.Count
    End With
    For Each rng In rngDB
        If InStr(rng, "mean") Then
            If Len(rng) = 5 Then
                n = n + 1
                ReDim Preserve rngY(1 To n)
                Set rngY(n) = rng.Offset(1, 0).Resize(r)
            Else
                c = c + 1
                ReDim Preserve rngY2(1 To c)
                Set rngY2(c) = rng.Offset(1, 0).Resize(r)
            End If
        End If
    Next rng
    k = 2
    For i = 1 To n '<~~~ Loop
         Set rngShp = Ws.Range("b" & k).Resize(10, 20)
         k = k + 11
         Set co = Worksheets("meangraphs").Shapes.AddChart
         Set Cht = co.Chart
         With co
            .Top = rngShp.Top
            .Left = rngShp.Left
            .Width = rngShp.Width
            .Height = rngShp.Height
        End With
         With Cht
             '.ChartType = xlXYScatter
             .ChartType = xlLineMarkers
             'remove any data which might have been
             '  picked up when adding the chart
             Do While .SeriesCollection.Count > 0
                 .SeriesCollection(1).Delete
             Loop
             'add the data
             'For i = 1 To n '<~~~ Loop
                 Set Srs = .SeriesCollection.NewSeries
                 With Srs
                     .XValues = rngX
                     .Values = rngY(i)
                     .Format.Line.Visible = msoFalse
                     .MarkerStyle = xlMarkerStyleCircle
                     .MarkerSize = 5
                 End With
                 Set Srs = .SeriesCollection.NewSeries
                 With Srs
                     .XValues = rngX
                     .Values = rngY2(i)
                     .Format.Line.Visible = msoFalse
                     .MarkerStyle = xlMarkerStyleCircle
                     .MarkerSize = 5
                 End With

             'Next i
             'formatting...
             With Cht.Axes(xlValue)
                 .MinimumScale = 5
                 .MaximumScale = 20
                 .TickLabels.NumberFormat = "0.00E+00"
             End With
             Cht.Axes(xlCategory, xlPrimary).HasTitle = True
             Cht.Axes(xlValue, xlPrimary).HasTitle = True

         End With
    Next i
End Sub

答案 1 :(得分:1)

    /// <summary>
    /// Gets the prefix of the controller name.
    /// <para> <see langword="Usage:"/>
    /// <code>var <paramref name="controllerNamePrefix"/> = 
    /// <see langword="nameof"/>(ExampleController).
    /// <see cref="GetControllerPrefix()"/>;
    /// </code>
    /// </para>
    /// </summary>
    /// <param name="fullControllerName"></param>
    /// <returns></returns>
    public static string GetControllerPrefix(this string fullControllerName)
    {
        const string Controller = nameof(Controller);

        if (string.IsNullOrEmpty(fullControllerName) || !fullControllerName.EndsWith(Controller))
            return fullControllerName;

        return fullControllerName.Substring(0, fullControllerName.Length - Controller.Length);
    }
相关问题