Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read a text file and plot the points on a chart. The file has randomly arranged numbers in columns. Initially, the program is getting the minimum and maximum numbers from the columns as per my requirement. After that, I plot them on to a chart, but the X and Y axis are not sorted. The axis are showing the random numbers rather than sorted values (min to max).
So far what I tried is copied below.
Please guide me how to change the color and size of the points in the chart to differentiate as I will have to load multiple files later.

THANKS in advance !!!

What I have tried:

VB
Dim Textfile As String
        Dim openDlg As New OpenFileDialog

        If openDlg.ShowDialog() = DialogResult.OK Then
            openDlg.Filter = "txt files (*.txt)| *.txt|All files (*.*)|*.*"
            openDlg.FilterIndex = 2
            openDlg.RestoreDirectory = True
            Textfile = openDlg.FileName

            Dim filelines() As String = File.ReadAllLines(Textfile)

            Dim col1 As List(Of Long) = New List(Of Long)()
            Dim col2 As List(Of Long) = New List(Of Long)()
            Dim col3 As List(Of Double) = New List(Of Double)()
            Dim col4 As List(Of Double) = New List(Of Double)()

            Chart1.Series.Clear()
            Dim r As New Series
            r.Name = "Receivers"
            r.ChartType = SeriesChartType.Point
            Chart1.Series.Add(r)

            For Each fileline As String In filelines
                If fileline.StartsWith("R") Then fileline = fileline.Substring(1)
                Dim items() As String = fileline.Split(New Char() {CChar(vbTab), " "}, StringSplitOptions.RemoveEmptyEntries)

                Dim Linenum As Long
                Dim statnum As Double
                Dim Easting As Double
                Dim Northing As Double

                If Integer.TryParse(items(0), Linenum) Then col1.Add(Linenum)
                If Integer.TryParse(items(1), statnum) Then col2.Add(statnum)
                If Double.TryParse(items(2), Easting) Then col3.Add(Easting)
                If Double.TryParse(items(3), Northing) Then col4.Add(Northing)

                r.Points.AddXY(items(2), items(3))
                
               
            Next

            'sorting the columns to get min and max
            col1.Sort()
            col2.Sort()
            col3.Sort()
            col4.Sort()
            
            'getting maximum and minimum value of 4 columns
            Me.Linemax.Text = col1.Item(col1.Count - 1).ToString()
            Me.Linemin.Text = col1.Item(0).ToString()
            Me.statemax.Text = col2.Item(col2.Count - 1).ToString()
            Me.statmin.Text = col2.Item(0).ToString()
            Me.Eastmax.Text = col3.Item(col3.Count - 1).ToString()
            Me.Eastmin.Text = col3.Item(0).ToString()
            Me.Northmax.Text = col4.Item(col4.Count - 1).ToString()
            Me.Northmin.Text = col4.Item(0).ToString()


            'restricted Y axis as per the maximum and minimum values in the column
            Chart1.ChartAreas(0).AxisY.Minimum = col4.Item(0).ToString()
            Chart1.ChartAreas(0).AxisY.Maximum = col4.Item(col4.Count - 1).ToString()
            

        End If
    End Sub
Posted
Updated 8-Jan-18 9:22am
v2
Comments
Ralf Meier 8-Jan-18 3:32am    
Post some of the real values from one of your files ... In the Moment I have no idea where your problem is ...
VB_Learner 8-Jan-18 5:03am    
Hi Ralf,

The data example is somewhat like this ..

194787.5 2667200.0
194812.5 2667200.0
195662.5 2667200.0
195687.5 2667200.0
201937.5 2667350.0
195737.5 2667200.0
195762.5 2667200.0
195787.5 2667200.0
196512.5 2667200.0
201837.5 2667350.0
198562.5 2667650.0
201887.5 2667350.0
201912.5 2667350.0
198187.5 2667500.0
201962.5 2667350.0
201987.5 2667350.0
202012.5 2667350.0
198412.5 2667650.0

Ralf Meier 8-Jan-18 7:11am    
Thanks ... now I have other questions ...
Your example show a List with X- and Y-Values - this will be two 1D-Array's or one 2D-Array.
So you would have all X-Values in col1 and all Y-Values in col2 ...?
In your code-snippet you have 4 Collections - what does that mean ? Different Curves ?
I guess that Xmin will be col1(0) and Xmax will be col1(col1.Count - 1) - the same to Y : Ymin will be col2(0) and Ymax will be col2(col2.Count - 1) ... but you assign col4 - what does that mean ? Please explain ...
VB_Learner 8-Jan-18 7:14am    
Yes there are 4 columns.. .I only sent the example of 2 columns which I am plotting.. the first 2 columns are just integers which I don't want to plot yet. I only want these two columns to plot as points ... I am only facing problem in sorting the axis X - Y of chart to start from Min to Max .. i hope you got my point..
Ralf Meier 8-Jan-18 9:44am    
OK ... but that is what you allready do - so which part doesn't work until now ?

1 solution

OK ... I have made some test with your data and your code.
At least some changes where necessary. I think with my method-sample it would be easy for you to modify your code that it works.

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim col3 As New List(Of Double)
        Dim col4 As New List(Of Double)
        Dim Easting As Double
        Dim Northing As Double

        Dim s(17) As String
        Dim item As String()
        s(0) = "194787.5 2667200.0"
        s(1) = "194812.5 2667200.0"
        s(2) = "195662.5 2667200.0"
        s(3) = "195687.5 2667200.0"
        s(4) = "201937.5 2667350.0"
        s(5) = "195737.5 2667200.0"
        s(6) = "195762.5 2667200.0"
        s(7) = "195787.5 2667200.0"
        s(8) = "196512.5 2667200.0"
        s(9) = "201837.5 2667350.0"
        s(10) = "198562.5 2667650.0"
        s(11) = "201887.5 2667350.0"
        s(12) = "201912.5 2667350.0"
        s(13) = "198187.5 2667500.0"
        s(14) = "201962.5 2667350.0"
        s(15) = "201987.5 2667350.0"
        s(16) = "202012.5 2667350.0"
        s(17) = "198412.5 2667650.0"

        For i As Integer = 0 To 17
            item = Split(s(i), " ")

            Easting = CDbl(item(0))
            Northing = CDbl(item(1))

            col3.Add(Easting)
            col4.Add(Northing)

            Chart1.Series(0).Points.AddXY(Easting, Northing)
        Next

        col3.Sort()
        col4.Sort()

        Chart1.ChartAreas(0).AxisX.Minimum = col3.Item(0)
        Chart1.ChartAreas(0).AxisX.Maximum = col3.Item(col3.Count - 1)
        Chart1.ChartAreas(0).AxisY.Minimum = col4.Item(0)
        Chart1.ChartAreas(0).AxisY.Maximum = col4.Item(col4.Count - 1)

    End Sub


In my opinion your basic mistake was that you all the time added strings as Chart-Limits and also as Chart-Points.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900