Line Plot question??

ejleiss

Active member
Joined
Oct 1, 2012
Messages
37
Programming Experience
1-3
Hi,

Is there an easy way to control the x axis of a line chart so that the minimum value is not always zero but scrolls along with the incoming data.
For instance if I want to plot an array of 100 elements one element at a time it would plot across the screen but then when it gets to right side edge the X axis minimum value would increment along with the incoming data. I think in some other platforms this can be done using a ring buffer (circular buffer) but I'm not sure how to implement that in vb.net

Any advice would help.
Thanks
 
Hi,

I was looking at this the other day and to be honest I did not know the answer. But after looking at your Circular Buffer question yesterday it suddenly hit me what the answer was. It's the exact interpretation of your Circular Buffer.

To clarify what I mean, what you do to the chart is remove the first point that was entered at the same time as adding your newest point to be plotted on the chart and then you let the x-axis of the chart sort itself out.

To demonstrate, create a new form, add a chart, a timer and then replace the code behind with the code below.

VB.NET:
Public Class Form1
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    With Chart1.Series(0).Points
      .Clear()
      .AddXY(Now.AddSeconds(-10).ToLongTimeString, 15)
      .AddXY(Now.AddSeconds(-9).ToLongTimeString, 10)
      .AddXY(Now.AddSeconds(-8).ToLongTimeString, 30)
      .AddXY(Now.AddSeconds(-7).ToLongTimeString, 40)
      .AddXY(Now.AddSeconds(-6).ToLongTimeString, 10)
      .AddXY(Now.AddSeconds(-5).ToLongTimeString, 6)
      .AddXY(Now.AddSeconds(-4).ToLongTimeString, 40)
      .AddXY(Now.AddSeconds(-3).ToLongTimeString, 60)
      .AddXY(Now.AddSeconds(-2).ToLongTimeString, 30)
      .AddXY(Now.AddSeconds(-1).ToLongTimeString, 40)
      .AddXY(Now.ToLongTimeString, 20)
    End With
    With Chart1.ChartAreas(0)
      .AxisX.Interval = 1
      .AxisX.LabelStyle.Interval = 1
      .Area3DStyle.Enable3D = False
    End With
 
    Timer1.Interval = 1000
    Timer1.Enabled = True
  End Sub
 
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Static Counter As Integer
    Dim YValue As Integer
 
    Select Case Counter
      Case 0
        YValue = 50
      Case 1
        YValue = 10
      Case 2
        YValue = 25
      Case 3
        YValue = 35
      Case 4
        YValue = 5
      Case 5
        YValue = 40
        Counter = -1
    End Select
    Counter += 1
    With Chart1.Series(0).Points
      .RemoveAt(0)
      .AddXY(Now.ToLongTimeString, YValue)
    End With
  End Sub
End Class
Obviously, I am only using sample data here, but if you then couple this with the sample code I supplied yesterday I think you may have a good working example which you can expand upon.

Good Luck.

Cheers,

Ian
 
Back
Top