Link to home
Start Free TrialLog in
Avatar of effundo
effundo

asked on

Creating a line graph from a text file

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

SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of effundo
effundo

ASKER

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
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.
Avatar of effundo

ASKER

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

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of effundo

ASKER

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.
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

Avatar of effundo

ASKER

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


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of effundo

ASKER

Thanks very much for all your help and patience, works great now.
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