【问题标题】:Excel VBA Group Chart Series ColorExcel VBA 组图系列颜色
【发布时间】:2015-04-07 07:22:42
【问题描述】:

我有一个程序会提示用户打开文件并从文件数据生成折线图。该系列将绘制在同一张图表上。将有两个命令按钮:一个供用户选择文件;另一个用于生成图表。每次单击图表的命令按钮时,将根据新打开的文件的数据添加系列。

With ThisWorkbook.cht

For a = 1 To lastRow

' Add each series
    Set chtSeries = .SeriesCollection.NewSeries

    With chtSeries

        .Values = rng
        .XValues = Worksheets(sheet).Range(Worksheets(sheet).Cells(a, 1), Worksheets(sheet).Cells(a, 10))


    End With

    Next a

    End With

但是,我需要对系列行进行分组,同一文件中的行用相同的颜色表示。

【问题讨论】:

  • 您应该只在第一次调用“Set chtSeries = .SeriesCollection.NewSeries”,每隔一次,使用对同一个系列的引用。同一系列中的数据点将具有相同的颜色。
  • 我使用循环不断创建一个新系列,因为每个文件中会有多个系列。对于每一行数据,都会有一个新的系列。

标签: vba excel


【解决方案1】:

这里有一些详细的代码可以帮助你:

Sub Graph()

Dim Gr As Chart

        Set Gr = ActiveWorkbook.Charts.Add
            With Gr
            'Whole data source
            .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 5)), PlotBy:=xlRows
            'Graph type
            .ChartType = xlXYScatterSmooth
            'Place
            .Location Where:=xlLocationAsNewSheet, Name:=NewSheetName
            'Title
            .HasTitle = True
            .ChartTitle.Characters.Text = "Chart Title"

            For a = 1 To 20
                'Data Series 1
                .SeriesCollection.NewSeries
                .SeriesCollection(a).Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5)) 'change with a
                .SeriesCollection(a).XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1)) 'not changing I think
                .SeriesCollection(a).AxisGroup = 1
                .SeriesCollection(a).Name = "MTTF"
                '.SeriesCollection(i).Format.Line.Weight = 1
                '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) ' for a row/line
                '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)
            Next a



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

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



'Clean memory
Set Gr = Nothing



End Sub

【讨论】:

  • 对您有帮助吗?你还需要别的吗?
  • 该系列将采用不同的颜色。我需要打开多个文件并将它们绘制在一张图表中。为了区分文件数据,我需要每个文件的系列线颜色相同。例如,“文件 1”中的线条颜色显示为红色,而另一个文件中的系列线条颜色显示为黄色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 2016-10-06
  • 2015-06-22
  • 2021-12-24
  • 2018-11-03
相关资源
最近更新 更多