如何在VB .NET 2010中对图表的(最小和最大)X轴和Y轴进行排序 [英] How to sort the (min and max) X and Y axis of the chart in VB .NET 2010
问题描述
我读了一个文本文件并在图表上绘制了点。该文件在列中随机排列数字。最初,程序根据我的要求从列中获取最小和最大数字。之后,我将它们绘制到图表上,但X轴和Y轴没有排序。轴显示随机数而不是排序值(从最小到最大)。
到目前为止,我尝试的内容复制如下。
请指导我如何更改图表中点的颜色和大小以区分,因为我将在以后加载多个文件。
感谢提前!!!
我尝试过:
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:
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
推荐答案
好的......我已经对你的数据和代码进行了一些测试。
至少在必要时进行一些更改。我认为使用我的方法示例,您可以很容易地修改它的代码。
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.
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.
这篇关于如何在VB .NET 2010中对图表的(最小和最大)X轴和Y轴进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!