Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points
Using Chart control to plot data points Using Chart control to plot data points
Using Chart control to plot data points
Go Back  Xtreme Visual Basic Talk > > > Using Chart control to plot data points


Reply
 
Thread Tools Display Modes
  #1  
Old 05-04-2015, 11:50 AM
BigHairy BigHairy is offline
Newcomer
 
Join Date: Feb 2015
Posts: 4
Default Using Chart control to plot data points


I am new to programming in general and I am trying to create a small application in VB 2013 to graph (and eventually perform a linear regression on) data points supplied by the user via textbox controls. I can get the graph to populate the first time, but after my "Graph Points" button is clicked a second time I keep getting errors. I have tried several different things and the error is either that 'object is not set to an instance of an object' or that my series already exists. Code as it currently stands (which is returning the object instance error) is below. Any suggestions?
Code:
Public Module GlobalVariables
    
    Public Series As String

End Module
Public Class Form1


    Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles CalibrationChart.Click

    End Sub

    Private Sub buttonGraphPoints_Click(sender As Object, e As EventArgs) Handles buttonGraphPoints.Click
        Dim SeriesName As String = TextBox1.Text
        Dim keyvaluepairXYValues As List(Of KeyValuePair(Of Integer, Integer)) = New List(Of KeyValuePair(Of Integer, Integer))
        Dim ElementIndex As Integer = 0

        Do Until ElementIndex = 5 Or Me.Controls.Find("textVolts" + CStr(ElementIndex), True).Length = 0 _
            Or Me.Controls.Find("textFlow" + CStr(ElementIndex), True).Length = 0 Or Me.Controls.Find("textVolts" + CStr(ElementIndex), True)(0).Text = "" _
            Or Me.Controls.Find("textFlow" + CStr(ElementIndex), True)(0).Text = ""

            keyvaluepairXYValues.Add(New KeyValuePair(Of Integer, Integer) _
                          (CInt(Me.Controls.Find("textVolts" + CStr(ElementIndex), True)(0).Text), _
                           CInt(Me.Controls.Find("textFlow" + CStr(ElementIndex), True)(0).Text)))

            ElementIndex += 1
        Loop
        ElementIndex = 0
        If CalibrationChart.Series.IndexOf(SeriesName) = -1 Then
            CalibrationChart.Series.Add(SeriesName)
        End If

        CalibrationChart.Series(SeriesName).ChartType = DataVisualization.Charting.SeriesChartType.Point

        For Each pair As KeyValuePair(Of Integer, Integer) In keyvaluepairXYValues
            'Dim key As Integer = pair.Key
            'Dim value As Integer = pair.Value

            CalibrationChart.Series(SeriesName).Points.AddXY(pair.Key, pair.Value)

            MsgBox("x" & ElementIndex & " = " & pair.Key _
               & vbNewLine & "y" & ElementIndex & " = " & pair.Value)
            ElementIndex += 1
        Next

        CalibrationChart.Series(SeriesName).Dispose()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

Last edited by passel; 05-04-2015 at 04:04 PM. Reason: Added code tags to perhaps make easier to read.
Reply With Quote
  #2  
Old 05-06-2015, 05:06 PM
hDC_0Using Chart control to plot data points hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 559
Default Some debugging "hints" for someone new to programming..

Thanks, passel, for adding the proper formatting..

Just a guess...but I'm not feeling comfortable with this line inside the buttonGraphPoints_Click event:
Code:
CalibrationChart.Series(SeriesName).Dispose()
In general, I don't dispose of things until the app's main form is ready to close down (using the Form Closing Event).

Be careful, though, on relying on the form closing event when using an old .Net framework version, per this page:
Quote:
Prior to the .NET Framework 2.0, the Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application.
If you have validation code in either of these events that must be executed,
you should call the Form.Close method for each open form individually
before calling the Exit method.

Otherwise if you try to re-reference anything (after it's disposed/gone) while the program is still running, then it may yield an object not found error..

Another troubleshooting hint:
The "Private Sub buttonGraphPoints_Click" only has two parameters "sender" and "e".
Neither of these is actually used in the code contained within the sub.
So break out the Do loop and For Next loop into individual subs called from "Private Sub buttonGraphPoints_Click".
This may make it easier to trace what's going on.

Other than that it's hard to debug your code just from a snippet because other controls are involved,
(which means that other things may be happening outside this code snippet, effecting how it functions).

Speaking of hard to debug - global scoped variables (1, 2, 3).

Quote:
Originally Posted by BigHairy
Public Module GlobalVariables
Public Series As String
End Module
Wikipedia has this to say:
Quote:
They are usually considered bad practice precisely because of their non-locality:
a global variable can potentially be modified from anywhere
(unless they reside in protected memory or are otherwise rendered read-only),
and any part of the program may depend on it.
A global variable therefore has an unlimited potential for creating mutual dependencies,
and adding mutual dependencies increases complexity. See action at a distance.
Global variables also make it difficult to integrate modules because software written by others
may use the same global names unless names are reserved by agreement, or by naming convention.


In this StackOverFlow thread, they are a little more generous, citing Steve McConnell, the author of Code Complete.

Section 13.3 of the Code Complete book, though, talks about using access routines (page 340 onward).

Good (recommended) reading.
If you feel you have to use global variables, then it's good to know the risks and how to take precautions.

On a final side note..

Quote:
Originally Posted by BigHairy
I am trying to create a small application in VB 2013 to graph (and eventually perform a linear regression on) data points supplied by the user
You do know that about Forecasting Formula (Chart Controls), which has, as it's first parameter, "RegressionType", that can be "Linear"?
This StackOverFlow thread has some relevant info also.

Last edited by hDC_0; 05-06-2015 at 06:19 PM.
Reply With Quote
  #3  
Old 05-08-2015, 08:28 AM
BigHairy BigHairy is offline
Newcomer
 
Join Date: Feb 2015
Posts: 4
Default

This entire response was hugely helpful. I have some more reading and work to do before I can totally debug and complete this project, but thank you for all the helpful links and insights!
Reply With Quote
Reply

Tags
chart, graph, plot, series, xy coordinate


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points Using Chart control to plot data points
Using Chart control to plot data points
Using Chart control to plot data points
 
Using Chart control to plot data points
Using Chart control to plot data points
 
-->