Imports System.Windows.Forms.DataVisualization.Charting
Public Class frmChart2
Dim WithEvents chart1 As New Chart With _
{.Parent = Me, .Top = 50}
Dim WithEvents nud As New NumericUpDown With _
{.Parent = Me}
Private Sub frmChart2_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Dim x() As Double = {5, 6, 7, 8, 9, 10, 6, 6}
Dim y() As Double = {100, 110, 120, 130, 140, 150, 80, 120}
Dim y2() As Double = {150, 90, 100, 160, 100, 90, 90, 130}
Dim x2() As Double = {5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 6.5, 6.5}
Dim nud_precision As Integer = 200 'Steps im Achsenbereich
With chart1
.Series.Clear()
.Series.Add("Demo1")
.Series.Add("Demo2")
.Series.Add("Demo3")
.ChartAreas.Clear()
.ChartAreas.Add("Area1")
With chart1.ChartAreas(0).AxisY
'Achsenbereich ....
.Minimum = 70
.Maximum = 180
.Interval = 20
'ins Numeric-Control übertragen
nud.Minimum = CDec(.Minimum)
nud.Maximum = CDec(.Maximum)
Dim pr As Double = (.Maximum - .Minimum) / nud_precision
nud.Increment = CDec(pr)
nud.DecimalPlaces = 0
If pr < 10 Then nud.DecimalPlaces = 1
If pr < 1 Then nud.DecimalPlaces = 2
If pr < 0.1 Then nud.DecimalPlaces = 3
End With
With .Series("Demo1")
For i As Integer = 0 To x.Length - 1
.Points.AddXY(x(i), y(i))
.Points(i).Color = Color.Green
Next i
.ChartType = SeriesChartType.Point
End With
With .Series("Demo2")
For i As Integer = 0 To x.Length - 1
.Points.AddXY(x2(i), y2(i))
.Points(i).Color = Color.Black
Next i
.ChartType = SeriesChartType.Point
End With
End With
End Sub
Dim MouseDataPoint As DataPoint
Dim SetDataPoint As DataPoint
Private Sub Chart1_MouseDown(sender As Object, _
e As MouseEventArgs) Handles chart1.MouseDown
Dim htr As HitTestResult = chart1.HitTest(e.X, e.Y)
MouseDataPoint = Nothing
SetDataPoint = Nothing
If htr.ChartElementType = ChartElementType.DataPoint Then
MouseDataPoint = _
chart1.Series(htr.Series.Name).Points(htr.PointIndex)
With MouseDataPoint
.MarkerStyle = MarkerStyle.Cross
.MarkerColor = Color.Red
.MarkerSize = 12
.Tag = htr.PointIndex
End With
SetDataPoint = MouseDataPoint
End If
End Sub
Private Sub chart1_MouseMove(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles chart1.MouseMove
If e.Y < 0 Or e.Y >= chart1.Height Then Exit Sub
If MouseDataPoint Is Nothing Then Exit Sub
With chart1.ChartAreas(0)
Dim yval As Double = .AxisY.PixelPositionToValue(e.Y)
If yval >= .AxisY.Minimum And yval <= .AxisY.Maximum Then
MouseDataPoint.YValues(0) = yval
nud.Value = CDec(SetDataPoint.YValues(0))
End If
End With
End Sub
Private Sub chart1_MouseUp(sender As Object,
e As System.Windows.Forms.MouseEventArgs) Handles chart1.MouseUp
If MouseDataPoint Is Nothing Then Exit Sub
With MouseDataPoint
.MarkerStyle = MarkerStyle.None
.MarkerSize = 5
.MarkerColor = MouseDataPoint.Color
End With
chart1.Invalidate()
nud.Value = CDec(SetDataPoint.YValues(0))
MouseDataPoint = Nothing
End Sub
Private Sub nud_ValueChanged(sender As Object, _
e As System.EventArgs) Handles nud.ValueChanged
If SetDataPoint Is Nothing Then Exit Sub
SetDataPoint.YValues(0) = nud.Value
chart1.Invalidate()
End Sub
End Class
Beitrag wurde zuletzt am 18.10.14 um 07:47:20 editiert. |