none
How to sort the (min and max) X and Y axis of the chart in VB .NET 2010 RRS feed

  • Question

  • I am reading a text file and plot the points from text file data 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 random number intervals rather than sorted values (i.e. min to max).
    So far what I tried is copied below.
    Please guide me how to change the color and size of the points as well in the chart to differentiate as I will have to load multiple files later.

    THANKS in advance !!!

    ***********************************************************

    Data example is somewhat like this

    R2072                2193                  194787.5 2667200.0            
    R2072                2194                  194812.5 2667200.0             
    R2072                2195                  195662.5 2667200.0             
    R2072                2196                  195687.5 2667200.0             
    R2072                2197                  201937.5 2667350.0             
    R2072                2198                  195737.5 2667200.0             
    R2072                2199                  195762.5 2667200.0             
    R2072                2200                  195787.5 2667200.0             
    R2072                2201                  196512.5 2667200.0             
    R2072                2202                  201837.5 2667350.0             
    R2072                2203                  198562.5 2667650.0             
    R2072                2204                  201887.5 2667350.0             
    R2072                2205                  201912.5 2667350.0             
    R2072                2206                  198187.5 2667500.0             
    R2072                2207                  201962.5 2667350.0             
    R2072                2208                  201987.5 2667350.0             
    R2072                2209                  202012.5 2667350.0   

    *************************************************************

    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

    Tuesday, January 9, 2018 6:20 AM

All replies

  • After that, I plot them on to a chart, but the X and Y axis are not sorted. The axis are showing random number intervals rather than sorted values (i.e. min to max).

    It's a point chart. so 'sorted' has no meaning.  The points will be positioned on the chart according to both the X and the Y values.  The axis intervals are defined by the data.    You should describe exactly what you mean by 'sorted'.

    Tuesday, January 9, 2018 6:53 AM
  • I want the point graph sorted as normal graphs.. e.g starting from 0 and so one 1-2-3-4-5-6-7 etc in ascending order. But the situation im facing is that my data is already sorted but in the graph the X axis is random like 0-2-4-3-1-6-8- etc.  please have a look on the image below and X axis highlighted in black rectangle ...
    • Edited by VB.net_Fan Tuesday, January 9, 2018 7:38 AM
    Tuesday, January 9, 2018 7:12 AM
  • But the situation im facing is that my data is already sorted but in the graph the X axis is random like 0-2-4-3-1-6-8- etc.  please have a look on the image below and X axis highlighted in black rectangle ...

    The area you have highlighted is not  "0-2-4-3-1-6-8- etc." so it's still not clear what you are trying to achieve.  What should those highlighted X-axis values be?

    Your data is sorted by the col2 values, not by the values you are plotting on the X axis.  But I suspect 'sorted' is not what you mean.

    Tuesday, January 9, 2018 7:48 AM
  • Hi VB.net_fan,

    You can use Dictionary(of Integer, List(of Double)) to store and sort data, I modify you code in some place, please take a look.

     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)()
                Dim dic As New Dictionary(Of Integer, 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)
                    End If
                    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
                    Dim list As New List(Of 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)
                    list.Add(items(2))
                    list.Add(items(3))
                    dic.Add(items(1), list)
                    'r.Points.AddXY(items(2), items(3))
    
    
                Next
    
                'sorting the columns to get min and max
                col1.Sort()
                col2.Sort()
                col3.Sort()
                col4.Sort()
    
                Dim sorted = From pair In dic
                             Order By pair.Value(0)
                Dim sortedDictionary = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value)
    
                For Each pair In sortedDictionary
                    ' Display Key and Value.
                    'Console.WriteLine("{0}, {1}", pair.Value(0), pair.Value(1))
                    r.Points.AddXY(pair.Value(0), pair.Value(1))
                Next
    
    
    
                '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

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, January 10, 2018 9:49 AM
    Wednesday, January 10, 2018 3:54 AM
    Moderator
  • You can use Dictionary(of Integer, List(of Double)) to store and sort data, I modify you code in some place, please take a look.

    If you are going to shuffle the data like that for a point chart, then the x axis should be indexed.

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

    Wednesday, January 10, 2018 5:45 AM