0
votes

I have a program which will prompt user open file and generate line chart from the file data. The series will be plotted on the same chart. There will be two command buttons: one for user to choose file; another is used to generate chart. Every time the command button for chart is clicked, the series will be added based on the newly open file's data.

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

However, I need to group the series line whereby the lines from the same file represented by the same colour.

1
You should probably only call "Set chtSeries = .SeriesCollection.NewSeries" the first time, every other time, use a reference to the same series. Data points in the same series will have the same color.Sheldon Neilson
I using loop to continuously create a new series because there will be more than 1 series in each file. For each row of the data, there will be a new series.Liew Yuqi

1 Answers

0
votes

Here is some detailed code that should help you :

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