1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Private pointsA As New List(Of PointF)
Private pointsB As New List(Of PointF)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetData()
Timer_recup_graph.Start()
End Sub
Private Sub GetData()
Dim rnd As New Random
'Canal A
pointsA.Add(New PointF(ParaH1_CA.Text, txt_valeur1CA.Text))
pointsA.Add(New PointF(ParaH2_CA.Text, txt_valeur2CA.Text))
pointsA.Add(New PointF(ParaH3_CA.Text, txt_valeur3CA.Text))
pointsA.Add(New PointF(ParaH4_CA.Text, txt_valeur4CA.Text))
pointsA.Add(New PointF(ParaH5_CA.Text, txt_valeur5CA.Text))
pointsA.Add(New PointF(ParaH6_CA.Text, txt_valeur6CA.Text))
pointsA.Add(New PointF(ParaH7_CA.Text, txt_valeur7CA.Text))
pointsA.Add(New PointF(ParaH8_CA.Text, txt_valeur8CA.Text))
pointsA.Add(New PointF(ParaH9_CA.Text, txt_valeur9CA.Text))
pointsA.Add(New PointF(ParaH10_CA.Text, txt_valeur10CA.Text))
BindingSourceA.DataSource = pointsA
Chart1.Series(0).ChartType = SeriesChartType.Line
Chart1.Series(0).XValueMember = "X"
Chart1.Series(0).YValueMembers = "Y"
Chart1.Series(0).BorderWidth = 2
Chart1.Series(0).MarkerColor = Color.Blue
Chart1.Series(0).MarkerStyle = MarkerStyle.Circle
Chart1.Series(0).MarkerSize = 18
With Chart1.Series(0)
.Name = "Canal A"
End With
'Move legend to bottom and center
Chart1.Legends(0).Docking = Docking.Bottom
Chart1.Legends(0).Alignment = StringAlignment.Center
' les Bindings
Chart1.Series(0).Points.DataBind(BindingSourceA, "X", "Y", Nothing)
Chart1.DataBind()
ListBoxA.DataSource = BindingSourceA
txtXA.DataBindings.Add("Text", BindingSourceA, "X", True, DataSourceUpdateMode.OnPropertyChanged)
txtYA.DataBindings.Add("Text", BindingSourceA, "Y", True, DataSourceUpdateMode.OnPropertyChanged)
'---------------------------------------------------------------------------------------------------
'Canal B
pointsB.Add(New PointF(ParaH1_CB.Text, txt_valeur1CB.Text))
pointsB.Add(New PointF(ParaH2_CB.Text, txt_valeur2CB.Text))
pointsB.Add(New PointF(ParaH3_CB.Text, txt_valeur3CB.Text))
pointsB.Add(New PointF(ParaH4_CB.Text, txt_valeur4CB.Text))
pointsB.Add(New PointF(ParaH5_CB.Text, txt_valeur5CB.Text))
pointsB.Add(New PointF(ParaH6_CB.Text, txt_valeur6CB.Text))
pointsB.Add(New PointF(ParaH7_CB.Text, txt_valeur7CB.Text))
pointsB.Add(New PointF(ParaH8_CB.Text, txt_valeur8CB.Text))
pointsB.Add(New PointF(ParaH9_CB.Text, txt_valeur9CB.Text))
pointsB.Add(New PointF(ParaH10_CB.Text, txt_valeur10CB.Text))
BindingSourceB.DataSource = pointsB
Chart1.Series(1).ChartType = SeriesChartType.Line
Chart1.Series(1).XValueMember = "X"
Chart1.Series(1).YValueMembers = "Y"
Chart1.Series(1).BorderWidth = 2
Chart1.Series(1).MarkerColor = Color.Blue
Chart1.Series(1).MarkerStyle = MarkerStyle.Circle
Chart1.Series(1).MarkerSize = 18
With Chart1.Series(1)
.Name = "Canal B"
End With
' les Bindings
Chart1.Series(1).Points.DataBind(BindingSourceB, "X", "Y", Nothing)
Chart1.DataBind()
ListBoxB.DataSource = BindingSourceB
txtXB.DataBindings.Add("Text", BindingSourceB, "X", True, DataSourceUpdateMode.OnPropertyChanged)
txtYB.DataBindings.Add("Text", BindingSourceB, "Y", True, DataSourceUpdateMode.OnPropertyChanged)
End Sub
'-----------------RAJOUT-----------------
Private selectedPointFA As PointF = Nothing
Private positionA As Integer = 0
Private selectedPointFB As PointF = Nothing
Private positionB As Integer = 0
'---------------------------------
Private selectedDataPointA As New DataPoint
Private selectedDataPointB As New DataPoint
Private Sub Chart1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseDown
selectedDataPointA = Nothing
positionA = -1
If e.Button = MouseButtons.Left Then
Dim hitResultA As HitTestResult = Chart1.HitTest(e.X, e.Y)
If hitResultA Is Nothing Then Return
If hitResultA.ChartElementType = ChartElementType.DataPoint Then
selectedDataPointA = CType(hitResultA.Object, DataPoint)
' Show point value as label
selectedDataPointA.IsValueShownAsLabel = True
'-----------------MAJ POINTS-----------------
positionA = hitResultA.PointIndex
selectedPointFA = New PointF(selectedDataPointA.XValue, selectedDataPointA.YValues(0))
End If
End If
selectedDataPointB = Nothing
positionB = -1
If e.Button = MouseButtons.Left Then
Dim hitResultB As HitTestResult = Chart1.HitTest(e.X, e.Y)
If hitResultB Is Nothing Then Return
If hitResultB.ChartElementType = ChartElementType.DataPoint Then
selectedDataPointB = CType(hitResultB.Object, DataPoint)
' Show point value as label
selectedDataPointB.IsValueShownAsLabel = True
'-----------------MAJ POINTS-----------------
positionB = hitResultB.PointIndex
selectedPointFB = New PointF(selectedDataPointB.XValue, selectedDataPointB.YValues(1))
End If
End If
End Sub
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
If selectedDataPointA Is Nothing Then Return
If e.Button = MouseButtons.Left Then
' X value from current cursor position
Dim xValueA As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.X)
xValueA = Math.Round(Math.Max(xValueA, Chart1.ChartAreas(0).AxisX.Minimum), 0)
xValueA = Math.Round(Math.Min(xValueA, Chart1.ChartAreas(0).AxisX.Maximum), 0)
' Y value from current cursor position
Dim yValueA As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Y)
yValueA = Math.Round(Math.Max(yValueA, Chart1.ChartAreas(0).AxisY.Minimum), 0)
yValueA = Math.Round(Math.Min(yValueA, Chart1.ChartAreas(0).AxisY.Maximum), 0)
' Update selected point X,Y value
selectedDataPointA.XValue = xValueA
'selectedDataPoint.XValue = -selectedDataPoint.XValue
selectedDataPointA.YValues(0) = yValueA
selectedPointFA = New PointF(selectedDataPointA.XValue, selectedDataPointA.YValues(0))
'-----------------MAJ POINTS-----------------
pointsA(positionA) = selectedPointFA
ResumeLiaison()
' Invalidate chart
Chart1.Invalidate()
End If
'-----------------------------------------------------------------------------------------
If selectedDataPointB Is Nothing Then Return
If e.Button = MouseButtons.Left Then
' X value from current cursor position
Dim xValueB As Double = Chart1.ChartAreas(1).AxisX.PixelPositionToValue(e.X)
xValueB = Math.Round(Math.Max(xValueB, Chart1.ChartAreas(1).AxisX.Minimum), 0)
xValueB = Math.Round(Math.Min(xValueB, Chart1.ChartAreas(1).AxisX.Maximum), 0)
' Y value from current cursor position
Dim yValueB As Double = Chart1.ChartAreas(1).AxisY.PixelPositionToValue(e.Y)
yValueB = Math.Round(Math.Max(yValueB, Chart1.ChartAreas(1).AxisY.Minimum), 0)
yValueB = Math.Round(Math.Min(yValueB, Chart1.ChartAreas(1).AxisY.Maximum), 0)
' Update selected point X,Y value
selectedDataPointB.XValue = xValueB
'selectedDataPoint.XValue = -selectedDataPoint.XValue
selectedDataPointB.YValues(1) = yValueB
selectedPointFB = New PointF(selectedDataPointB.XValue, selectedDataPointB.YValues(0))
'-----------------MAJ POINTS-----------------
pointsB(positionB) = selectedPointFB
ResumeLiaison()
' Invalidate chart
Chart1.Invalidate()
End If
End Sub
Private Sub Chart1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseUp
If selectedDataPointA Is Nothing Then Return
If e.Button = MouseButtons.Right Then
' Hide point label
selectedDataPointA.IsValueShownAsLabel = False
selectedPointFA = New PointF(selectedDataPointA.XValue, selectedDataPointA.YValues(0))
'-----------------MAJ POINTS-----------------
pointsA(positionA) = selectedPointFA
ResumeLiaison()
' reset selected object
selectedDataPointA = Nothing
' Invalidate chart
Chart1.Invalidate()
End If
'---------------------------------------------------------------------------------------------------
If selectedDataPointB Is Nothing Then Return
If e.Button = MouseButtons.Right Then
' Hide point label
selectedDataPointB.IsValueShownAsLabel = False
selectedPointFB = New PointF(selectedDataPointB.XValue, selectedDataPointB.YValues(0))
'-----------------MAJ POINTS-----------------
pointsB(positionB) = selectedPointFB
ResumeLiaison()
' reset selected object
selectedDataPointB = Nothing
' Invalidate chart
Chart1.Invalidate()
End If
End Sub
Private Sub ResumeLiaison()
BindingSourceA.ResetBindings(True)
BindingSourceA.Position = positionA
BindingSourceB.ResetBindings(True)
BindingSourceB.Position = positionB
End Sub
Private Sub BindingSource1_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingSourceA.PositionChanged
txtIndexA.Text = BindingSourceA.Position.ToString
End Sub
Private Sub BindingSourceB_PositionChanged(sender As Object, e As EventArgs) Handles BindingSourceB.PositionChanged
txtIndexB.Text = BindingSourceB.Position.ToString
End Sub
End Class |
Partager