|
-
Oct 5th, 2011, 06:02 PM
#1
Thread Starter
Addicted Member
graphing makes my head hurt...
so I was able to successfully code this...
vb Code:
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form2
Public Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim theChart As New Chart
Dim lineone As New List(Of Point)
Dim linetwo As New List(Of Point)
Dim theArea As New ChartArea
Dim Series As New Series
lineone.Add(New Point(TextBox1.Text, TextBox3.Text))
lineone.Add(New Point(TextBox2.Text, TextBox4.Text))
'linetwo.Add(New Point(textbox1.text, TextBox5.Text))
'linetwo.Add(New Point(textbox2.text, TextBox6.Text))
theChart.DataSource = lineone 'I want to add linetwo to this
theArea.Name = "chartArea"
theChart.ChartAreas.Add(theArea)
theChart.Series.Clear()
Series.XValueMember = "X"
Series.YValueMembers = "Y"
Series.ChartArea = "chartArea"
theChart.Series.Add(Series)
Me.Controls.Add(theChart)
theChart.Dock = DockStyle.Fill
End Sub
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
-
Oct 6th, 2011, 01:19 AM
#2
Re: graphing makes my head hurt...
here's how to add a second series:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
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)}
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)}
'add points to first series
For x As Integer = 0 To 5
Chart1.Series(0).Points.AddXY(x, s1(x).Y)
Next
'add a second series
Chart1.Series.Add("Series2")
'set both series ChartType
Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
'add points to second series
For x As Integer = 0 To 5
Chart1.Series(1).Points.AddXY(x, s2(x).Y)
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 6th, 2011, 12:22 PM
#3
Thread Starter
Addicted Member
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)"
-
Oct 6th, 2011, 02:59 PM
#4
Thread Starter
Addicted Member
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:
Imports System.Windows.Forms.DataVisualization.Charting Public Class Form2 Dim chart1 As New Chart Shared Property T As Integer Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged If T <= 2 Then Label1.Visible = False TextBox20.Text = TextBox21.Text TextBox31.Text = TextBox32.Text TextBox21.Text = TextBox22.Text TextBox32.Text = TextBox33.Text TextBox22.Text = Form1.TextBox10.Text TextBox33.Text = Form1.TextBox4.Text Dim s1() As Point = {New Point(T - 2, TextBox20.Text), New Point(T - 1, TextBox21.Text), New Point(T, TextBox22.Text)} Dim s2() As Point = {New Point(T - 2, TextBox31.Text), New Point(T - 1, TextBox32.Text), New Point(T, TextBox33.Text)} For x As Integer = T - 2 To T chart1.Series(0).Points.Add(x, s1(x).Y) Next chart1.Series.Add("Series2") chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line For x As Integer = T - 2 To T chart1.Series(1).Points.Add(x, s1(x).Y) Next chart1.Series(0).Label = "PSS" chart1.Series(1).Label = "USS" End If End Sub Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If Form1.CheckBox1.Checked Then Form1.CheckBox1.Checked = False End Sub End Class
-
Oct 6th, 2011, 03:49 PM
#5
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
 
-
Oct 6th, 2011, 05:14 PM
#6
Thread Starter
Addicted Member
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:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged If Timer1.Enabled = False Then MessageBox.Show("You Must Start The Test First!") Else Form2.Show() End If 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:
Imports System.Windows.Forms.DataVisualization.Charting Public Class Form2 Dim chart1 As New Chart Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Integer.TryParse(Me.TextBox11.Text, Form1.T) End Sub Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged If Form1.T >= 2 Then Label1.Visible = False TextBox20.Text = TextBox21.Text TextBox31.Text = TextBox32.Text TextBox21.Text = TextBox22.Text TextBox32.Text = TextBox33.Text TextBox22.Text = Form1.TextBox10.Text TextBox33.Text = Form1.TextBox4.Text 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)} For x As Integer = Form1.T - 2 To Form1.T chart1.Series(0).Points.Add(x, s1(x).Y) Next chart1.Series.Add("Series2") chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line For x As Integer = Form1.T - 2 To Form1.T chart1.Series(1).Points.Add(x, s1(x).Y) Next chart1.Series(0).Label = "PSS" chart1.Series(1).Label = "USS" End If End Sub Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If Form1.CheckBox1.Checked Then Form1.CheckBox1.Checked = False End Sub 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.
-
Oct 6th, 2011, 05:32 PM
#7
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
?
-
Oct 6th, 2011, 06:09 PM
#8
Thread Starter
Addicted Member
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
-
Oct 6th, 2011, 06:32 PM
#9
Thread Starter
Addicted Member
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...
-
Oct 6th, 2011, 06:44 PM
#10
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 )
-
Oct 7th, 2011, 12:25 PM
#11
Thread Starter
Addicted Member
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:
Public Sub UpdateChart(ByVal T As Integer, ByVal X As Integer, ByVal Y As Integer) If Me.Visible = True And T > 2 Then Label1.Visible = False TextBox20.Text = TextBox21.Text TextBox31.Text = TextBox32.Text TextBox21.Text = TextBox22.Text TextBox32.Text = TextBox33.Text TextBox22.Text = Form1.TextBox10.Text TextBox33.Text = Form1.TextBox4.Text Dim s1() As Point = {New Point(T - 2, CInt(TextBox20.Text)), New Point(T - 1, CInt(TextBox21.Text)), New Point(T, CInt(TextBox22.Text))} Dim s2() As Point = {New Point(T - 2, CInt(TextBox31.Text)), New Point(T - 1, CInt(TextBox32.Text)), New Point(T, CInt(TextBox33.Text))} For X = T - 2 To T chart1.Series(0).Points.Add(X, s1(X).Y) Next chart1.Series.Add("Series2") chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line For X = T - 2 To T chart1.Series(1).Points.Add(X, s1(X).Y) Next chart1.Series(0).Label = "PSS" chart1.Series(1).Label = "USS" End If 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
-
Oct 7th, 2011, 01:24 PM
#12
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:
chart1.Series.Add("Series2") chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line chart1.Series(0).Label = "PSS" 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:
For X = T - 2 To T chart1.Series(0).Points.Add(X, s1(X).Y) 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
-
Oct 7th, 2011, 01:52 PM
#13
Thread Starter
Addicted Member
Re: graphing makes my head hurt...
alright, im going back and starting from the beginning :
vb Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
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)}
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)}
'add points to first series
For x As Integer = 0 To 5
chart1.Series(0).Points.AddXY(x, s1(x).Y)
Next
'add a second series
chart1.Series.Add("Series2")
'set both series ChartType
chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
'add points to second series
For x As Integer = 0 To 5
chart1.Series(1).Points.AddXY(x, s2(x).Y)
Next
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?
-
Oct 7th, 2011, 02:06 PM
#14
Re: graphing makes my head hurt...
That actually works for me (no other code on Form2). Where are you getting that error ?
-
Oct 7th, 2011, 02:24 PM
#15
Thread Starter
Addicted Member
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
-
Oct 7th, 2011, 02:34 PM
#16
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:
Option Strict On
Public Class Form1
Dim f2 As New Form2
Dim intY As Integer = 4
Dim intDir As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = 1000
Timer1.Enabled = True
f2.Show()
f2.BringToFront()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static intTime As Integer = 0
intTime += 1
intY += intDir
If intY > 8 Then intDir = -1
If intY < 4 Then intDir = 1
Call f2.UpdateChart(intTime, intY)
End Sub
End Class
vb Code:
Option Strict On
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form2
Private Yvalues() As Integer = {0, 0, 0}
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Chart1.Series.Add("Series2")
Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series(0).Label = "PSS"
Chart1.Series(1).Label = "USS"
End Sub
Public Sub UpdateChart(ByVal T As Integer, ByVal NewY As Integer)
If Me.Visible = True Then
Label1.Visible = False
Yvalues(0) = Yvalues(1)
Yvalues(1) = Yvalues(2)
Yvalues(2) = NewY
TextBox20.Text = Yvalues(0).ToString
TextBox21.Text = Yvalues(1).ToString
TextBox22.Text = Yvalues(2).ToString
TextBox31.Text = T.ToString
Chart1.Series(0).Points.Clear()
For X As Integer = 0 To 2
Chart1.Series(0).Points.AddY(Yvalues(X))
Next
End If
End Sub
End Class
It's not elegant; I'm just wondering if I've understood what you are trying to do. Hope it helps.
-
Oct 7th, 2011, 03:59 PM
#17
Thread Starter
Addicted Member
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?
-
Oct 7th, 2011, 05:24 PM
#18
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:
Option Strict On Imports System.Windows.Forms.DataVisualization.Charting Public Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Chart1.Series.Add("Series2") Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line ' Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line ' Chart1.Series(0).Label = "PSS" ' Chart1.Series(1).Label = "USS" End Sub Public Sub UpdateChart(ByVal T As Integer, ByVal NewY As Integer) If Me.Visible = True Then Label1.Visible = False TextBox31.Text = T.ToString Chart1.ChartAreas(0).AxisX.Minimum = T - 7 Chart1.ChartAreas(0).AxisX.Maximum = T + 1 Chart1.Series(0).Points.AddY(NewY) Chart1.Series(0).Points(Chart1.Series(0).Points.Count - 1).Label = NewY.ToString End If End Sub 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
-
Oct 7th, 2011, 07:14 PM
#19
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|