Creating a line graph from a text file

effundo
effundo used Ask the Experts™
on
Hi everyone,

Im wondering if you could help me, What i'm needing to do is extract a range of values from a text file and use these values to create a line graph. I have the ZenGraph Sample up and running. I'm just struggling with how to tie them both together.

Here is a part of my text file below

Box temp invalid     ETI 547493
Accumulated charge = 204 Ah, accumulated discharge = 105 Ah
547493,3.361,3.348,3.350,3.349,3.360,3.360,3.350,29.0,28.8,29.6,29.4,29.8,28.6,29.2,0,0,0,0,0,0,0,-1.130,196.280,-506,981
Total volts 269.9 V
Total Current -1.096 A

The values i am needing to extract are "29.0,28.8,29.6,29.4,29.8,28.6,29.2" from the third line.

Then once i have these temperature values how do i plot these on the Graph.

Thanks in Advance.



'This part of the code plots only once
Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
        Dim myPane As GraphPane = zgc.GraphPane
 
        ' Set the title and axis labels
        myPane.Title.Text = "Thermistor Test"
        myPane.XAxis.Title.Text = "Cells X"
        myPane.YAxis.Title.Text = "Temperature Y"
 
        ' Make up some data arrays based on the Sine function
        Dim list1 As New PointPairList()
        '  Dim list2 As New PointPairList()
        Dim i As Integer, x As Double, y1 As Double
        For i = 0 To 1
            x = string
            y1 = 29.0
 
            list1.Add(x, y1)
 
        Next i
 
        Dim myCurve As LineItem = myPane.AddCurve("Temperature", _
           list1, Color.Red, SymbolType.Diamond)
 
        zgc.AxisChange()
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
Hi CodeCruiser,

Thanks for the links.

I now know how to create the line graph using the values.

Im still not sure how to extract the values im needing from the text file.

Box temp invalid     ETI 547493
Accumulated charge = 204 Ah, accumulated discharge = 105 Ah
547493,3.361,3.348,3.350,3.349,3.360,3.360,3.350,29.0,28.8,29.6,29.4,29.8,28.6,29.2,0,0,0,0,0,0,0,-1.130,196.280,-506,981
Total volts 269.9 V
Total Current -1.096 A

The values i am needing to extract are "29.0,28.8,29.6,29.4,29.8,28.6,29.2" from the third line.

Any Idea's would be helpful
Thankyou
Most Valuable Expert 2012
Top Expert 2014

Commented:
Unless you have some condition such as starting from 9th value in the third row and taking next 7 values, it would be very difficult. Is it one record in the file? Would there be multiple records? Please explain the file structure.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Hi CodeCruiser,

What i am doing is taking a snapshot from a Hyper Terminal feed and creating a txt file from this.

These values im after will never be on the same line as the snapshot could be taken at any time during the feed.

Here is a typical text file that i am working from.
These values i am needing always come after "Accumulated charge = 204 Ah, accumulated discharge = 105 Ah"  line if thats any help.

I would only need these values from one of the lines, does not matter which.

TEXTFILE

Slv:1 sts = 0[17] Rx 89571, bad 0
Slv:2 sts = 0[17] Rx 89571, bad 0
Slv:3 sts = 0[17] Rx 89571, bad 0
Slv:4 sts = 0[17] Rx 89571, bad 0
Slv:5 sts = 0[17] Rx 89571, bad 0
Slv:6 sts = 0[17] Rx 89571, bad 0
State = Idle  
Total volts 269.9 V
Total Current -1.130 A
SOC 98.1 %
Charge 196.280 Ah
Errors: 000000000000000000000000000000000  [ ]
Critical errors: 000000000000
Safety faults: 00000000[ ]
Last error No 5    
Balance off
Charger PWM: 0
CC SOC: -506, SE SOC: 0.C flag: 1.
Max Volts 3.924 at cell [48]    Min Volts 3.347 at cell [68]
Max cell temp 33.6 at cell [39]    Min Temp 26.6 at cell [1]
CC raw main:16384 aux:8192  CC I raw main:16384 aux:0
CC I calc main:0.819  aux:403.865   ADC I main:5540 aux:5609
ADC I inst main:22112   aux:22400
ADC I calc main:-1.142A aux:0.012A (5 second peak:1.220)
Used charge (soft)=98.917 Ah
Box temp invalid     ETI 547493
Accumulated charge = 204 Ah, accumulated discharge = 105 Ah
547493,3.361,3.348,3.350,3.349,3.360,3.360,3.350,29.0,28.8,29.6,29.4,29.8,28.6,29.2,0,0,0,0,0,0,0


      --------      ========      --------
Slv:1 sts = 0[17] Rx 89572, bad 0
Slv:2 sts = 0[17] Rx 89572, bad 0
Slv:3 sts = 0[17] Rx 89572, bad 0
Slv:4 sts = 0[17] Rx 89572, bad 0
Slv:5 sts = 0[17] Rx 89572, bad 0
Slv:6 sts = 0[17] Rx 89572, bad 0
State = Idle  
Total volts 269.9 V
Total Current -1.096 A
SOC 98.1 %
Charge 196.280 Ah
Errors: 000000000000000000000000000000000  [ ]
Critical errors: 000000000000
Safety faults: 00000000[ ]
Last error No 5    
Balance off
Charger PWM: 0
CC SOC: -506, SE SOC: 0.C flag: 1.
Max Volts 3.924 at cell [48]    Min Volts 3.347 at cell [68]
Max cell temp 33.6 at cell [39]    Min Temp 26.6 at cell [1]
CC raw main:16384 aux:8192  CC I raw main:16384 aux:0
CC I calc main:0.819  aux:403.865   ADC I main:5540 aux:5610
ADC I inst main:22176   aux:22464
ADC I calc main:-1.116A aux:0.020A (5 second peak:1.220)
Used charge (soft)=98.917 Ah
Box temp invalid     ETI 547494
Accumulated charge = 204 Ah, accumulated discharge = 105 Ah
547494,3.361,3.348,3.350,3.349,3.360,3.360,3.350,29.0,28.8,29.6,29.4,29.8,28.6,29.2,0,0,0,0,0,0,0


      --------      ========      --------
Slv:1 sts = 0[17] Rx 89573, bad 0
Slv:2 sts = 0[17] Rx 89573, bad 0
Slv:3 sts = 0[17] Rx 89573, bad 0
Slv:4 sts = 0[17] Rx 89573, bad 0
Slv:5 sts = 0[17] Rx 89573, bad 0
Slv:6 sts = 0[17] Rx 89573, bad 0
State = Idle  
Total volts 269.9 V
Total Current -1.007 A


Thanks

Most Valuable Expert 2012
Top Expert 2014

Commented:
I have quickly wrote the following code snippet. Give it  a try. Values(8) to Values(14) is what you are looking for



            Dim Found As Boolean
            Dim RequiredLine As String
            For Each Line In IO.File.ReadAllLines("path")
                If Found Then
                    RequiredLine = Line
                    Exit For
                End If
                If Line = "Accumulated charge = 204 Ah, accumulated discharge = 105 Ah" Then
                    Found = True
                End If
            Next
            Dim Values As String() = RequiredLine.Split(",")

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Another example...
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Accumulated charge = 204 Ah, accumulated discharge = 105 Ah
        ' 547494,3.361,3.348,3.350,3.349,3.360,3.360,3.350,29.0,28.8,29.6,29.4,29.8,28.6,29.2,0,0,0,0,0,0,0
        '                                                  ---------------------------------- 
        ' Need Index 8 thru 14 from line following that which begins with "Accumulated charge ="
 
        Dim DataFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "someFile.txt")
        Dim values As List(Of Double) = GetValues(DataFile)
        If Not IsNothing(values) Then
            For i As Integer = 0 To values.Count - 1
                Debug.Print(i & ": " & values(i).ToString("0.0"))
            Next
        End If
    End Sub
 
    Private Function GetValues(ByVal DataFile As String) As List(Of Double)
        Dim values As New List(Of Double)
        Dim lines() As String = System.IO.File.ReadAllLines(DataFile)
        For i As Integer = 0 To lines.GetUpperBound(0)
            If lines(i).Trim.ToLower.StartsWith("accumulated charge =") Then
                If i < lines.GetUpperBound(0) - 1 Then
                    Dim strValues() As String = lines(i + 1).Split(",")
                    Try
                        Dim dblValues() As Double = Array.ConvertAll(strValues, New Converter(Of String, Double)(AddressOf StrToDouble))
                        For x As Integer = 8 To 14
                            values.Add(dblValues(x))
                        Next
                        Return values
                    Catch ex As Exception
                    End Try
                End If
            End If
        Next
        Return Nothing
    End Function
 
    Private Function StrToDouble(ByVal str As String) As Double
        Return Double.Parse(str)
    End Function
 
End Class

Open in new window

Author

Commented:
Hi Idle Mind,

That code worked great.
Im struggling to use these values to create my chart tho.

i guess im needing to split these values up not sure how to do that.

For my chart to work i just need to put these values in this line of code below somehow.

So it would be something like:

Dim Sales() As Integer = {29.0,28.8,29.6,29.4,29.8,28.6,29.2}

Or

Dim Sales() As Integer = values

Thanks for all your help so far.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Here is the code for that
Check the last line. Values(8) to Values(14) is what you would use to create the graph.


            Dim Found As Boolean
            Dim RequiredLine As String
            For Each Line In IO.File.ReadAllLines("path")
                If Found Then
                    RequiredLine = Line
                    Exit For
                End If
                If Line = "Accumulated charge = 204 Ah, accumulated discharge = 105 Ah" Then
                    Found = True
                End If
            Next
            Dim Values As Integer() = RequiredLine.Split(",")

Open in new window

Author

Commented:
Hi Codecruiser,

The last line of your code does not seem to work for me i keep getting this error below.

Error      1      Value of type '1-dimensional array of String' cannot be converted to '1-dimensional array of Integer' because 'String' is not derived from 'Integer'.

This is where i am at the moment, i have the list of numbers that i need which are *Dim values As List(Of Double)* which Idle Mind created in his code.

So the Values look like this

(0) 29.0
(1) 28.8
(2) 29.6
(3) 29.4
(4) 29.8
(5) 28.6
(6) 29.2

When i try to use these values like this "Dim Sales() As Integer = values" i would recieve this error below

Error      1      Value of type 'System.Collections.Generic.List(Of Double)' cannot be converted to '1-dimensional array of Integer'.

I guess i need to convert the List(Of Double) into something that i can work with.

I just need a way to make them usable to put in my line of code below.

Dim Sales() As Integer = {29.0,28.8,29.6,29.4,29.8,28.6,29.2}

Many Thanks


Most Valuable Expert 2012
Top Expert 2014
Commented:
Then try this
Dim Sales as Integer() = { Values(8), Values(9), Values(10), Values(11), Values(12), Values(13), Values(14) }

Author

Commented:
Thanks very much for all your help and patience, works great now.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
So you need to convert the Doubles to Integers?...
(note the "helper" function down below)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DataFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "someFile.txt")
        Dim values As List(Of Double) = GetValues(DataFile)
        Dim SalesList As List(Of Integer) = values.ConvertAll(AddressOf DoubleToInt)
        ' ...use "SalesList"...

        ' < or >

        ' If you REALLY need an Array (not a List) then use:
        Dim Sales() As Integer = SalesList.ToArray
        ' ...use "Sales"...
    End Sub

    Private Function DoubleToInt(ByVal dbl As Double) As Integer
        Return Convert.ToInt32(dbl)
    End Function

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial