none
Plotting Instantaneous Y values in MS CHART RRS feed

  • Question

  • Hello

    I have a BAR Graph and I need to update its Y values Instantaneously.

    When I use 

    Chart1.Series("Series1").Points.AddXY(1, recArray(1))

    The Y value plotted on X=1 stays there. for e.g if recArray(1) =5, it plots 5, now recArray(1)=100, it plots 100, recArray(1)=2

    I cannot see 2 plotted as previously 100 was plotted and it stays there. I need new value plotted without explicitly clearing the old one. 

    I tried following command but it does not work. i.e. nothing is plotted

    Chart1.Series("Series1").Points.DataBindY(recArray(1))

    Monday, January 22, 2018 7:44 PM

All replies

  • The easiest way to update the chart is to remake and redraw the chart.

    Here is an example that uses a timer. Each timer tick a new y = sin(x) value is calculated and added to a list of y values. If there are more than the max values in the list then a value is removed from the start of the list.

    After the list has been updated, the chart is redrawn by clearing and remaking the chart data with the new list data.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = False}
        Private Angle As Integer
        Structure DataPointType
            Public x As Single
            Public y As Single
        End Structure
        Private ChartDataList As New List(Of DataPointType)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            'setup the chart
            Chart1.Titles.Add("Sin Wave Example")
            Chart1.Titles(0).Font = New Font("Arial", 10, FontStyle.Bold)
    
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Angle (degs)"
                .AxisX.MajorGrid.Enabled = False
                .AxisX.MajorGrid.LineColor = Color.LightGray
                .AxisX.Interval = 50
                .AxisX.IsLabelAutoFit = False
                .AxisX.LabelStyle.Font = New Font("Arial", 10, FontStyle.Regular)
                .AxisX.LabelStyle.Angle = -90
                .AxisX.LabelStyle.IsStaggered = False
                .AxisX.LabelStyle.Enabled = True
    
                .AxisY.Title = "Sin(Angle)"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.IsInterlaced = True
                .AxisY.InterlacedColor = Color.FloralWhite
    
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
            GetDataPoint()
    
            DrawChart()
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            GetDataPoint()
    
            DrawChart()
    
        End Sub
    
        Private Sub DrawChart()
            'draw the chart
            Chart1.Series.Clear()
            Chart1.Series.Add("Line Type")
            Chart1.Series(0).IsVisibleInLegend = False
            Chart1.Series(0).Color = Color.Red
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.ChartAreas(0).AxisX.Minimum = ChartDataList(0).x
            Chart1.ChartAreas(0).AxisX.Maximum = Chart1.ChartAreas(0).AxisX.Minimum + 360
    
            For i = 0 To ChartDataList.Count - 1
                Chart1.Series(0).Points.AddXY(ChartDataList(i).x, ChartDataList(i).y)
            Next
    
        End Sub
    
        Private Sub GetDataPoint()
            'remove a point
            If ChartDataList.Count > 17 Then
                ChartDataList.Remove(ChartDataList(0))
            End If
    
            'add next point
            Dim thisDataPoint As DataPointType
            thisDataPoint.x = Angle
            thisDataPoint.y = CSng(Math.Sin(Angle / 57.8))
    
            ChartDataList.Add(thisDataPoint)
    
            Angle += 20
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Timer1.Enabled Then
                Timer1.Enabled = False
                Button1.Text = "Start"
            Else
                Timer1.Enabled = True
                Button1.Text = "Stop"
            End If
        End Sub
    End Class

    Monday, January 22, 2018 10:12 PM
  • I tried following command but it does not work. i.e. nothing is plotted

    Chart1.Series("Series1").Points.DataBindY(recArray(1))

    The argument to the DataBindY method whould be an IEnumerable, such as a list or array.
    https://msdn.microsoft.com/en-us/library/dd455641(v=vs.110).aspx

    You have passed a single value ( recArray(1) ).  If you pass an array to the method then you can update single elements in that array and the corresponding Y plot will change.

    Monday, January 22, 2018 11:13 PM