What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse MSChart to graph data in a text file
KeywordsMSChart, graph, data file, chart
CategoriesGraphics, Controls
When the program starts, it calls subroutine LoadData to load the data in a text file. LoadData opens the file and reads the number of data points and the number of columns of data. It then reads the data into an array.
Private Values() As Single
Private NumPoints As Integer
Private NumYs As Integer

Private Sub LoadData()
Dim fnum As Integer
Dim fname As String
Dim pt_num As Integer
Dim val_num As Integer
Dim X As Single

    ' Open the file.
    fnum = FreeFile
    fname = App.Path
    If Right$(fname, 1) <> "\" Then fname = fname & "\"
    fname = fname & "points.dat"
    Open fname For Input As #fnum

    ' Read the number of values.
    Input #fnum, NumPoints, NumYs
    ReDim Values(1 To NumPoints, 1 To 2 * NumYs)

    ' Read the data.
    For pt_num = 1 To NumPoints
        Input #fnum, X
        For val_num = 0 To NumYs - 1
            Values(pt_num, 2 * val_num + 1) = X
            Input #fnum, Values(pt_num, 2 * val_num + 2)
        Next val_num
    Next pt_num
    Close #fnum
End Sub

After loading the data, the program uses the following code to send the data to the MSChart control. It uses the VtChChartType2dXY chart type to give each data set its own X and Y values.

' Send the data to the chart.
Chart1.chartType = VtChChartType2dXY
Chart1.RowCount = NumPoints
Chart1.ColumnCount = 2 * NumYs
Chart1.ChartData = Values
My book Custom Controls Library implements a control that does simple graphics without the MSChart control.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.