View Single Post
Old 05-04-2015, 11:50 AM
BigHairy BigHairy is offline
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?
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
        ElementIndex = 0
        If CalibrationChart.Series.IndexOf(SeriesName) = -1 Then
        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

    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