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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
| Public Class GraphCourbes
Inherits System.Windows.Forms.Control
Public Enum Position
gauche = 0
droite = 1
End Enum
Private Class EchelleDeTemps
Public DateDeb As Date
Public DateFin As Date
Public CtrlWidth As Integer
Public Shared Hauteur As Integer = 30
Public ReadOnly Property ValeursIntervals() As System.Collections.Generic.List(Of Date)
Get
Dim minutes As Long
Minutes = DateDiff(DateInterval.Minute, Me.DateDeb, Me.DateFin)
Select Case Minutes
Case Is < 35
Minutes = 5
Case Is < 70
Minutes = 10
Case Is < 140
Minutes = 20
Case Is < 7 * 60
Minutes = 60
Case Is < 15 * 60
Minutes = 2 * 60
Case Is < 24 * 60
Minutes = 4 * 60
Case Is < 2 * 24 * 60
Minutes = 6 * 60
Case Is < 4 * 24 * 60
Minutes = 12 * 60
Case Is < 8 * 24 * 60
Minutes = 24 * 60
Case Is < 15 * 24 * 60
Minutes = 2 * 24 * 60
Case Is < 23 * 24 * 60
Minutes = 4 * 24 * 60
Case Is < 40 * 24 * 60
Minutes = 7 * 24 * 60
Case Else
Minutes = Minutes \ 60 \ 7
End Select
ValeursIntervals = New System.Collections.Generic.List(Of Date)
Dim i As Date = Me.DateDeb
i = Me.DateDeb.AddSeconds(-Me.DateDeb.Second)
If minutes < 60 Then
i = i.AddMinutes(-i.Minute Mod 10)
If i.Minute Mod 10 = 0 Then
End If
'i = DateAdd(DateInterval.Minute,datediff(DateInterval.Minute, Me.DateDeb.Minute
Else
End If
While i <= Me.DateFin
i = i.AddMinutes(minutes)
ValeursIntervals.Add(i)
End While
Return ValeursIntervals
End Get
End Property
Public Sub AjouteTailleEchelle(ByVal Width As Integer, ByVal x2 As Boolean)
Me.DateDeb = Me.DateDeb.AddMinutes(-Me.CtrlWidth * Width / DateDiff(DateInterval.Minute, Me.DateDeb, Me.DateFin))
If x2 Then Me.DateFin = Me.DateFin.AddMinutes(-Me.CtrlWidth * Width / DateDiff(DateInterval.Minute, Me.DateDeb, Me.DateFin))
End Sub
End Class
Private Class Echelle
Public Position As Position
Public ValeurMin As Decimal
Public ValeurMax As Decimal
Public CtrlHeight As Integer
Public Unite As String
Public Shared Largeur As Integer = 40
Public ReadOnly Property ValeursIntervals() As System.Collections.Generic.List(Of Decimal)
Get
Dim interval As Double
interval = (Me.ValeurMax - Me.ValeurMin) / 8
Select Case interval
Case Is < 0.3
interval = 0.1
Case Is < 0.4
interval = 0.25
Case Is < 0.7
interval = 0.5
Case Is < 0.8
interval = 0.75
Case Is < 1.3
interval = 1
Case Is < 3
interval = 2
Case Is < 8
interval = 5
Case Is < 15
interval = 10
Case Else
interval = (CLng(interval) \ 10) * 10
End Select
ValeursIntervals = New System.Collections.Generic.List(Of Decimal)
Dim i As Decimal = System.Math.Round(Me.ValeurMin, 0)
While i <= Me.ValeurMax
i += CDec(interval)
ValeursIntervals.Add(i)
End While
Return ValeursIntervals
End Get
End Property
Public Sub AjouteTailleEchelleDeTemps(ByVal height As Integer)
Me.ValeurMin -= System.Math.Round((height * (Me.ValeurMax - Me.ValeurMin)) / Me.CtrlHeight, 2)
End Sub
End Class
Public Sub New()
Me.BackColor = Color.White
_CentrageTexte.Alignment = StringAlignment.Center
_CentrageTexte.LineAlignment = StringAlignment.Center
End Sub
Private _CentrageTexte As New System.Drawing.StringFormat
Private _buffer As System.Drawing.BufferedGraphics
Private _courbes As System.Collections.Generic.List(Of Courbe)
Private _ddeb As Date
Private _dfin As Date
Private _Echelles As New System.Collections.Generic.Dictionary(Of String, Echelle)
Private _EchelleTemps As EchelleDeTemps
Public Sub TraceCourbes(ByVal courbes As System.Collections.Generic.List(Of Courbe), ByVal ddeb As Date, ByVal dfin As Date)
Try
_courbes = Nothing
_ddeb = Nothing
_dfin = Nothing
_Echelles.Clear()
_buffer.Graphics.Clear(Me.BackColor)
_buffer.Render()
If courbes Is Nothing Then Exit Sub
If courbes.Count = 0 Then Exit Sub
If ddeb = Nothing Then Exit Sub
If dfin = Nothing Then Exit Sub
_courbes = courbes
_ddeb = ddeb
_dfin = dfin
_EchelleTemps = New EchelleDeTemps()
_EchelleTemps.DateDeb = ddeb
_EchelleTemps.DateFin = dfin
_EchelleTemps.CtrlWidth = Me.Width
For Each crb As Courbe In courbes
If _Echelles.ContainsKey(crb.Capteur.Unite) Then
If crb.minvalue < _Echelles(crb.Capteur.Unite).ValeurMin Then _Echelles(crb.Capteur.Unite).ValeurMin = crb.minvalue
If crb.minvalue < _Echelles(crb.Capteur.Unite).ValeurMin Then _Echelles(crb.Capteur.Unite).ValeurMin = crb.minvalue
Continue For
End If
If _Echelles.Count >= 4 Then
'MSGS.AfficheMessage("Il ne peut y avoir que 4 types d'échelles en même temps sur un graphique" & vbCrLf & "La courbe " & crb.Capteur.Nom_Capteur & " ne sera pas tracée", "Trop de courbes différentes")
' todo : arreter tout et décocher la courbe en question (donc rappel de cette sub)
End If
Dim ech As New Echelle
ech.Position = CType(_Echelles.Count Mod 2, Position)
ech.CtrlHeight = Me.Height
ech.Unite = crb.Capteur.Unite
ech.ValeurMin = crb.minvalue
ech.ValeurMax = crb.maxvalue
_Echelles.Add(crb.Capteur.Unite, ech)
Next
For Each ech As Echelle In _Echelles.Values
If ech.ValeurMax = ech.ValeurMin Then Continue For
ech.AjouteTailleEchelleDeTemps(EchelleDeTemps.Hauteur)
Next
_EchelleTemps.AjouteTailleEchelle(Echelle.Largeur, (_Echelles.Count > 1))
RefreshGraph()
Catch ex As Exception
SaveErreur(ex, Me)
End Try
End Sub
Public Sub Print(ByVal g As System.Drawing.Graphics)
Try
Catch ex As Exception
SaveErreur(ex, Me)
End Try
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(pevent)
AppliqueBuffer()
End Sub
Private Sub AppliqueBuffer()
If _buffer IsNot Nothing Then _buffer.Render()
If ptproche <> Nothing Then Me.CreateGraphics.DrawEllipse(New System.Drawing.Pen(_couleurptproche), ptproche.X - 3, ptproche.Y - 3, 6, 6)
If ptproche <> Nothing Then Me.CreateGraphics.DrawEllipse(New System.Drawing.Pen(_couleurptproche), ptproche.X - 2, ptproche.Y - 2, 4, 4)
End Sub
Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
MyBase.OnSizeChanged(e)
If _buffer IsNot Nothing Then _buffer.Dispose()
_buffer = System.Drawing.BufferedGraphicsManager.Current.Allocate(Me.CreateGraphics, New System.Drawing.Rectangle(0, 0, Me.Width, Me.Height))
_buffer.Graphics.Clear(Me.BackColor)
If _courbes IsNot Nothing Then TraceCourbes(_courbes, _ddeb, _dfin)
End Sub
Public Sub RefreshGraph()
_buffer.Graphics.Clear(Me.BackColor)
_buffer.Render()
If _courbes.Count = 0 Then Exit Sub
TraceAxes()
For Each ech As Echelle In _Echelles.Values
TraceEchelle(ech)
Next
TraceEchelleTemps()
For Each crb As Courbe In _courbes
TraceCourbe(crb)
Next
_buffer.Render()
End Sub
Private Sub TraceAxes()
_buffer.Graphics.DrawLine(Pens.Black, Echelle.Largeur, Me.Height - EchelleDeTemps.Hauteur, Echelle.Largeur, 0) ' pour verticale echelle de temps de gauche
If _Echelles.Count = 1 Then
_buffer.Graphics.DrawLine(Pens.Black, Echelle.Largeur, Me.Height - EchelleDeTemps.Hauteur, Me.Width, Me.Height - EchelleDeTemps.Hauteur) ' pour horizontale au dessus de echelle de temps
Else
_buffer.Graphics.DrawLine(Pens.Black, Echelle.Largeur, Me.Height - EchelleDeTemps.Hauteur, Me.Width - Echelle.Largeur, Me.Height - EchelleDeTemps.Hauteur) ' pour horizontale au dessus de echelle de temps
_buffer.Graphics.DrawLine(Pens.Black, Me.Width - Echelle.Largeur, Me.Height - EchelleDeTemps.Hauteur, Me.Width - Echelle.Largeur, 0) ' pour verticale echelle de temps de droite
End If
End Sub
Private Sub TraceEchelle(ByVal ech As Echelle)
If ech.ValeurMin = ech.ValeurMax Then Exit Sub
Dim tirets As New System.Drawing.Pen(Color.Black, 1)
tirets.DashStyle = Drawing2D.DashStyle.Dash
tirets.DashPattern = New Single(1) {3, 5}
Dim coul As System.Drawing.Color
Dim droite As Integer = Me.Width
If _Echelles.Count > 1 Then droite = Me.Width - Echelle.Largeur
Dim xtexte As Integer
If ech.Position = Position.gauche Then
coul = Color.Blue
' coul = Color.DarkGray
xtexte = Echelle.Largeur \ 3
Else
coul = Color.Brown
' coul = Color.DarkKhaki
xtexte = Me.Width - Echelle.Largeur \ 3
End If
For Each interval As Decimal In ech.ValeursIntervals
tirets.Color = coul
_buffer.Graphics.DrawLine(tirets, Echelle.Largeur, Courbe.GetCoord(ech.ValeurMin, ech.ValeurMax, interval, Me.Height), droite, Courbe.GetCoord(ech.ValeurMin, ech.ValeurMax, interval, Me.Height))
_buffer.Graphics.DrawString(interval.ToString, Me.Font, New System.Drawing.SolidBrush(coul), xtexte, Courbe.GetCoord(ech.ValeurMin, ech.ValeurMax, interval, Me.Height), _CentrageTexte)
Next
End Sub
Private Sub TraceEchelleTemps()
For Each interval As Date In _EchelleTemps.ValeursIntervals
_buffer.Graphics.DrawLine(Pens.Black, Courbe.GetCoord(_EchelleTemps.DateDeb, _EchelleTemps.DateFin, interval, Me.Width), Me.Height - EchelleDeTemps.Hauteur + 5, Courbe.GetCoord(_EchelleTemps.DateDeb, _EchelleTemps.DateFin, interval, Me.Width), Me.Height - EchelleDeTemps.Hauteur)
_buffer.Graphics.DrawString(interval.ToString, Me.Font, Brushes.Black, Courbe.GetCoord(_EchelleTemps.DateDeb, _EchelleTemps.DateFin, interval, Me.Width), Me.Height - EchelleDeTemps.Hauteur + 16, _CentrageTexte)
Next
End Sub
Private Sub TraceCourbe(ByVal crb As Courbe)
Dim lastdateON As Date = Nothing
Dim lastPoint As System.Drawing.Point
Dim currentPoint As System.Drawing.Point
For Each d As Date In crb.Points.Keys
If crb.Points(d) Is System.DBNull.Value Then ' si pas de point enregistré
If lastdateON = Nothing Then ' si pas de point avant
Continue For
End If
' si un point avant
' pour couleur auto : Courbe.Couleurs(ncourbe)
'_buffer.Graphics.DrawLine(New System.Drawing.Pen(crb.Capteur.Couleur), lastPoint, lastPoint)
If Not lastdateON = Nothing Then
_buffer.Graphics.DrawRectangle(New System.Drawing.Pen(crb.Capteur.Couleur), lastPoint.X - 2, lastPoint.Y - 2, 4, 4)
End If
lastdateON = Nothing
lastPoint = Nothing
Continue For
End If
' si un point existe
currentPoint.X = Courbe.GetCoord(_ddeb, _dfin, d, Me.Width)
currentPoint.Y = Courbe.GetCoord(_Echelles(crb.Capteur.Unite).ValeurMin, _Echelles(crb.Capteur.Unite).ValeurMax, CDec(crb.Points(d)), Me.Height)
If lastdateON <> Nothing Then ' si un point avant
_buffer.Graphics.DrawRectangle(New System.Drawing.Pen(crb.Capteur.Couleur), lastPoint.X - 2, lastPoint.Y - 2, 4, 4)
_buffer.Graphics.DrawLine(New System.Drawing.Pen(crb.Capteur.Couleur), lastPoint, currentPoint)
End If
lastdateON = d
lastPoint = currentPoint
Continue For
Next
End Sub
Private ptproche As System.Drawing.Point
Private _couleurptproche As System.Drawing.Color
Public Event PointProcheChanged(ByVal sender As Object, ByVal ptText As String)
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If _courbes Is Nothing Then Exit Sub
If _courbes.Count = 0 Then Exit Sub
ptproche = Nothing
Dim mindist As Decimal = 1000
Dim crbmindist As Courbe = Nothing
Dim datemindist As Date
Dim pt As System.Drawing.Point = Nothing
For Each crb As Courbe In _courbes
For Each d As Date In crb.Points.Keys
If crb.Points(d) Is System.DBNull.Value Then Continue For
If _Echelles(crb.Capteur.Unite).ValeurMin = _Echelles(crb.Capteur.Unite).ValeurMax Then Continue For
pt.X = Courbe.GetCoord(_ddeb, _dfin, d, Me.Width)
pt.Y = Courbe.GetCoord(_Echelles(crb.Capteur.Unite).ValeurMin, _Echelles(crb.Capteur.Unite).ValeurMax, CDec(crb.Points(d)), Me.Height)
If calcdistance(e.Location, pt) < mindist Then
mindist = calcdistance(e.Location, pt)
crbmindist = crb
datemindist = d
End If
Next
Next
Dim texte As String
If datemindist = Nothing Then
texte = ""
Else
pt.X = Courbe.GetCoord(_ddeb, _dfin, datemindist, Me.Width)
pt.Y = Courbe.GetCoord(_Echelles(crbmindist.Capteur.Unite).ValeurMin, _Echelles(crbmindist.Capteur.Unite).ValeurMax, CDec(crbmindist.Points(datemindist)), Me.Height)
ptproche = pt
_couleurptproche = System.Drawing.Color.FromArgb(255 - crbmindist.Capteur.Couleur.R, 255 - crbmindist.Capteur.Couleur.G, 255 - crbmindist.Capteur.Couleur.B)
AppliqueBuffer()
'Me.CreateGraphics.DrawEllipse(Pens.Brown, pt.X - 2, pt.Y - 2, 4, 4)
texte = datemindist.Date & " " & datemindist.Hour & ":" & Microsoft.VisualBasic.Right("0" & datemindist.Minute, 2) & " " & CDec(crbmindist.Points(datemindist)) & " " & crbmindist.Capteur.Unite
End If
RaiseEvent PointProcheChanged(Me, texte)
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
ptproche = Nothing
AppliqueBuffer()
RaiseEvent PointProcheChanged(Me, "")
End Sub
Private Function calcdistance(ByVal ptsouris As System.Drawing.Point, ByVal pt As System.Drawing.Point) As Decimal
Return CDec(System.Math.Sqrt(System.Math.Pow(ptsouris.X - pt.X, 2) + System.Math.Pow(ptsouris.Y - pt.Y, 2)))
End Function
End Class |
Partager