none
adjust X point value of single series in linechart RRS feed

  • Question

  • Hi everyone.  I am a very basic beginner so I will try my best to make this question make sense.  I have a fastline chart with 5 different series.  Each of series can be started and stopped individually. The X axis is the PC time and the Y axis for all series are random integer values. My problem arises when I start the series at different times.  If series 1 is started before series 2 then series 2 always lags series 1 which shows changes in the Y axis at different times.I want to be able to have all series at the same X value when they are started.  I know they can be started at the same time but I want to be able to start them at any time and have the X value equal to the PC time.  Sorry, I am a very basic beginner so please make answers easy to understand, thanks!   
    Sunday, January 24, 2016 5:09 PM

Answers

  • Thank you so much.  I was able to apply this to my code and it worked well.  thanks again for taking the time to answer my question. I'm marking this as answered.   
    • Marked as answer by Mrpike Wednesday, January 27, 2016 3:45 AM
    Wednesday, January 27, 2016 3:45 AM

All replies

  • Does this look like what you want?

    It saves the second series values when the checkbox is checked. When not checked it saves the second series values as double.nan for emptypoint. Then double.nan values are not are plotted with the emptypoint line style which I set to zero.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form4
        Friend WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 1000, .Enabled = True}
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'setup the chart
            With Chart1.ChartAreas(0)
                .AxisX.LabelStyle.Font = New System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold)
                .AxisX.LabelStyle.Format = "mm:ss"
                .AxisX.LabelStyle.Interval = 30
                .AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds
                .AxisX.LineColor = Color.LightGray
                .AxisX.Title = "Time"
            End With
    
            'setup two series
            Chart1.Series.Clear()
            Chart1.Series.Add("Value 1")
    
            With Chart1.Series("Value 1")
                .IsVisibleInLegend = False
                .ChartType = SeriesChartType.Line
                .BorderWidth = 2
                .Color = Color.Red
            End With
    
            Chart1.Series.Add("Value 2")
            With Chart1.Series("Value 2")
                .IsVisibleInLegend = False
                .ChartType = SeriesChartType.Line
                .BorderWidth = 2
                .Color = Color.Blue
    
                'set the empty points so they are not visible
                .EmptyPointStyle.BorderWidth = 0
                .EmptyPointStyle.MarkerStyle = MarkerStyle.None
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim timeStamp As DateTime = DateTime.Now
            Dim y As Double
    
            For i = 0 To Chart1.Series.Count - 1
                ' Add new data point to its series.
                If i = 1 Then
                    If CheckBox1.Checked Then
                        y = Math.Cos(timeStamp.Second * 12 / 57.3)
                    Else
                        y = Double.NaN
                    End If
                Else
                    y = Math.Sin(timeStamp.Second * 12 / 57.3)
                End If
    
                Chart1.Series(i).Points.AddXY(timeStamp.ToOADate(), y)
    
                If Chart1.Series(i).Points.Count > 0 Then
                    'remove oldest values to maintain a constant number of data points
                    Dim removeBefore As Double = timeStamp.AddSeconds(-30).ToOADate()
                    While Chart1.Series(i).Points(0).XValue < removeBefore
                        Chart1.Series(i).Points.RemoveAt(0)
                    End While
                End If
    
            Next
    
            Chart1.ChartAreas(0).AxisX.Minimum = Chart1.Series(0).Points(0).XValue
            Chart1.ChartAreas(0).AxisX.Maximum = DateTime.FromOADate(Chart1.Series(0).Points(0).XValue).AddMinutes(1).ToOADate()
            Chart1.Invalidate()
        End Sub
    End Class

    Sunday, January 24, 2016 9:37 PM
  • This looks close as the second series appears to restart at the same X value as the first series.  I am not sure how to implement my "y" variable into this code.  In my application, the Y value is an integer read from a label.text  like shown

    If CheckBox1.Checked Then

    Chart1.Series("series 1").Points.AddXY(DateAndTime.Now.ToString("h:mm:ss"), Label1.text)

    End If

    How can I apply this to my application?

      
    Monday, January 25, 2016 1:59 AM
  • This looks close as the second series appears to restart at the same X value as the first series.  I am not sure how to implement my "y" variable into this code.  In my application, the Y value is an integer read from a label.text  like shown

    If CheckBox1.Checked Then

    Chart1.Series("series 1").Points.AddXY(DateAndTime.Now.ToString("h:mm:ss"), Label1.text)

    End If

    How can I apply this to my application?

      

    I am not sure what you mean.

    To convert from the label.text to integer you need to use the parse statement.

             Dim y As Integer = Integer.Parse(Label1.text)

    If you mean how to make you entire application work then there are many details. If you update the chart each time a user enters a number then you replace the example timer with that user looping each time they enter a value.

    Each time through the loop you save the time, and all the values by using the chart series. If a user is "off" the value saved is zero and therefore not plotted.

    Monday, January 25, 2016 3:18 AM
  • Ok. I like this approach. I think I can implement this with some lengthy rewriting of my code. I was hoping to avoid that by just setting the X value of the series to match the Chart area maximum. It appears the .count property is read only but is there a way to shift the series along the X axis by setting a property value?
    Tuesday, January 26, 2016 3:26 AM
  • Ok. I like this approach. I think I can implement this with some lengthy rewriting of my code. I was hoping to avoid that by just setting the X value of the series to match the Chart area maximum. It appears the .count property is read only but is there a way to shift the series along the X axis by setting a property value?

    I cant find a built in property that will do that.

    It seems it would not be hard to just add an X axis offset value to the series when you add data points to the series ie:

        Dim Xoffset as double = 4.5

        Chart1.Series(i).Points.AddXY(x + Xoffset, y)

    Yours is a time so you will have to work that out.

    Tuesday, January 26, 2016 11:34 AM
  • Thank you so much.  I was able to apply this to my code and it worked well.  thanks again for taking the time to answer my question. I'm marking this as answered.   
    • Marked as answer by Mrpike Wednesday, January 27, 2016 3:45 AM
    Wednesday, January 27, 2016 3:45 AM