Générateur de graphique

Description

Générateur de graphique avec histogramme et/ou courbe, droite, cible...
Ajoutez l'élément graphique de la DLL dans un Form et utilisez les fonctions de la classe pour ajouter des valeurs ou modifier les paramètres.

Source / Exemple :


'Source de la DLL

'Date    : 02/08/2011 
'Auteur  : Mauran Pascal
'Version : 1.0.0

Public Class OE_PE_Graph
    Private bkColor As Color
    Private titreGraphique As String
    Private titreVisible As Boolean
    Private courbeVisible As Boolean
    Private curveVisible As Boolean
    Private histoVisible As Boolean
    Private valeurVisible As Boolean
    Private cibleVisible As Boolean

    Private ligneZeroVisible As Boolean

    Private couleurCourbe As Color

    Private couleurValeur As Brush

    Private animation As Boolean

    Private version As String

    Dim NewGraphic As System.Drawing.Graphics

    Dim x, y As Integer

    Dim l As Label

    Structure DataGraphique
        Dim nbValeurs As Integer
        Dim cible As Double
        Dim tabValeur() As Double

        Dim c() As Pen
        Dim b() As Brush
        Dim pF() As PointF
    End Structure

    Private ThePen As New System.Drawing.Pen(Color.Red)

    Private pDG As DataGraphique

    Public Sub SetCouleurLigne(ByRef c As Color)

    End Sub

    Public Sub AddValue(ByVal v As Double)

        ReDim Preserve pDG.tabValeur(0 To pDG.nbValeurs)
        ReDim Preserve pDG.c(0 To pDG.nbValeurs)
        ReDim Preserve pDG.b(0 To pDG.nbValeurs)
        ReDim Preserve pDG.pF(0 To pDG.nbValeurs)
        pDG.tabValeur(pDG.nbValeurs) = v
        pDG.nbValeurs += 1
    End Sub

    Public Sub SetCouleurValeur(ByVal b As Brush)
        couleurValeur = b
    End Sub

    Public Sub SetCourbeVisible(ByVal b As Boolean)
        courbeVisible = b
    End Sub

    Public Sub SetCurveVisible(ByVal b As Boolean)
        curveVisible = b
    End Sub

    Public Sub SetHistogrammeVisible(ByVal b As Boolean)
        histoVisible = b
    End Sub

    Public Sub SetCibleVisible(ByVal b As Boolean)
        cibleVisible = b
    End Sub

    Public Sub SetCible(ByVal c As Double)
        pDG.cible = c
    End Sub

    Public Sub SetTitre(ByVal titre As String)
        titreGraphique = titre
        LblTitre.Text = titreGraphique
        titreVisible = True
    End Sub

    Public Sub SetTitreVisible(ByVal b As Boolean)
        titreVisible = b
        LblTitre.Visible = titreVisible
    End Sub

    Public Sub SetValeurVisible(ByVal b As Boolean)
        valeurVisible = b
    End Sub

    Public Sub SetLigneZeroVisible(ByVal b As Boolean)
        ligneZeroVisible = b
    End Sub

    Public Sub SetGraphBkgColor(ByVal couleur As Color)
        bkColor = couleur
    End Sub

    Private Sub OE_PE_Graph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        DrawGraph()
    End Sub

    Public Sub SetFontTitre(ByVal taille As Single, ByVal gras As Boolean, ByVal italic As Boolean, ByVal souligne As Boolean)

        Dim f As Font

        f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Regular)

        If gras = True And italic = True And souligne = True Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)
        End If
        If gras = True And italic = False And souligne = False Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Bold)
        End If
        If gras = True And italic = True And souligne = False Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Bold Or FontStyle.Italic)
        End If
        If gras = True And italic = False And souligne = True Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Bold Or FontStyle.Underline)
        End If
        If gras = False And italic = True And souligne = True Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Italic Or FontStyle.Underline)
        End If
        If gras = False And italic = True And souligne = False Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Italic)
        End If
        If gras = False And italic = False And souligne = True Then
            f = New Font(FontFamily.GenericSansSerif, taille, FontStyle.Underline)
        End If
        

        LblTitre.Font = f

    End Sub

    Private Sub FrmGraph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        titreVisible = False
        LblTitre.Visible = titreVisible
        courbeVisible = False
        curveVisible = False
        valeurVisible = False
        histoVisible = True
        cibleVisible = True
        ligneZeroVisible = True

        l = New Label
        l.Parent = Me
        couleurValeur = Brushes.Beige

        bkColor = Color.Beige

        Me.BackColor = bkColor

    
        NewGraphic = Me.CreateGraphics()
        pDG.nbValeurs = 0
        ReDim pDG.tabValeur(0 To pDG.nbValeurs)
        ReDim pDG.c(0 To pDG.nbValeurs)
        ReDim pDG.b(0 To pDG.nbValeurs)
        ReDim pDG.pF(0 To pDG.nbValeurs)

        version = "SLP_Graph_Ctrl - V 1.0.0 - 01/08/2011" & vbCrLf & vbCrLf & "Pascal Mauran"

        Dim sizeFont As Single
        Dim f As Font
        sizeFont = 22

        f = New Font(FontFamily.GenericSansSerif, sizeFont, FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)

        LblTitre.Font = f
    End Sub

    Private Sub FrmGraph_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        NewGraphic = Me.CreateGraphics()
        NewGraphic.Clear(Me.BackColor)
        DrawGraph()
    End Sub

    Private Sub DrawGraph()
        Dim i As Integer
        Dim max, min As Double
        Dim coef As Double
        Dim largeurBarre As Integer
        Dim margeHaut, margeBas As Integer
        Dim margeGauche, margeDroite As Integer
        Dim s As String
        Dim xLabel As Integer

        Dim x1, x2, y1, y2 As Integer

        Dim positionZero As Double

        Dim heightGraph As Double
        Dim widthGraph As Double

        x1 = (Me.Width / 2) - (LblTitre.Width / 2)
        LblTitre.Left = x1

        NewGraphic = Me.CreateGraphics()
        NewGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed

        margeHaut = 50
        margeBas = 40 + 20 '40 minimun

        margeGauche = 20
        margeDroite = 40

        heightGraph = Me.Height - margeHaut - margeBas
        widthGraph = Me.Width - margeGauche - margeDroite

        Dim f As Font
        Dim sf As Single

        For i = 0 To pDG.nbValeurs - 1
            If pDG.tabValeur(i) < pDG.cible Then
                pDG.c(i) = New System.Drawing.Pen(System.Drawing.Color.Red)
                pDG.b(i) = Brushes.Red
            Else
                pDG.c(i) = New System.Drawing.Pen(System.Drawing.Color.Green)
                pDG.b(i) = Brushes.Green
            End If

        Next

        If pDG.nbValeurs > 0 Then

            'recherche des valeurs min et max afin de définir l'échelle
            max = 0
            For i = 0 To pDG.nbValeurs - 1
                If max < pDG.tabValeur(i) Then max = pDG.tabValeur(i)
            Next
            min = max
            For i = 0 To pDG.nbValeurs - 1
                If min > pDG.tabValeur(i) Then min = pDG.tabValeur(i)
            Next
            coef = heightGraph / max

            sf = (heightGraph + widthGraph) / 100
            sf = 10
            f = New Font(FontFamily.GenericSansSerif, sf, FontStyle.Bold)

            If min < 0 Then
                coef = heightGraph / (max + (min * -1))
                positionZero = (min * -1) * coef
            End If
            largeurBarre = (Me.Width - margeGauche - margeDroite) / pDG.nbValeurs

            '*****************************
            '*** Affichage histogramme ***
            '*****************************
            If histoVisible = True Then
                For i = 0 To pDG.nbValeurs - 1
                    'Définition de la taille et de la position du rectangle
                    x1 = (largeurBarre * i) + 1 + margeGauche
                    x2 = x1 + largeurBarre - 1
                    y2 = heightGraph - positionZero
                    y1 = (y2 - (pDG.tabValeur(i) * coef) + 1)

                    If pDG.tabValeur(i) >= 0 Then 'Cas valeur positive
                        'Dessin du rectangle
                        NewGraphic.DrawRectangle(pDG.c(i), x1, y1 + margeHaut, largeurBarre - 1, y2 - y1)
                        NewGraphic.FillRectangle(pDG.b(i), x1 + 2, y1 + 2 + margeHaut, largeurBarre - 4, y2 - y1 - 2)
                    Else 'Cas valeur négative
                        'Dessin du rectangle
                        Dim he As Integer
                        he = (pDG.tabValeur(i) * coef) * -1 + margeHaut
                        'NewGraphic.FillRectangle(pDG.b(i), x1 + 2, y2 + 2 + margeHaut, largeurBarre - 4, he - margeHaut)
                        NewGraphic.FillRectangle(pDG.b(i), x1 + 2, y2 + margeHaut, largeurBarre - 4, he - margeHaut - 1)
                        NewGraphic.DrawRectangle(Pens.Black, x1, y2 + margeHaut, largeurBarre - 1, he - margeHaut)
                    End If

                Next
            End If

            '******************************
            '*** Affichage de la valeur ***
            '******************************
            If valeurVisible = True Then
                For i = 0 To pDG.nbValeurs - 1
                    'Définition de la taille et de la position du rectangle
                    x1 = (largeurBarre * i) + 1 + margeGauche
                    x2 = x1 + largeurBarre - 1
                    y2 = heightGraph - positionZero
                    y1 = (y2 - (pDG.tabValeur(i) * coef) + 1)

                    s = pDG.tabValeur(i).ToString
                    xLabel = x1 + ((largeurBarre - (f.Size * s.Length())) / 2)

                    If pDG.tabValeur(i) >= 0 Then 'Cas valeur positive
                        NewGraphic.DrawString(pDG.tabValeur(i), f, couleurValeur, xLabel, y1 + margeHaut)
                    Else 'Cas valeur négative
                        NewGraphic.DrawString(pDG.tabValeur(i), f, couleurValeur, xLabel, y1 - f.Height + margeHaut)
                    End If
                Next
            End If

            '******************************
            '*** Affichage de la courbe ***
            '******************************
            If courbeVisible = True Or curveVisible = True Then
                For i = 0 To pDG.nbValeurs - 1
                    pDG.pF(i).X = ((widthGraph / pDG.nbValeurs) * (i + 1)) - ((widthGraph / pDG.nbValeurs) / 2) + margeGauche
                    pDG.pF(i).Y = heightGraph - positionZero - (pDG.tabValeur(i) * coef) + margeHaut '+ margeBas
                Next
                Dim p As New System.Drawing.Pen(System.Drawing.Color.Red)
                p.Width = 3
                p.Color = Color.Black

                If curveVisible = True Then
                    NewGraphic.DrawCurve(p, pDG.pF)
                End If

                If courbeVisible = True Then
                    NewGraphic.DrawLines(p, pDG.pF)
                End If

            End If

            If ligneZeroVisible = True Then
                If min < 0 Then
                    coef = heightGraph / (max + (min * -1))
                    positionZero = (min * -1) * coef
                    x1 = margeGauche
                    x2 = margeGauche + widthGraph
                    y2 = heightGraph - positionZero + margeHaut
                    NewGraphic.DrawLine(Pens.Peru, x1, y2, x2, y2)
                    NewGraphic.DrawString("0", f, Brushes.Black, x1 - f.SizeInPoints, y2 - (f.Height / 2))
                End If
            End If

            '*****************************
            '*** Affichage de la cible ***
            '*****************************
            If cibleVisible = True Then
                x1 = margeGauche
                x2 = margeGauche + widthGraph
                y2 = (heightGraph - positionZero + margeHaut) - (pDG.cible * coef)
                Dim penCible As Pen
                penCible = New Pen(Color.Black)
                penCible.DashOffset = 10
                penCible.DashCap = Drawing2D.DashCap.Triangle
                penCible.DashStyle = Drawing2D.DashStyle.Dash
                penCible.Width = 2
                NewGraphic.DrawLine(penCible, x1, y2, x2, y2)
                'Affichage de la valeur de la cible
                NewGraphic.DrawString("Cible = " & pDG.cible, f, Brushes.Black, x1, y2 - f.Height)
            End If

            NewGraphic.Dispose()
        End If

    End Sub

    Private Sub CmdAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAbout.Click

        InitAnimation()
    End Sub

    Private Sub InitAnimation()
        y = Me.Height
        animation = True
        Timer1.Interval = 10
        l.Visible = True
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If animation = True Then
            If y > -l.Height Then
                l.Text = version
                l.Width = 250
                l.Height = 60

                l.BorderStyle = Windows.Forms.BorderStyle.FixedSingle

                x = (Me.Width / 2) - (l.Width / 2)

                l.Left = x
                l.Top = y

                l.TextAlign = ContentAlignment.MiddleCenter

                y -= 1
            Else
                l.Visible = False
                Timer1.Stop()
            End If
        End If

    End Sub

End Class

Conclusion :


Les propositions d'amélioration sont les bienvenues....

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.