Question how to use array's in a chart?

Tylerdurden84

New member
Joined
Jul 11, 2012
Messages
2
Programming Experience
Beginner
I'm currently working on my first real program, but I'm currently stuck due to my poor programming skills.

Scenario:
Ive created a chart; that's linked to a specified array "Say: array_1" this is now operational and fully functional.
However I'd like to use the chart to display information as required; based on the selected item from a listbox.

The problem I'm facing however is that the data I'd like to show in the chart needs to be pulled from different array's. say array 1; array 2 ; array 3 etc.
I'm familiar with the use of getting data from within a specified array, but not how to get the data from different array's.

My question is what's the best way to realize this?
 
Well the good news is that you can simply add the arrays to the ListBox and use them from there, eg. ...

VB.NET:
Public Class Form1
    Dim x(12, 12) As String
    Dim y(12, 12) As Int16


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        x(0, 0) = "abc"
        y(0, 0) = 12
        ListBox1.Items.Add(x)
        ListBox1.Items.Add(y)
        Label1.Text = ListBox1.Items(0)(0, 0) & (ListBox1.Items(1)(0, 0) + 1)


    End Sub


End Class

The not so good news is that they won't be identifiable to the user so you can use my sneaky double listbox trick (making sure that both listboxes are unsorted before we start!)... in ListBox1, which is not visible, you add the arrays, and simultaneously in ListBox2 which is visible you add a user friendly name like "Set of stuff 1" or whatever. When the user selects any item in the visible list box you use the SelectedIndexChanged event to set the same item selected in the invisible one and this is the one you use to fill the chart.
 
Thanks, but I'dont think I'm there yet.
I've included part of the class to make the situation more clear.

VB.NET:
    Sub startTrending1()
        ' Disable all controls on the form
        base_form.startTrending.Enabled = False
        ' and only Enable the Stop button
        base_form.stoptrending.Enabled = True

        Dim addDataThreadStart As New System.Threading.ThreadStart(AddressOf AddDataThreadLoop)
        base_form.addDataRunner = New System.Threading.Thread(addDataThreadStart)
        base_form.addDataDel = New base_form.AddDataDelegate(AddressOf AddData)

        ' Predefine the viewing area of the chart
        minValue = base_form.doorlopende_teller_vanaf_start
        maxValue = minValue + 120

        base_form.Mainchart.ChartAreas(0).AxisX.Minimum = minValue
        base_form.Mainchart.ChartAreas(0).AxisX.Maximum = maxValue

        ' Reset number of series in the chart.
        base_form.Mainchart.Series.Clear()

        Call startTrending_count_no_series()
        base_form.addDataRunner.Start()

    End Sub 'startTrending_Click 

    Sub stopTrending1()

        If Not base_form.addDataRunner Is Nothing Then

            If base_form.addDataRunner.IsAlive = True Then
                base_form.addDataRunner.Abort()
            End If

        End If

        ' Enable all controls on the form
        base_form.startTrending.Enabled = True
        ' and only Disable the Stop button
        base_form.stoptrending.Enabled = False
    End Sub 'stopTrending_Click

    '/ Main loop for the thread that adds data to the chart.
    '/ The main purpose of this function is to Invoke AddData
    '/ function every 1000ms (1 se
    Private Sub AddDataThreadLoop()
        While True
            base_form.Mainchart.Invoke(base_form.addDataDel)
            System.Threading.Thread.Sleep(1000)
        End While
    End Sub 'AddDataThreadLoop

    Public Sub AddData()
        Dim timeStamp As Double = base_form.doorlopende_teller_vanaf_start

        Dim ptSeries As System.Windows.Forms.DataVisualization.Charting.Series
        For Each ptSeries In base_form.Mainchart.Series
            AddNewPoint(timeStamp, ptSeries)
        Next ptSeries
    End Sub 'AddData

    '/ The AddNewPoint function is called for each series in the chart when
    '/ new points need to be added.  The new point will be placed at specified
    '/ X axis (Date/Time) position with a Y value in a range +/- 1 from the previous
    '/ data point's Y value, and not smaller than zero.
 
   Public Sub AddNewPoint(ByVal timeStamp As Double, ByVal ptSeries As System.Windows.Forms.DataVisualization.Charting.Series)

        ' Add new data point to its series.
        ptSeries.Points.AddXY(timeStamp, [COLOR=#0000ff][SIZE=3][B]array_account_info(89, 1))[/B][/SIZE][/COLOR][COLOR=#0000FF]
[/COLOR]
        ' remove all points from the source series older than 1.5 minutes.

        Dim removeBefore As Double = base_form.doorlopende_teller_vanaf_start - 90 '* 30
        'remove oldest values to maintain a constant number of data points
        While ptSeries.Points(0).XValue < removeBefore
            ptSeries.Points.RemoveAt(0)
        End While

        ' dit is nu de leading plot
        If ptSeries.Name = "Series1" Then
            base_form.Mainchart.ChartAreas(0).AxisX.Minimum = ptSeries.Points(0).XValue
            base_form.Mainchart.ChartAreas(0).AxisX.Maximum = base_form.doorlopende_teller_vanaf_start + 120 ' * 30



            base_form.Mainchart.ChartAreas(0).AxisY.Minimum = ptSeries.Points.FindMinByValue("Y1", 0).YValues(0) - 10
            base_form.Mainchart.ChartAreas(0).AxisY.Maximum = ptSeries.Points.FindMaxByValue("Y1", 0).YValues(0) + 10

            base_form.Mainchart.Invalidate()
        End If

    End Sub 'AddNewPoint

End Class

The problem I'm facing has to do with the specified array array_account_info(89, 1)) highlighted in blue.
The above class is operational, however I'd like it to be able to switch data sources in runtime -> via listbox events
I'm familiar with the SelectedIndexChanged event, however the format I'm getting is in string "array_account_info", that's of course not accepted by this routine, is there a workaround?
 
Not really sure how you're getting a string. I've made my example more specific in the hope that it covers the problem but shout if it doesn't.

VB.NET:
Public Class Form1


    Dim x(12, 12) As String
    Dim y(12, 12) As Int16




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

        x(0, 0) = "abc"
        y(0, 0) = 12
        ListBoxInvisible.Items.Add(x)
        ListBoxInvisible.Items.Add(y)
        ListBoxVisible.Items.Add("This is where x is")
        ListBoxVisible.Items.Add("This is where y is")




    End Sub


    Private Sub ListBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBoxVisible.SelectedIndexChanged

        Label1.Text = ListBoxInvisible.Items(ListBoxVisible.SelectedIndex)(0, 0)

    End Sub
End Class
 
Back
Top