Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > Datas in a bar graph
 

Datas in a bar graph

Hoi,

i want to display numeric datas from a text-file.
each row contains only one numeric data i.e.
11,20
12,01
10,98

etc.

How can i read this textfile row by row and display each data in a bar graph ?
Only the last 30 datas should be display, otherwise the bar graph will get to large.

And what i have to do if the textfile only contains 15 datas (i.e. in the beginning of catchin gthose datas = ?)

Doei
Franz
Trixi-N  Friday, October 16, 2009 7:24 PM
@Dave

thanks - what I showed was what I have seen in a lot of the MSDN Help pages. Like I said, not real familiar with CultureInfo stuff. I thought that would work.



Hi Franz

I just realized my error

Remember. in my original code I thought the csv file was Integers separated by commas. So I never updated the code when reading the lines.

So change it to this:

While Not sr.EndOfStream
    lstY.Add(CDbl(sr.ReadLine))                    
End While
notice the CDbl instead of CInt. Sometimes it is the simplest things.

then it should work fine :)

  • Marked As Answer byTrixi-N Monday, October 19, 2009 6:42 PM
  •  
jwavila  Sunday, October 18, 2009 6:49 PM
Franz

there is a ChartControl Forum that you can also post questions

http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/threads

but, hopefully this will help get you started. I'm assuming your first numbers in each textfile line are to be in order (eg 10,11,12, etc). And I only did this with 12 lines in the textfile, so you'll have to adjust where it says 10 for the Count.

This is a very, very basic example :(


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        Dim lstX As New List(Of Integer)
        Dim lstY As New List(Of Integer)

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
            Try
                Using reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
                    reader.TextFieldType = FileIO.FieldType.Delimited
                    reader.SetDelimiters(",")
                    While Not reader.EndOfData
                        Dim Fields() As String = reader.ReadFields
                        lstX.Add(CInt(Fields(0)))
                        lstY.Add(CInt(Fields(1)))
                    End While
                End Using
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End If

        If Not (lstX.Count > 10) Then
            Chart1.Series(0).Points.DataBindXY(lstX, lstY)
        Else
            Dim lstShortX As New List(Of Integer)
            Dim lstShortY As New List(Of Integer)
            For i As Integer = lstX.Count - 10 To lstX.Count - 1
                lstShortX.Add(lstX(i))
                lstShortY.Add(lstY(i))
            Next
            Chart1.Series(0).Points.DataBindXY(lstShortX, lstShortY)
        End If
    End Sub

jwavila  Friday, October 16, 2009 9:16 PM
Hoi,

thx for your reply - this works so far.
i changed the "10" to "29", also in the part below.

But -as i am in germany- my datas look like this:
11,41
12,20
7,63
9,62
9,63
7,91
9,69
11,6
10,5
7,17
11,8

So, with your example, only the last part is shown because the delimter is set to ","
When i set it to nothing an error occurs, and when i set the variable to Double instead of Integer also an error occurs.

I do not really need the parts behind the ",", but would be nice.

How can i manage it to show the datas as in my txt-file ?

Doei
Franz
Trixi-N  Saturday, October 17, 2009 2:32 PM
.....or without using a chart control. I just used a textfile containing the numbers 1 to 10 repeating for testing. You would have to adjust the scaling to suit your data.

Public Class Form1

Dim Data As New List(Of Double)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Size = New Size(700, 400)

Dim SR As New IO.StreamReader("data.txt")

Dim Line As String

Dim Number As Double

Do While Not SR.EndOfStream

Line = SR.ReadLine

If Double.TryParse(Line, Number) Then

Data.Add(Number)

End If

Loop

SR.Close()

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim P As New Pen(Color.Blue, 15)

Dim GraphStart, GraphEnd As Integer

If Data.Count > 30 Then

GraphStart = Data.Count - 31

GraphEnd = Data.Count - 1

Else

GraphStart = 0

GraphEnd = Data.Count - 1

End If

For count As Integer = 0 To GraphEnd - GraphStart

e.Graphics.DrawLine(P, 20 + 20 * count, 300, 20 + 20 * count, 300 - CSng(Data(count + GraphStart) * 20))

Next

P.Dispose()

End Sub

End Class

Dave299  Saturday, October 17, 2009 2:59 PM
Oops - sorry, when I saw the 11,24, I thought there were 2 values per line separated by a comma - hence the TextFieldParser

so, since each of your lines is : 11,24 = 11.24 in the US

SOhere is how you can read each line and chart them. It's similar to the above without splitting the lines.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        Dim lstY As New List(Of Double)

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
            Try
                Using sr As StreamReader = New StreamReader(ofd.FileName)
                    While Not sr.EndOfStream
                        lstY.Add(CInt(sr.ReadLine))
                    End While
                End Using
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End If

        If Not (lstY.Count > 10) Then
            Chart1.Series(0).Points.DataBindY(lstY)
        Else
            Dim lstShortY As New List(Of Double)
            For i As Integer = lstY.Count - 10 To lstY.Count - 1
                lstShortY.Add(lstY(i))
            Next
            Chart1.Series(0).Points.DataBindY(lstShortY)
        End If
    End Sub
@Dave

Hey Dave :)
I don't know much about Globalization.NumberFormatInfo. I tried changing it to "de-DE" so I could try the textfile with values like 11,24 - using the comma for the decimal point. But I couldn't get it to work... What am I doing wrong?

Joe


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim nfi As NumberFormatInfo = New CultureInfo("de-DE", True).NumberFormat
        nfi.NumberDecimalSeparator = ","
    End Sub
jwavila  Saturday, October 17, 2009 4:15 PM
Hoi Friends,

this works also, but - next Problem.
The datas shown in the Colums are without decimal places, so 11 is shown instead of 11,24 and are rounded (up or down, depending of the decimal places)

How can i work this out ?
i played around with the parameters of Graph1, but no satisfaction ;-)

Doei
Franz
Trixi-N  Saturday, October 17, 2009 10:04 PM
Hey Dave :)
I don't know much about Globalization.NumberFormatInfo. I tried changing it to "de-DE" so I could try the textfile with values like 11,24 - using the comma for the decimal point. But I couldn't get it to work... What am I doing wrong?
You're just creating a NumberFormatInfo but not doing anything with it. You can change the culture for the thread like this (in the form load):

Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")

Dave299  Saturday, October 17, 2009 10:08 PM
@Dave

thanks - what I showed was what I have seen in a lot of the MSDN Help pages. Like I said, not real familiar with CultureInfo stuff. I thought that would work.



Hi Franz

I just realized my error

Remember. in my original code I thought the csv file was Integers separated by commas. So I never updated the code when reading the lines.

So change it to this:

While Not sr.EndOfStream
    lstY.Add(CDbl(sr.ReadLine))                    
End While
notice the CDbl instead of CInt. Sometimes it is the simplest things.

then it should work fine :)

  • Marked As Answer byTrixi-N Monday, October 19, 2009 6:42 PM
  •  
jwavila  Sunday, October 18, 2009 6:49 PM
Hoi,

Dank u wel - this did it like i want :-)

Doei
Franz
Trixi-N  Monday, October 19, 2009 6:43 PM
"Datas" is a word that occurs with a great rarity in English.
Renee
Renee Culver  20 hours 10 minutes ago
Es freut mich dass DU Deine Sprache gut kannst - aber anstatt zu meckern wäre ein Vorschlag des richtigen Wortes besser gewesen
Trixi-N  13 hours 24 minutes ago
This, by rule, is an English speaking forum so "Es freut mich dass DU Deine Sprache gut kannst - aber anstatt zu meckern wäre ein Vorschlag des richtigen Wortes besser gewesen" is not legal. Too bad because I'm sure that what you said in German, would have been enlightening if it had been legal.

Renee
Renee Culver  10 hours 30 minutes ago
That meant:
I am glad that YOUknow your language well - but would rather be better to give me a better proposal by the right word ;-)

Doei
Franz
Trixi-N  8 hours 21 minutes ago
Oh, Ok "data" is almost always the correct word.
Renee
Renee Culver  7 hours 23 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• what is the different of?
• VB app to do MKDIR create
• Reading and Writing to the Registry!!!
• Navigating thru Datagridview!!!
• How can I log data
• why have this error during installation?
• Adding data to a datagridview
• Invalid Registration Key
• from VB to VB Express 2008
• How to compare text in ComboBox to the text ComboBox list