0

I'm using .Points.RemoveAt(0) to remove the oldest and .AddXY to add the newest.

But chart is not working right. As I delete each oldest and add each newest (x-axis is time), I am not seeing the chart scroll to the left, as I expect.

Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • No need to do that, you can shift the points in the series `.Points()` array. – John Alexiou Dec 11 '18 at 02:50
  • Look at [this post](https://stackoverflow.com/questions/42891742/c-sharp-oscilloscope-simulator-with-ms-chart-control/42892670#42892670) on how to do it. – jsanalytics Dec 11 '18 at 07:48

1 Answers1

0

Oops I answered with VBA, instead of C#. Well maybe you can do the same trick in C# by reading in the array of values, modifying it and writing it back to the chart.

I think you are trying to make something like this:

scr

and here is the code I am using to do this:

Private Const MaxPoints As Long = 100

Private Sub CommandButton1_Click()
    Dim i As Long, PI As Double
    PI = 4 * Atn(1)
    For i = 1 To 720 / 5
        AddValueToChart 50# + 35# * Sin(5 * i * PI / 180)
        DoEvents
    Next i
End Sub

Public Sub AddValueToChart(ByVal x As Double)
    Dim ch As Chart, list() As Variant
    Set ch = Me.ChartObjects("Chart 1").Chart

    Dim serlist As SeriesCollection
    Set serlist = ch.SeriesCollection()
    ' If chart is empty then add a line with 100 points
    If serlist.Count = 0 Then
        serlist.NewSeries
        ReDim list(1 To MaxPoints)
        ch.SeriesCollection(1).Values = list
    End If

    Dim ser As Series
    Set ser = ch.SeriesCollection(1)
    ' Get an array of values
    list = ser.Values
    Dim i As Long
    For i = MaxPoints To 2 Step -1
        ' Shift points
        list(i) = list(i - 1)
    Next i
    ' Add a new point to the begining of the chart.
    list(1) = x
    ' Assign the modified list as values of the series.
    ser.Values = list
End Sub
John Alexiou
  • 28,472
  • 11
  • 77
  • 133