none
Line Chart with VB 2010

    Question

  • Hello, i want to implement a line chart to one of my projects. The line chart will be refreshed every half or a second so it must be flowing for example from right to left.

    Can you suggest something to begin with?? I have found quite a few questions similar to mine here but it didnt help. 

    Thursday, January 13, 2011 11:47 PM

Answers

  • Hi georgz - sorry I didn't back to this post earlier :(

    one of the major problems is all of your variables are declared within the Timer Tick event - meaning everytime it ticks they are reset.

    and it really looks like a lot of extra unnecessary coding.

    try this in an app with just a chart and timer. see if this is more of the look you want. Note that it keeps the number of datapoints at 20 - if you want more, just change the If...Then block

    Dim rndm As New Random
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        Chart1.ChartAreas(0).AxisX.Maximum = 20
    
        Timer1.Interval = 1000
    
        Timer1.Start()
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Chart1.Series(0).Points.Count = 20 Then
          Chart1.Series(0).Points.RemoveAt(0)
        End If
    
        Chart1.Series(0).Points.AddY(rndm.Next(1000, 5000))
      End Sub
    Monday, January 17, 2011 1:10 AM
  • try it this way:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sfd As New SaveFileDialog
        sfd.AddExtension = True
        sfd.Filter = "JPEG Files (*.jpg)|*.jpg"
        sfd.Title = "Select the file to save the image too."
        sfd.AddExtension = True
    
        If sfd.ShowDialog = DialogResult.OK AndAlso sfd.FileName <> "" Then
          Chart1.SaveImage(sfd.FileName, Imaging.ImageFormat.Jpeg)
        End If
      End Sub
    
    Monday, January 17, 2011 4:18 PM

All replies

  • use the Chart control from the toolbox

    here is some bare-bones code to get started. You'll obviously want to use some other properties and methods - such as limiting the number of datapoints.

    try it in an app with a Timer and Chart control

    Dim rndm As New Random
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
    
        Chart1.Series(0).XValueType = DataVisualization.Charting.ChartValueType.Time
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "hh:mm:ss"
    
        Timer1.Start()
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Chart1.Series(0).Points.AddXY(Now, rndm.Next(1, 51))
    
      End Sub
    
    Friday, January 14, 2011 3:11 AM
  • Yes this is what i'm searching for but i dont want the X and Y axis moving, just the blue line! Is it any tutorial on how to make charts on VB?? 
    Friday, January 14, 2011 9:35 AM
  • I found some examples here but i cant find the VB code. I downloaded the Windows Forms Samples but it's for visual studio which i dont have. 

    Could anyone help??

     

    Anyway i found this video on youtube. I guess this is the example program but how do i execute it?? 

    Friday, January 14, 2011 11:27 AM
  • Imports System.Windows.Forms.DataVisualization.Charting
    ...
    
    Private random As New Random()
    Private pointIndex As Integer = 0
    ...
    
    Private Sub timerRealTimeData_Tick(sender As Object, e As System.EventArgs)
      ' Define some variables
      Dim numberOfPointsInChart As Integer = 200
      Dim numberOfPointsAfterRemoval As Integer = 150
      
      ' Simulate adding new data points
      Dim numberOfPointsAddedMin As Integer = 5
      Dim numberOfPointsAddedMax As Integer = 10
      Dim pointNumber As Integer
      For pointNumber = 0 To (random.Next(numberOfPointsAddedMin, numberOfPointsAddedMax)) - 1
       chart1.Series(0).Points.AddXY(pointIndex + 1, random.Next(1000, 5000))
      Next pointNumber
      
      ' Adjust Y & X axis scale
      chart1.ResetAutoValues()
      
      ' Keep a constant number of points by removing them from the left
      While chart1.Series(0).Points.Count > numberOfPointsInChart
       ' Remove data points on the left side
       While chart1.Series(0).Points.Count > numberOfPointsAfterRemoval
         chart1.Series(0).Points.RemoveAt(0)
       End While
       
       ' Adjust X axis scale
       chart1.ChartAreas("Default").AxisX.Minimum = pointIndex - numberOfPointsAfterRemoval
       chart1.ChartAreas("Default").AxisX.Maximum = chart1.ChartAreas("Default").AxisX.Minimum + numberOfPointsInChart
      End While
      
      ' Invalidate chart
      chart1.Invalidate()
    End Sub 'timerRealTimeData_Tick
    
    Hi, i found these code on the examples, i use a form with just a chart and a timer but it doesnt give me random results. What might be wrong??
    Monday, January 17, 2011 12:47 AM
  • Hi georgz - sorry I didn't back to this post earlier :(

    one of the major problems is all of your variables are declared within the Timer Tick event - meaning everytime it ticks they are reset.

    and it really looks like a lot of extra unnecessary coding.

    try this in an app with just a chart and timer. see if this is more of the look you want. Note that it keeps the number of datapoints at 20 - if you want more, just change the If...Then block

    Dim rndm As New Random
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        Chart1.ChartAreas(0).AxisX.Maximum = 20
    
        Timer1.Interval = 1000
    
        Timer1.Start()
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Chart1.Series(0).Points.Count = 20 Then
          Chart1.Series(0).Points.RemoveAt(0)
        End If
    
        Chart1.Series(0).Points.AddY(rndm.Next(1000, 5000))
      End Sub
    Monday, January 17, 2011 1:10 AM
  • Thaks jwavila this works much better. Just a couple of things though, how can i make the Y axis stable, now is moving according to the line.

    Also i would like to take a picture of the graph by clicking a button. 

    Monday, January 17, 2011 8:57 AM
  • notice in the Form1 Load event, there is a line to set the Maximum property of the X axis?

    I bet there is one for the Y axis also, along with a Minimum property

    The best way to see the things you can do with an object is start by typing an object's name in the code editor followed by a period. Intellisense will show you the methods, properties, etc available for that object

    So if you type (make sure you have the period at the end):

    Chart1.
    

    in the code editor, look through the list supplied by Intellisense. There might be a method for saving the image :)

    Then in a button click event you can instantiate a SaveFileDialog. This will allow you to choose the folder to save to and supply a file name.

    Search the Forums for SaveFileDialog - you'll find examples of how to set an initial directory, the file types, etc

    Monday, January 17, 2011 9:23 AM
  • Thanks i added a Y maximum value and is ok. 

    I added a button and inside this code. It doesnt seems to work. 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim saveDlg As SaveFileDialog = New SaveFileDialog
    
        Dim dlgResult As DialogResult = New DialogResult
    
        'Set the filter to PNG files
    
        'saveDlg.Filter = "PNG Files (*.png)|*.png"
    
        'Set the filter to JPEG files
    
        saveDlg.Filter = "JPEG Files (*.jpg)|*.jpg"
    
        saveDlg.Title = "Select the file to save the image too."
    
        dlgResult = saveDlg.ShowDialog
    
        If dlgResult = Windows.Forms.DialogResult.OK Then
    
          'Save the image in the picturebox.
    
          Chart1.SaveImage("chart.jpg", Imaging.ImageFormat.Jpeg)
    
        End If
      End Sub
    

    Monday, January 17, 2011 11:22 AM
  • try it this way:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sfd As New SaveFileDialog
        sfd.AddExtension = True
        sfd.Filter = "JPEG Files (*.jpg)|*.jpg"
        sfd.Title = "Select the file to save the image too."
        sfd.AddExtension = True
    
        If sfd.ShowDialog = DialogResult.OK AndAlso sfd.FileName <> "" Then
          Chart1.SaveImage(sfd.FileName, Imaging.ImageFormat.Jpeg)
        End If
      End Sub
    
    Monday, January 17, 2011 4:18 PM
  • Thank you, i found an other way which it saves the whole program as a picture. I'll try this way too! 
    Monday, January 17, 2011 7:08 PM
  • Hi jwavila,

    Sorry for poor english!, i read this forum and it is in line with our project!, my problem now, I have my temperature sensor sending data to arduino and it will appear to Visual Basic!, And i want to plot it to chart!, temperature versus the time!, could you help me on the codes?, It is just the same on your forum but you are using random lines!,

    Hope for your quick reply!,

    Tuesday, January 8, 2013 9:50 AM
  • Hi jwavila

    I suggest you start a new thread and provide more information andperhaps code samples.

    Ron

    Wednesday, January 9, 2013 12:20 AM
  • could you also give a code for auto saving a chart as jpg file in a certain time interval?,

    hope for your quick response!.

    Tuesday, January 15, 2013 8:03 AM