Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question'Imports System 'Imports System.Collections.Generic Imports System.ComponentModel Imports System.Drawing.Drawing2D 'Imports System.Drawing 'Imports System.Data 'Imports System.Text 'Imports System.Windows.Forms Imports System.Threading Friend Class CtlGraph Public Interface IGrpSerie ReadOnly Property Name() As String ReadOnly Property MaxValue() As Integer ReadOnly Property MinValue() As Integer Property MaxNbPoints() As Integer End Interface ''' <summary> ''' Classe pour une série ''' </summary> ''' <remarks></remarks> Public Class ClsSerie Implements IGrpSerie Private mListPoints As New List(Of Integer) Private mMax As Integer Private mMin As Integer Private mAve As Double Private mTotalPoints As Integer Private mMaxNbPoints As Integer Private mForeColor As Color Private mName As String Public Delegate Sub EvtChange(ByVal pSerie As ClsSerie) Public Event Change As EvtChange #Region "Constructeurs" ''' <summary> ''' Constructeur de la classe ''' </summary> ''' <remarks></remarks> Public Sub New() mForeColor = Color.Blue mName = "" mMaxNbPoints = 100 mTotalPoints = 0 mMax = 0 mMin = 0 mAve = 0 mListPoints.Clear() AddPoint(0) End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Nom de la série ''' <remarks></remarks> Public Sub New(ByVal pName As String) MyClass.New() mName = pName End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Couleur de la série ''' <remarks></remarks> Public Sub New(ByVal pForeColor As Color) MyClass.New() mForeColor = pForeColor End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Nom de la série ''' Couleur de la série ''' <remarks></remarks> Public Sub New(ByVal pName As String, ByVal pForeColor As Color) MyClass.New() mForeColor = pForeColor mName = pName End Sub #End Region #Region "Propriétés" ''' <summary> ''' Retourne ou définit la couleur de la courbe ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public Property ForeColor() As Color Get Return mForeColor End Get Set(ByVal value As Color) mForeColor = value End Set End Property ''' <summary> ''' etourne ou définit le nombre maximum de points mémorisés ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public Property MaxNbPoints() As Integer Implements IGrpSerie.MaxNbPoints Get Return mMaxNbPoints End Get Set(ByVal value As Integer) If value >= 20 Then mMaxNbPoints = value End If End Set End Property ''' <summary> ''' Retourne le nom de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Name() As String Implements IGrpSerie.Name Get Return mName End Get End Property ''' <summary> ''' Retourne la valeur maximale de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property MaxValue() As Integer Implements IGrpSerie.MaxValue Get Return mMax End Get End Property ''' <summary> ''' Retourne la valeur minimale de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property MinValue() As Integer Implements IGrpSerie.MinValue Get Return mMin End Get End Property ''' <summary> ''' Retourne la valeur moyenne de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property MoyValue() As Double Get Return mAve End Get End Property ''' <summary> ''' Retourne la liste des valeurs du tableau ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Values() As Collections.ObjectModel.ReadOnlyCollection(Of Integer) Get Return New Collections.ObjectModel.ReadOnlyCollection(Of Integer)(mListPoints) End Get End Property #End Region #Region "Méthodes" ''' <summary> ''' Ajoute un point dans la liste ''' </summary> ''' Valeur à ajouter ''' <remarks></remarks> Public Sub AddPoint(ByVal pValue As Integer) SyncLock mListPoints mListPoints.Add(pValue) mTotalPoints += pValue If mMax < pValue Then mMax = pValue If mMin > pValue Then mMin = pValue CalcMoyenne() End SyncLock CtrlNbPoints() RaiseEvent Change(Me) End Sub ''' <summary> ''' Controle le nombre de points mémorisés (avec suppression si besoin) ''' </summary> ''' <remarks></remarks> Private Sub CtrlNbPoints() Dim lInt As Integer Do While mListPoints.Count > mMaxNbPoints SyncLock mListPoints lInt = mListPoints(0) mListPoints.RemoveAt(0) mTotalPoints -= lInt End SyncLock If mMax lInt Or mMin lInt Then Dim lLst As New List(Of Integer) lLst.AddRange(mListPoints) lLst.Sort() mMax = lLst(lLst.Count - 1) mMin = lLst(0) End If CalcMoyenne() Loop End Sub ''' <summary> ''' Calcule la moyenne des points ''' </summary> ''' <remarks></remarks> Private Sub CalcMoyenne() mAve = mTotalPoints / mListPoints.Count End Sub #End Region End Class ''' <summary> ''' Classe pour les groupements de séries ''' </summary> ''' <remarks></remarks> Public Class ClsGroupe Implements IGrpSerie Private mSeries As New List(Of IGrpSerie) Private mName As String #Region "Constructeurs" ''' <summary> ''' Constructeur de la classe ''' </summary> ''' <remarks></remarks> Public Sub New() mSeries.Clear() End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Nom de la série ''' <remarks></remarks> Public Sub New(ByVal pName As String) MyClass.New() mName = pName End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Liste des séries ''' <remarks></remarks> Public Sub New(ByVal pSeries As ClsSerie()) MyClass.New() mSeries.AddRange(pSeries) End Sub ''' <summary> ''' Constructeur de la classe ''' </summary> ''' Nom de la série ''' Liste des séries ''' <remarks></remarks> Public Sub New(ByVal pName As String, ByVal pSeries As ClsSerie()) MyClass.New() mSeries.AddRange(pSeries) mName = pName End Sub #End Region #Region "Propriétés" ''' <summary> ''' Retourne la liste des séries ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Series() As List(Of IGrpSerie) Get Return mSeries End Get End Property ''' <summary> ''' Retourne ou définit le nombre maximum de points mémorisés ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public Property MaxNbPoints() As Integer Implements IGrpSerie.MaxNbPoints Get If mSeries.Count > 0 Then Return mSeries(0).MaxNbPoints Else Return 0 End If End Get Set(ByVal value As Integer) If value >= 20 Then For Each lSerie As ClsSerie In mSeries lSerie.MaxNbPoints = value Next End If End Set End Property ''' <summary> ''' Retourne le nom de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Name() As String Implements IGrpSerie.Name Get Return mName End Get End Property ''' <summary> ''' Retourne la valeur maximale de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property MaxValue() As Integer Implements IGrpSerie.MaxValue Get Dim lInt As Integer If mSeries.Count > 0 Then lInt = mSeries(0).MaxValue For Each lSerie As ClsSerie In mSeries If lInt < lSerie.MaxValue Then lInt = lSerie.MaxValue End If Next Else lInt = 0 End If Return lInt End Get End Property ''' <summary> ''' Retourne la valeur minimale de la série ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property MinValue() As Integer Implements IGrpSerie.MinValue Get Dim lInt As Integer If mSeries.Count > 0 Then lInt = mSeries(0).MaxValue For Each lSerie As ClsSerie In mSeries If lInt > lSerie.MinValue Then lInt = lSerie.MinValue End If Next Else lInt = 0 End If Return lInt End Get End Property #End Region End Class Private mXMove As Integer = 0 Private mGridSize As Integer = 12 Private mValues As List(Of IGrpSerie) Private mChanged As Boolean = True Private mStep As Integer = 2 Public Event RefreshGraphic(ByVal pSender As CtlGraph) #Region "Propriétés" ''' <summary> ''' Retourne ou définit la vitesse de rafraichissement ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> <Category("Paramètres"), DefaultValue(1000), Description("Vitesse de rafraichissement")> _ Public Property Interval() As Integer Get Return TRefresh.Interval End Get Set(ByVal value As Integer) TRefresh.Interval = value Me.Invalidate() End Set End Property ''' <summary> ''' Retourne ou définit la taille de la grille ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> <Category("Paramètres"), DefaultValue(12), Description("Taille de la grille")> _ Public Property GridSize() As Integer Get Return mGridSize End Get Set(ByVal value As Integer) If value > 0 Then mGridSize = value Me.Invalidate() End If End Set End Property ''' <summary> ''' Retourne un booléen indiquant si le controle défile ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> _ Public Property Activate() As Boolean Get Return TRefresh.Enabled End Get Set(ByVal pValue As Boolean) TRefresh.Enabled = pValue End Set End Property ''' <summary> ''' Pas de progression du défilement (en pixel) ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> <Category("Paramètres"), DefaultValue(2), Description("Pas de progression en pixel")> _ Public Property [Step]() As Integer Get Return mStep End Get Set(ByVal value As Integer) If value >1 Then mStep value RefreshScreenhSize() End Set End Property <Category("Paramètres"), Description("Couleur de fond")> _ Public Overrides Property BackColor() As System.Drawing.Color Get Return MyBase.BackColor End Get Set(ByVal value As System.Drawing.Color) MyBase.BackColor = value End Set End Property <Category("Paramètres"), DefaultValue(100), Description("Couleur de la grille")> _ Public Overrides Property ForeColor() As System.Drawing.Color Get Return MyBase.ForeColor End Get Set(ByVal value As System.Drawing.Color) MyBase.ForeColor = value End Set End Property '<Category("Paramètres"), DefaultValue(100), Description("Valeur maximale possible")> _ 'Public Property MaxValue() As Integer ' Get ' Return _maxValue ' End Get ' Set(ByVal value As Integer) ' If _maxValue > 1 Then ' _maxValue = value ' Me.Invalidate() ' End If ' End Set 'End Property '''' <summary> '''' Retourne ou définit le texte à afficher '''' </summary> '''' <value></value> '''' <returns></returns> '''' <remarks></remarks> '<Category("Paramètres"), DefaultValue(""), Description("Le texte à afficher")> _ 'Public Overloads Overrides Property Text() As String ' Get ' Return MyBase.Text ' End Get ' Set(ByVal value As String) ' MyBase.Text = value ' Me.Invalidate() ' End Set 'End Property '<Category("Paramètres"), DefaultValue(False), Description("Indique si la valeur est exprimé en pourcentage ou si l'on utilise la forme valeur/valeur_max")> _ 'Public Property UsePourcentage() As Boolean ' Get ' Return _usePourcentage ' End Get ' Set(ByVal value As Boolean) ' _usePourcentage = value ' Me.Invalidate() ' End Set 'End Property #End Region ''' <summary> ''' Constructeur de la classe ''' </summary> ''' <remarks></remarks> Public Sub New() Ini() End Sub Public Sub AddSerie(ByVal pSerie As IGrpSerie) mValues.Add(pSerie) pSerie.MaxNbPoints = Me.Width \ mStep End Sub Private Sub Ini() Me.SetStyle(ControlStyles.ResizeRedraw, True) Me.SetStyle(ControlStyles.UserPaint, True) Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True) Me.SetStyle(ControlStyles.DoubleBuffer, True) InitializeComponent() mValues = New List(Of IGrpSerie)() End Sub Protected Overrides Sub OnResize(ByVal e As System.EventArgs) MyBase.OnResize(e) RefreshScreenhSize() End Sub Private Sub RefreshScreenhSize() If Me.Width \ mStep < 40 Then Exit Sub For Each lValue As IGrpSerie In mValues lValue.MaxNbPoints = Me.Width \ mStep + 20 Next End Sub Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) Dim lGridPen As New Pen(Me.ForeColor) For i As Integer = 0 To CInt((Me.Height / mGridSize) + 1) e.Graphics.DrawLine(lGridPen, 0, Height - i * mGridSize, Me.Width, Height - i * mGridSize) System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1) Next For i As Integer = 0 To CInt((Me.Width / mGridSize) + 2) e.Graphics.DrawLine(lGridPen, i * mGridSize - mXMove, 0, i * mGridSize - mXMove, Me.Height) System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1) Next SyncLock mValues For Each lSerie As IGrpSerie In mValues DrawGroupe(e.Graphics, lSerie, lSerie.MinValue, lSerie.MaxValue) Next End SyncLock ControlPaint.DrawBorder3D(e.Graphics, 0, 0, Me.Width, Me.Height, Border3DStyle.Sunken) End Sub Private Sub DrawGroupe(ByVal pGraphic As Graphics, ByVal pSerie As IGrpSerie, ByVal pMin As Integer, ByVal pMax As Integer) If TypeOf pSerie Is ClsSerie Then DrawSerie(pGraphic, DirectCast(pSerie, ClsSerie), pMin, pMax) ElseIf TypeOf pSerie Is ClsGroupe Then For Each lSerie As IGrpSerie In DirectCast(pSerie, ClsGroupe).Series DrawGroupe(pGraphic, lSerie, pMin, pMax) Next End If End Sub Private Sub DrawSerie(ByVal pGraphic As Graphics, ByVal pSerie As ClsSerie, ByVal pMin As Integer, ByVal pMax As Integer) Dim pen As Pen Dim lValues As List(Of Integer) Dim lOffsetY As Double Dim lRatio As Double Dim lOffsetX As Integer Dim lPrecPoint As PointF Dim lNextPoint As PointF pen = New Pen(pSerie.ForeColor, 1.0F) lValues = New List(Of Integer) lValues.AddRange(pSerie.Values) pen.DashStyle = DashStyle.Solid If pMin pMax Or lValues.Count 0 Then Exit Sub End If lOffsetX = Width - lValues.Count * mStep lOffsetY = Height '+ pMin lRatio = Height / (pMax - pMin) For i As Integer = 0 To lValues.Count - 1 lPrecPoint = lNextPoint lNextPoint = New PointF(lOffsetX + i * mStep, CSng(lOffsetY - (lValues(i) - pMin) * lRatio)) If i > 0 Then pGraphic.DrawLine(pen, lPrecPoint, lNextPoint) End If Next End Sub Protected Overloads Overrides Sub OnPaintBackground(ByVal pevent As PaintEventArgs) MyBase.OnPaintBackground(pevent) Dim bounds As New Rectangle(0, 0, Me.Width, Me.Height) pevent.Graphics.FillRectangle(New SolidBrush(BackColor), bounds) End Sub Private Sub TRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TRefresh.Tick RaiseEvent RefreshGraphic(Me) mXMove = (mXMove + mStep) Mod (mGridSize * mStep) Me.Invalidate() End Sub End Class