Results 1 to 19 of 19

Thread: graphing makes my head hurt...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    graphing makes my head hurt...

    so I was able to successfully code this...


    vb Code:
    1. Imports System.Windows.Forms.DataVisualization.Charting
    2.  
    3.  
    4. Public Class Form2
    5.  
    6.  
    7.  
    8.     Public Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    9.  
    10.         Dim theChart As New Chart
    11.         Dim lineone As New List(Of Point)
    12.         Dim linetwo As New List(Of Point)
    13.         Dim theArea As New ChartArea
    14.         Dim Series As New Series
    15.  
    16.         lineone.Add(New Point(TextBox1.Text, TextBox3.Text))
    17.         lineone.Add(New Point(TextBox2.Text, TextBox4.Text))
    18.         'linetwo.Add(New Point(textbox1.text, TextBox5.Text))
    19.         'linetwo.Add(New Point(textbox2.text, TextBox6.Text))
    20.  
    21.         theChart.DataSource = lineone 'I want to add linetwo to this
    22.  
    23.  
    24.         theArea.Name = "chartArea"
    25.         theChart.ChartAreas.Add(theArea)
    26.  
    27.         theChart.Series.Clear()
    28.  
    29.  
    30.         Series.XValueMember = "X"
    31.         Series.YValueMembers = "Y"
    32.         Series.ChartArea = "chartArea"
    33.  
    34.         theChart.Series.Add(Series)
    35.  
    36.         Me.Controls.Add(theChart)
    37.         theChart.Dock = DockStyle.Fill
    38.  
    39.     End Sub
    40.  
    41.  
    42. End Class


    To which I was pretty happy that it worked the first time, the problem I am having now is trying to switch this to a line graph, add the second line/second set of points, and label the lines. I have read a few hundred posts about chart controls and they all seem to be geared around bar graphs with one set of data. I know im only a few lines of code away from getting this to work I just need a little nudge in the right direction

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: graphing makes my head hurt...

    here's how to add a second series:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim s1() As Point = {New Point(0, 10), New Point(1, 15), New Point(2, 10), New Point(3, 15), New Point(4, 10), New Point(5, 15)}
    5.         Dim s2() As Point = {New Point(0, 15), New Point(1, 10), New Point(2, 15), New Point(3, 10), New Point(4, 15), New Point(5, 10)}
    6.         'add points to first series
    7.         For x As Integer = 0 To 5
    8.             Chart1.Series(0).Points.AddXY(x, s1(x).Y)
    9.         Next
    10.         'add a second series
    11.         Chart1.Series.Add("Series2")
    12.         'set both series ChartType
    13.         Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    14.         Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    15.         'add points to second series
    16.         For x As Integer = 0 To 5
    17.             Chart1.Series(1).Points.AddXY(x, s2(x).Y)
    18.         Next
    19.     End Sub
    20.  
    21. End Class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    I keep getting this error message: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    on this line: "Chart1.Series(0).Points.AddXY(x, s1(x).Y)"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    Alright, I am going to post what I got so far...

    The reason I am doing the step ladder approach is because I am going to place the text boxes under the points on the X axis so the graph looks like its scrolling. The goal of this graph is to update every second with the updated information.

    Im having trouble importing the T however, it keeps coming back to me with the error message, "cannot use a string "" as an integer."

    As always, anytips or advice would be appreciated.

    vb Code:
    1. Imports System.Windows.Forms.DataVisualization.Charting
    2.  
    3. Public Class Form2
    4.  
    5.     Dim chart1 As New Chart
    6.     Shared Property T As Integer
    7.  
    8.  
    9.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.  
    11.     End Sub
    12.  
    13.     Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
    14.  
    15.         If T <= 2 Then
    16.             Label1.Visible = False
    17.  
    18.             TextBox20.Text = TextBox21.Text
    19.             TextBox31.Text = TextBox32.Text
    20.  
    21.             TextBox21.Text = TextBox22.Text
    22.             TextBox32.Text = TextBox33.Text
    23.  
    24.             TextBox22.Text = Form1.TextBox10.Text
    25.             TextBox33.Text = Form1.TextBox4.Text
    26.  
    27.             Dim s1() As Point = {New Point(T - 2, TextBox20.Text), New Point(T - 1, TextBox21.Text), New Point(T, TextBox22.Text)}
    28.             Dim s2() As Point = {New Point(T - 2, TextBox31.Text), New Point(T - 1, TextBox32.Text), New Point(T, TextBox33.Text)}
    29.             For x As Integer = T - 2 To T
    30.                 chart1.Series(0).Points.Add(x, s1(x).Y)
    31.             Next
    32.  
    33.             chart1.Series.Add("Series2")
    34.  
    35.             chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    36.             chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    37.  
    38.             For x As Integer = T - 2 To T
    39.                 chart1.Series(1).Points.Add(x, s1(x).Y)
    40.             Next
    41.             chart1.Series(0).Label = "PSS"
    42.             chart1.Series(1).Label = "USS"
    43.  
    44.  
    45.         End If
    46.     End Sub
    47.  
    48.     Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    49.         If Form1.CheckBox1.Checked Then Form1.CheckBox1.Checked = False
    50.     End Sub
    51.  
    52.  
    53. End Class

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: graphing makes my head hurt...

    What line is giving you that error? There are lots of possibilities, as you have this type of thing in several places:

    Point(T - 2, TextBox20.Text)

    Any .Text property is going to return a string. You are implicitly converting that to an integer, which is always a bad idea, but if the string can't be converted to an integer, as an empty string cannot, then you will get an invalid conversion error. In other words, if you enter anything into the textbox that is not an integer, or leave the textbox blank, then these lines will cause an error.

    The best thing to do is to turn Option Strict ON, which will result in lots of errors, including every one of these, but it would be good to fix every one of those errors, because they are all problems in one way or another.

    Whether or not you turn Option Strict ON, you will still need to fix this issue, and the fix is pretty much the same in all cases. You have to explicitly convert the textbox text to an integer prior to using it. The best way to do that is using Integer.TryParse to convert the contents of the textbox into an integer prior to using it.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    alright, I see what you are saying about trying to send a string in the place of an integer so i am now attempting to send form1.t to form2.textbox11.text as an integer

    the point I am actually getting the error is on form1 at form2.show

    vb Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    2.         If Timer1.Enabled = False Then
    3.         MessageBox.Show("You Must Start The Test First!")
    4.         Else
    5.         Form2.Show()
    6.         End If
    7.  
    8.     End Sub


    I added "Integer.TryParse(Me.TextBox11.Text, Form1.T)" and from my reference books it seems like it should work. But as soon as I try to click the checkbox button I get the, string "" as an integer error message


    vb Code:
    1. Imports System.Windows.Forms.DataVisualization.Charting
    2.  
    3. Public Class Form2
    4.  
    5.     Dim chart1 As New Chart
    6.  
    7.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.         Integer.TryParse(Me.TextBox11.Text, Form1.T)
    10.  
    11.     End Sub
    12.  
    13.     Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
    14.  
    15.  
    16.         If Form1.T >= 2 Then
    17.  
    18.             Label1.Visible = False
    19.  
    20.             TextBox20.Text = TextBox21.Text
    21.             TextBox31.Text = TextBox32.Text
    22.  
    23.             TextBox21.Text = TextBox22.Text
    24.             TextBox32.Text = TextBox33.Text
    25.  
    26.             TextBox22.Text = Form1.TextBox10.Text
    27.             TextBox33.Text = Form1.TextBox4.Text
    28.  
    29.             Dim s1() As Point = {New Point(Form1.T - 2, TextBox20.Text), New Point(Form1.T - 1, TextBox21.Text), New Point(Form1.T, TextBox22.Text)}
    30.             Dim s2() As Point = {New Point(Form1.T - 2, TextBox31.Text), New Point(Form1.T - 1, TextBox32.Text), New Point(Form1.T, TextBox33.Text)}
    31.             For x As Integer = Form1.T - 2 To Form1.T
    32.                 chart1.Series(0).Points.Add(x, s1(x).Y)
    33.             Next
    34.  
    35.             chart1.Series.Add("Series2")
    36.  
    37.             chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    38.             chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    39.  
    40.             For x As Integer = Form1.T - 2 To Form1.T
    41.                 chart1.Series(1).Points.Add(x, s1(x).Y)
    42.             Next
    43.             chart1.Series(0).Label = "PSS"
    44.             chart1.Series(1).Label = "USS"
    45.  
    46.  
    47.         End If
    48.     End Sub
    49.  
    50.     Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    51.         If Form1.CheckBox1.Checked Then Form1.CheckBox1.Checked = False
    52.     End Sub
    53.  
    54.  
    55. End Class

    I guess what I am trying to do is, as soon as textbox11 is updated with new data, move all the old data down a row, and then graph all the data
    Last edited by Zmcpherson; Oct 6th, 2011 at 05:18 PM.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    Hey,

    What is T?
    What are you attempting with
    Code:
      Integer.TryParse(Me.TextBox11.Text, Form1.T)
    Are you using that to try to update TextBox11 on Form2 with the value of T ?

    Also do you realise you are handling TextBox10's TextChanged with
    Code:
    Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
    ?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    T = Time

    I use it for what most people use for i. On form1 I have a loop that is t+1

    Yes, I am trying to update form2.textbox11 with form1.t

    and no, I did not see that I had textbox11 handle textbox10_change. I will fix that now.

    However, im still receiving the form2.show "string as integer" message.

    what is the way to have form2 handle form1.timer1_tick? maybe that would be an easier alternative to form2 handling form1.t

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    random side regex question... if my line looks like this..

    ** example test me 1234 [com.test.example] **

    how do I pull out what's in between the []?

    if tried:

    [[-]]
    [-]
    \[\]
    \[:alnum:\]
    \[:word:\]
    \[...\]
    ^[]$
    \[rint:\]

    and maybe a dozen others...

  10. #10
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    That's not how integer.tryparse works
    Code:
     Integer.TryParse(Me.TextBox11.Text, Form1.T)
    will try to parse the string from Form2's TextBox11.Text and store the Integer result in Form1.T (If the TextBox is empty, it will set T to 0).

    When Shaggy was talking about having to use Integer.TryParse, I think he was mainly (but not only) refering to the TextBoxes in
    Code:
      Dim s1() As Point = {New Point(Form1.T - 2, TextBox20.Text), New Point(Form1.T - 1, TextBox21.Text), New Point(Form1.T, TextBox22.Text)}
      Dim s2() As Point = {New Point(Form1.T - 2, TextBox31.Text), New Point(Form1.T - 1, TextBox32.Text), New Point(Form1.T, TextBox33.Text)}
    since the Point structure requires Integers as parameters, not Strings.

    He also advised to turn Option Strict On i.e.
    Code:
    Option Strict On
    
    Imports System.Windows.Forms.DataVisualization.Charting
     
    Public Class Form2
    (on both Forms!! )


    As for Form2 handling the Form1 Timer Tick Event, I'd be tempted to replace your TextBox11.TextChanged handler with a Public sub on Form 2.
    Code:
        Public Sub UpdateChart(ByVal t As Integer, ByVal newX As Integer, ByVal newY As Integer)
    
          'code from your Sub TextBox11_TextChanged goes here
    
        End Sub
    but I am happy to be corrected. Call it from your form1.timer1_tick Event, passing in the values for T and what is in Form1.TextBox10.Text and Form1.TextBox4.Text converted to integers

    There's some bits not quite right in your Charting code, but one step at a time (get some more Aspirins )

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    Alright, I turned option strict on on both forms, and fixed the string/integer errors that it suggested.

    Now the fun part, not only trying to learn how to graph, but also learning how to handle timer ticks across forms : P

    alright, im going out on a limb here but would the syntax of form1.timer1_tick look like this?

    updatechart(t,pvalue,uvalue)?

    and then on form two under public sub updatechart:

    vb Code:
    1. Public Sub UpdateChart(ByVal T As Integer, ByVal X As Integer, ByVal Y As Integer)
    2.  
    3.         If Me.Visible = True And T > 2 Then
    4.  
    5.             Label1.Visible = False
    6.  
    7.             TextBox20.Text = TextBox21.Text
    8.             TextBox31.Text = TextBox32.Text
    9.  
    10.             TextBox21.Text = TextBox22.Text
    11.             TextBox32.Text = TextBox33.Text
    12.  
    13.             TextBox22.Text = Form1.TextBox10.Text
    14.             TextBox33.Text = Form1.TextBox4.Text
    15.  
    16.             Dim s1() As Point = {New Point(T - 2, CInt(TextBox20.Text)), New Point(T - 1, CInt(TextBox21.Text)), New Point(T, CInt(TextBox22.Text))}
    17.             Dim s2() As Point = {New Point(T - 2, CInt(TextBox31.Text)), New Point(T - 1, CInt(TextBox32.Text)), New Point(T, CInt(TextBox33.Text))}
    18.             For X = T - 2 To T
    19.                 chart1.Series(0).Points.Add(X, s1(X).Y)
    20.             Next
    21.  
    22.             chart1.Series.Add("Series2")
    23.  
    24.             chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    25.             chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    26.  
    27.             For X = T - 2 To T
    28.                 chart1.Series(1).Points.Add(X, s1(X).Y)
    29.             Next
    30.             chart1.Series(0).Label = "PSS"
    31.             chart1.Series(1).Label = "USS"
    32.         End If
    33.  
    34.     End Sub


    now form2 will show, but I get still get this error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    on this line, "chart1.Series(0).Points.Add(X, s1(X).Y)"

    btw, if I did that updatechart(x,y,z) thing right, that is an awesome trick, i had no idea. And the new bottle of aspirin has been opened : P

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    OK, first off, I know nothing about using charts, so I was hoping you'd be teaching me as you went along.....

    That said, I think there's a few things wrong with the code you posted. If you look at the example .paul. gave in Post #2, he did it all in his Form_Load handler, and I think some of the code you have there needs moving to your Form_Load handler too.

    I'm thinking these lines in particular
    vb Code:
    1. chart1.Series.Add("Series2")
    2.  
    3. chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    4. chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    5.  
    6.  
    7. chart1.Series(0).Label = "PSS"
    8. chart1.Series(1).Label = "USS"
    .

    Next, are the values of X and Y found from Form1.TextBox10.Text and Form1.TextBox4.Text ? If so you can replace lines 13 and 14 as
    Code:
       TextBox22.Text = X
       TextBox33.Text = Y
    What values are you passing in for T X and Y?
    T is obviously > 2 for the code to be executed and so X in
    vb Code:
    1. For X = T - 2 To T
    2.    chart1.Series(0).Points.Add(X, s1(X).Y)
    3. Next
    will exceed 2 but your s1 array has an upperbound of 2 so you will get an error.

    Also, I don't think you are using the right method to add your points.
    .paul. used Chart1.Series(0).Points.AddXY(x, s1(x).Y). I'm sure you know where the docs are

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    alright, im going back and starting from the beginning :

    vb Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim s1() As Point = {New Point(0, 10), New Point(1, 15), New Point(2, 10), New Point(3, 15), New Point(4, 10), New Point(5, 15)}
    3.         Dim s2() As Point = {New Point(0, 15), New Point(1, 10), New Point(2, 15), New Point(3, 10), New Point(4, 15), New Point(5, 10)}
    4.         'add points to first series
    5.         For x As Integer = 0 To 5
    6.             chart1.Series(0).Points.AddXY(x, s1(x).Y)
    7.         Next
    8.         'add a second series
    9.         chart1.Series.Add("Series2")
    10.         'set both series ChartType
    11.         chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    12.         chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    13.         'add points to second series
    14.         For x As Integer = 0 To 5
    15.             chart1.Series(1).Points.AddXY(x, s2(x).Y)
    16.         Next
    17.     End Sub

    This is .paul's original code and even this is outputting the error: "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"

    I trust that .paul is right and im just doing something wrong but any thoughts?

  14. #14
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    That actually works for me (no other code on Form2). Where are you getting that error ?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    on

    chart1.Series(0).Points.AddXY(x, s1(x).Y)


    I thought it might be me

  16. #16
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    Let me throw this at you and see if it's the sort of thing you are trying to do.
    vb Code:
    1. Option Strict On
    2.  
    3. Public Class Form1
    4.  
    5.     Dim f2 As New Form2
    6.     Dim intY As Integer = 4
    7.     Dim intDir As Integer = 1
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Timer1.Interval = 1000
    11.         Timer1.Enabled = True
    12.  
    13.         f2.Show()
    14.         f2.BringToFront()
    15.     End Sub
    16.  
    17.  
    18.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    19.         Static intTime As Integer = 0
    20.         intTime += 1
    21.  
    22.         intY += intDir
    23.         If intY > 8 Then intDir = -1
    24.         If intY < 4 Then intDir = 1
    25.         Call f2.UpdateChart(intTime, intY)
    26.     End Sub
    27.  
    28. End Class

    vb Code:
    1. Option Strict On
    2. Imports System.Windows.Forms.DataVisualization.Charting
    3.  
    4. Public Class Form2
    5.  
    6.     Private Yvalues() As Integer = {0, 0, 0}
    7.  
    8.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         Chart1.Series.Add("Series2")
    10.  
    11.         Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    12.         Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    13.  
    14.         Chart1.Series(0).Label = "PSS"
    15.         Chart1.Series(1).Label = "USS"
    16.  
    17.     End Sub
    18.  
    19.     Public Sub UpdateChart(ByVal T As Integer, ByVal NewY As Integer)
    20.  
    21.         If Me.Visible = True Then
    22.  
    23.             Label1.Visible = False
    24.  
    25.             Yvalues(0) = Yvalues(1)
    26.             Yvalues(1) = Yvalues(2)
    27.             Yvalues(2) = NewY
    28.  
    29.             TextBox20.Text = Yvalues(0).ToString
    30.             TextBox21.Text = Yvalues(1).ToString
    31.             TextBox22.Text = Yvalues(2).ToString
    32.             TextBox31.Text = T.ToString
    33.  
    34.             Chart1.Series(0).Points.Clear()
    35.             For X As Integer = 0 To 2
    36.                 Chart1.Series(0).Points.AddY(Yvalues(X))
    37.             Next
    38.  
    39.         End If
    40.  
    41.     End Sub
    42.  
    43. End Class

    It's not elegant; I'm just wondering if I've understood what you are trying to do. Hope it helps.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    a few things i have questions on..

    why would you add:

    TextBox31.Text = T.ToString

    T is the X

    could I do something like:

    Yvalues1(t-2) = Yvalues1(t-1)
    Yvalues1(t-1) = Yvalues1(t)
    Yvalues1(t) = NewY1

    For X As Integer = t-2 To t
    chart1.Series(0).Points.AddY(Yvalues1(X))


    the only reason why I would like T to be the X is because that way the user can see how far along the graph he is.

    i tried compiling it and running it your way and then my way and now I am getting the error message

    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"

    on the line:

    Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line

    why is it always the same error, am i missing a reference?

  18. #18
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: graphing makes my head hurt...

    Hey,
    The code is meant purely to see if you can get it working and, hopefully, for you to build upon. I still don't have a clear idea of what you are trying to achieve. I am thinking you want to display only 3 points and scroll the result to the left as you add the latest data point as the new third point on the chart. Is that correct?

    So:
    TextBox31.Text = T.ToString
    was just to show T changing and to demonstrate you can update your TextBoxes with the values you pass to the sub rather than fetching them again from Form1.

    I don't know what values T will have. I assume T will start at 1 and increment by 1 at each Timer Tick on Form1. So
    Yvalues1(t-2) = Yvalues1(t-1)
    Yvalues1(t-1) = Yvalues1(t)
    Yvalues1(t) = NewY1

    For X As Integer = t-2 To t
    chart1.Series(0).Points.AddY(Yvalues1(X))
    will give problems as Yvalues1 is a fixed length array. For example, when T is 10, Yvalues1(t-2) will be trying to access index 8 of an array whose Upperbound is only 2. So you'd have to do some more maths on T to get the index range between 0 and 2. Easy enough. That's assuming you want to stick with using an array to store the latest 3 data points you have and plotting only them. I'll come back to that in a minute.

    The error you are getting has me wondering if you have a Chart Control on your Form2, and if you have 'Series 1' in its Series collection? If you do have a Chart Control on Form2, I'd try deleting it and adding a fresh one.

    Now, I've changed the code on Form2 (been having a nose around the available methods outlined in the MSDN) and come up with an example of the effect I'm assuming you want. Please say if it's not right.

    vb Code:
    1. Option Strict On
    2. Imports System.Windows.Forms.DataVisualization.Charting
    3.  
    4. Public Class Form2
    5.  
    6.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         ' Chart1.Series.Add("Series2")
    8.  
    9.         Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    10.         ' Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
    11.  
    12.         ' Chart1.Series(0).Label = "PSS"
    13.         ' Chart1.Series(1).Label = "USS"
    14.  
    15.     End Sub
    16.  
    17.     Public Sub UpdateChart(ByVal T As Integer, ByVal NewY As Integer)
    18.  
    19.         If Me.Visible = True Then
    20.  
    21.             Label1.Visible = False
    22.  
    23.             TextBox31.Text = T.ToString
    24.  
    25.             Chart1.ChartAreas(0).AxisX.Minimum = T - 7
    26.             Chart1.ChartAreas(0).AxisX.Maximum = T + 1
    27.             Chart1.Series(0).Points.AddY(NewY)
    28.             Chart1.Series(0).Points(Chart1.Series(0).Points.Count - 1).Label = NewY.ToString
    29.  
    30.         End If
    31.  
    32.     End Sub
    33.  
    34. End Class

    It's adding the data (as it becomes available) to the Chart's Series DataPoint Collection and modifying the XAxis Min and Max values which gives the desired scrolling effect.

    The big down side is I have no idea how many points you can add before it throws a wobbler. At some point it's going to run out of memory, although I suspect it will not actually be a problem unless it's left running for several days (pure guess).

    The advantage of the earlier array approach is that you only store a fixed set of points using a fixed amount of memory; But it's a bit more complex (you have to shuffle them off one end to make space to add the new point; adjust indexing values; clear the Chart points; etc).

    Well, as I say, Charts are new to me and I'm just trying to help you get some code that works and give you a few ideas. Maybe you can adapt the two diferent approaches to one that is reliable without your head exploding

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: graphing makes my head hurt...

    By the way, I just wanted to say thanks for being patient and explaining things well, im definitely further then I would of been on my own : P

    Alright, with that being said...

    Your code is awesome.

    That is exactly what I am trying to do in every way, shape, and form.

    not only that but it makes no sense how you did it in so few lines.

    Seriously though, Im looking at your code and how does it remember all the previous yvalues???

    one thing that I am noticing though, if you close form2, the graph will not reappear again.

    So it will just keep storing updatechart(x,y) as long as it keeps going?

    but holy crap man, genius
    Last edited by Zmcpherson; Oct 7th, 2011 at 07:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width