Hier eine überarbeitete Fassung mit ChartDemo:
Public Function GetData(ByVal filename As String) As DataTable
Dim dt As New DataTable
With dt.Columns
.Add("ID", GetType(Integer))
.Add("SP1", GetType(Integer))
.Add("SP2", GetType(Integer))
.Add("Rel", GetType(Decimal), "SP2 / SP1")
End With
Dim lines() As String = IO.File.ReadAllLines(filename)
Dim startline% = -1, endline% = -1
For i As Integer = 0 To lines.Count - 1
If lines(i).ToUpper.Trim.StartsWith("[GEAR_RATIOS]") Then
startline = i + 1
End If
If startline >= 0 Then
If lines(i).ToUpper.Trim.StartsWith("[") Then
endline = i - 1
If endline > startline + 3 Then Exit For
End If
End If
Next i
If startline = -1 Or endline < startline Then Return Nothing
Dim s, e As Integer
Dim n1, n2, z As Integer
For i As Integer = startline To endline
If lines(i).ToLower.StartsWith("ratio") Then
s = lines(i).IndexOf("(")
e = lines(i).IndexOf(")")
If s >= 0 And e >= s + 1 Then
Dim numbers As String = lines(i).Substring(s + 1, e - s - 1)
Dim nums() As String = numbers.Split(","c)
If nums.Length = 2 Then
If Integer.TryParse(nums(0), n1) AndAlso _
Integer.TryParse(nums(1), n2) Then
z += 1
dt.Rows.Add(z, n1, n2)
Console.WriteLine(n1.ToString & " " & n2.ToString)
End If
End If
End If
End If
Next i
Return dt
End Function
Public Sub Diagramm(ByVal cht As Chart, ByVal dt As DataTable)
With cht
.DataSource = dt
.Series.Clear()
.Series.Add("Series1")
.ChartAreas.Clear()
.ChartAreas.Add("Area1")
.Legends.Clear()
.Legends.Add("Series1Legend")
.Titles.Clear()
.Titles.Add("Title1")
.Titles("Title1").Text = "Einen Titel braucht das Chart"
With .Series("Series1")
.IsVisibleInLegend = True
.Legend = "Series1Legend"
.LegendText = dt.Columns(2).ColumnName & _
" / " & dt.Columns(1).ColumnName
.ChartArea = "Area1"
.XValueMember = dt.Columns(0).ColumnName
.YValueMembers = dt.Columns(3).ColumnName
.ChartType = SeriesChartType.Line
End With
End With
End Sub Anwendung:
Sei Chart1 ein Chart-Control (NS: DataVisualization.Charting)
Dim dt as Datatable = GetData("Dateiname")
if not dt is nothing then
Diagramm(chart1, dt)
end if
Beitrag wurde zuletzt am 09.10.14 um 16:31:34 editiert. |