• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

MSchart control: Source data is from a text file. And then use the MSchart to represent the data distribution.

I have a text file with 2 columns and about 2000 rows of value. It looks like below

col_1 col_2
709.0 1220.0
391.0 1181.0
295.0 303.0
289.0 1303.0
2714.0 303.0
2745.0 1303.0
261.0 1254.0
And so on…

I want to use MS chart control to draw a chart. The chart shows the 2 column's values in 2 different colors. The chart doesn't need to be too fancy and complicated (just need show the chart after load the form). And
because there so many rows of data in the text file. Can ms chart control adjust the scale to let the chart looks not too crowded (just need manual change the in coding place)?
2 Solutions
Leo EikelmanDirector, IT and Business DevelopmentCommented:
I found this code example


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


you can find the source and app here


This tutorial might help as well



hsuyfAuthor Commented:
I found this example. but i still have problem to implement my project. Could you help me more about my example?

Try this ...
After the file is imported

Cells(1,1). Activate ' puts you in the upper most cell where the col header is
MyRange = ActiveCell.CurrentRegion.Address ' Creates a range Address  bordered by empty col and rows
Charts(1).Seriescollection.Add Source = ActiveWorksheet.Range(MyRAnge) 'Creates the Chart

'After the chart is created'

Charts(1).Activate ' makes the new chart Active
With ActiveChart
      .Charttype = XLLine  'makes sure you have a line chart (you can use whatever you want e.g. xlbar...)
      .Axes(xlcategory).majorunitisAuto = True

You can add other modifications (titiles etc within the with object to automate changes to your chart.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now