ChartDirector 6.0 (ASP/COM/VB Edition)

Reading Data from Text File


For data that are in text files, simply read them and parse them into array variables. Once the data is in array variables, they can be used with the ChartDirector API.

The exact details of reading and parsing text files depend on the format of the text files. The following provides a simple example.

Suppose your text file is in comma delimited text format, with each line representing a record, like the followings:

Jan,90,47
Feb,67,69
Mar,87,46
Apr,26,95
May,67,99
Jun,65,22
Jul,32,43
Aug,67,78
Sep,46,14
Oct,48,56
Nov,93,16
Dec,36,44

To read the above into array variables, you may use the following code:

'
'The following code is in VBScript and is designed to run on web servers.
'If you are using Visual Basic, you can use the Visual Basic "Open" function
'to open the text file instead of FileSystemObject.
'
Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.OpenTextFile(Server.MapPath("testdata.txt"))
'
'Assume we do not know the number of rows. Simply declare the array size
'using the worst case.
'
ReDim col0(1000)
ReDim col1(1000)
ReDim col2(1000)
'
'Read in the text file line by line, split the line, and put the result
'in the array variables
'
i = 0
Do While Not myFile.AtEndOfStream
 line = myFile.ReadLine()
 If line <> "" Then
  fields = Split(line, ",")
  col0(i) = fields(0)
  col1(i) = fields(1)
  col2(i) = fields(2)
  i = i + 1
  If i > UBound(col0) Then Exit Do
 End If
Loop
Call myFile.Close()
'
'Now we know the exact number of rows, so we can trim the array.
'
ReDim Preserve col0(i - 1)
ReDim Preserve col1(i - 1)
ReDim Preserve col2(i - 1)