locked
How to draw Graph from a large text(.txt) file? RRS feed

  • Question

  • Hi Everyone,

    I have a text(.txt) file which has around 30,000 rows and 2 columns. I am using below code to draw a line chart but program throws "System.OutOfMemory" exception!!

    How can this problem be resolved?
    Any suggestion is much appreciated.

    Dim

    xs As New List(Of Double)()

     

    Dim ys As New List(Of Double)()

     

    Try

     

    Using reader As New StreamReader("E:\rez.txt")

     

    Dim line As String = reader.ReadLine()

     

    While Not line Is Nothing

     

    Dim values As String() = line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)

    xs.Add(

    Double.Parse(values(0).Trim()))

    ys.Add(

    Double.Parse(values(1).Trim()))

     

    End While

     

    End Using

    Chart1.Series(0).Points.DataBindXY(xs, ys)

    Chart1.Series(0).ChartType = SeriesChartType.FastLine

    Chart1.Series(0).ChartArea = Chart1.ChartAreas(0).Name

     

    Catch ex As Exception

    MessageBox.Show(ex.Message)

     

    End Try

    Tuesday, March 16, 2010 7:21 AM

Answers

  • You have an infinite loop there. Set line = reader.ReadLine() inside the while-loop.

    Here's the code without the using-statement, the databinding and the x- and y-arrays:

    Try
    
    	'no need for the using-statement
    	Dim reader As New System.IO.StreamReader("E:\rez.txt")
    
    	Dim values As String()
    
    	Dim line As String = reader.ReadLine()
    
    	While Not line Is Nothing
    
    		values = line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    
    		'just add the points to the series
    		Chart1.Series(0).Points.AddXY(Double.Parse(values(0).Trim()), Double.Parse(values(1).Trim()))
    
    		'update line
    		line = reader.ReadLine()
    
    	End While
    
    	Chart1.Series(0).ChartType = SeriesChartType.FastLine
    	Chart1.Series(0).ChartArea = Chart1.ChartAreas(0).Name
    
    Catch ex As Exception
    
    	MessageBox.Show(ex.Message)
    
    End Try
    Tuesday, March 16, 2010 9:43 AM
    Moderator

All replies

  • You have an infinite loop there. Set line = reader.ReadLine() inside the while-loop.

    Here's the code without the using-statement, the databinding and the x- and y-arrays:

    Try
    
    	'no need for the using-statement
    	Dim reader As New System.IO.StreamReader("E:\rez.txt")
    
    	Dim values As String()
    
    	Dim line As String = reader.ReadLine()
    
    	While Not line Is Nothing
    
    		values = line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    
    		'just add the points to the series
    		Chart1.Series(0).Points.AddXY(Double.Parse(values(0).Trim()), Double.Parse(values(1).Trim()))
    
    		'update line
    		line = reader.ReadLine()
    
    	End While
    
    	Chart1.Series(0).ChartType = SeriesChartType.FastLine
    	Chart1.Series(0).ChartArea = Chart1.ChartAreas(0).Name
    
    Catch ex As Exception
    
    	MessageBox.Show(ex.Message)
    
    End Try
    Tuesday, March 16, 2010 9:43 AM
    Moderator
  • Thank You,
    It Worked!
    Tuesday, March 16, 2010 11:22 AM
  • Thank You,
    It Worked!


    When someone answers your question, please mark it as the answer.

    Thanks!


    Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)
    • Proposed as answer by Tomás Aranda Thursday, February 9, 2012 12:51 PM
    Saturday, January 21, 2012 3:12 AM