Home

GDI+ Application Yearly Sales

 

Yearly Sales

Introduction

This is an example of a vertical rectangular chart. To draw the rectangles, we use hatch brushes.

Windows Controls:

     

Practical LearningPractical Learning: Using a Hatch Brush

  1. Start a new Windows Forms Application named YearlySales2
  2. Design the form as follows:
     
    Company Yearly Sales
     
    Control Name Text
    GroupBox GroupBox   Current Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtCurrentQtr1 0
    TextBox TextBox txtCurrentQtr2 0
    TextBox TextBox txtCurrentQtr3 0
    TextBox TextBox txtCurrentQtr4 0
    Button Button Close btnClose
    GroupBox GroupBox   Previous Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtPreviousQtr1 0
    TextBox TextBox txtPreviousQtr2 0
    TextBox TextBox txtPreviousQtr3 0
    TextBox TextBox txtPreviousQtr4 0
    Button Button Generate btnGenerate
    Label Label    Legend
    Label Label   This Year's Sales
    Label Label   Last Year's Sales
  3. Double-click an unoccupied area of the form and change the file as follows:
    Imports System.Drawing.Drawing2D
    
    Public Class Form1
    
        Private graphDrawingArea As Graphics
        Private bmpDrawingArea As Bitmap
    
        Private Sub Form1Load(ByVal sender As System.Object, 
                               ByVal e As System.EventArgs) 
                               Handles MyBase.Load
            bmpDrawingArea = New Bitmap(Width, Height)
            graphDrawingArea = Graphics.FromImage(bmpDrawingArea)
        End Sub
    End Class
  4. In Method Name combo box, select Paint and implement its event as follows:
    Private Sub Form1Paint(ByVal sender As Object, 
                            ByVal e As System.Windows.Forms.PaintEventArgs) 
                            Handles Me.Paint
            e.Graphics.DrawImage(bmpDrawingArea, 0, 0)
    End Sub
  5. In the Class Name combo box, select btnGenerate
  6. In the Method Name combo box, select Click and implement the event as follows:
    Private Sub btnGenerateClick(ByVal sender As Object, 
                                      ByVal e As System.EventArgs) 
                                      Handles btnGenerate.Click
            ' Retrieve the values of the current year's sales
            Dim CurQtr1 As Integer
            Dim CurQtr2 As Integer
            Dim CurQtr3 As Integer
            Dim CurQtr4 As Integer
    
            Try
                CurQtr1 = CInt(txtCurrentQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr2 = CInt(txtCurrentQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr3 = CInt(txtCurrentQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr4 = CInt(txtCurrentQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the current year
            Dim RectCurrentYear As Rectangle() = { 
                    New Rectangle(txtCurrentQtr1.Left + 20, 
                                380 - CurQtr1, 40, CurQtr1), 
                    New Rectangle(txtCurrentQtr2.Left + 20, 
                                 380 - CurQtr2, 40, CurQtr2), 
                    New Rectangle(txtCurrentQtr3.Left + 20, 
                                380 - CurQtr3, 40, CurQtr3), 
                    New Rectangle(txtCurrentQtr4.Left + 20, 
                                380 - CurQtr4, 40, CurQtr4)}
    
            ' Retrieve the values of last year's sales
            Dim PrvQtr1 As Integer
            Dim PrvQtr2 As Integer
            Dim PrvQtr3 As Integer
            Dim PrvQtr4 As Integer
    
            Try
                PrvQtr1 = CInt(txtPreviousQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr2 = CInt(txtPreviousQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr3 = CInt(txtPreviousQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr4 = CInt(txtPreviousQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the previous year
            Dim RectPreviousYear As Rectangle() = { 
                    New Rectangle(txtPreviousQtr1.Left + 30, 
                         380 - PrvQtr1, 40, PrvQtr1), 
                    New Rectangle(txtPreviousQtr2.Left + 30, 
                         380 - PrvQtr2, 40, PrvQtr2), 
                    New Rectangle(txtPreviousQtr3.Left + 30, 
                         380 - PrvQtr3, 40, PrvQtr3), 
                    New Rectangle(txtPreviousQtr4.Left + 30, 
                         380 - PrvQtr4, 40, PrvQtr4)}
    
            ' In case the user has changed the values, erase the previous chart
            GraphDrawingArea.Clear(BackColor)
    
            Dim brushDiagCross As HatchBrush = 
                 New HatchBrush(HatchStyle.DiagonalCross, 
                          Color.White, Color.Blue)
            Dim brushDotDiamond As HatchBrush = 
             New HatchBrush(HatchStyle.DottedDiamond, 
                      Color.Fuchsia, Color.Brown)
    
            ' Draw the chart for the previous year first to send it back
            GraphDrawingArea.FillRectangles(brushDiagCross, 
                                 RectPreviousYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Blue), 
                                 RectPreviousYear)
            ' Draw the chart for the current year in front
            GraphDrawingArea.FillRectangles(brushDotDiamond, 
                                 RectCurrentYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Red), 
                                 RectCurrentYear)
    
            ' Draw the small rectangles of the legend
                graphDrawingArea.FillRectangle(brushDotDiamond,
                                lblCurYear.Left - 30,
                                lblCurYear.Top, 20, 14)
                graphDrawingArea.DrawRectangle(new Pen(Color.Red),
                                lblCurYear.Left - 30,
                                lblCurYear.Top, 20, 14)
                graphDrawingArea.FillRectangle(brushDiagCross,
                                lblLastYear.Left - 30,
                                lblLastYear.Top, 20, 14)
                graphDrawingArea.DrawRectangle(new Pen(Color.Blue),
                                lblLastYear.Left - 30,
                                lblLastYear.Top, 20, 14)
    
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                                25, 380, Width - 220, 1)
            Invalidate()
    End Sub
  7. Execute the application and test the form
     
     Yearly Sales
  8. After using it, close the form
  9. Change the Paint event of the form as follows:
    Private Sub btnGenerateClick(ByVal sender As Object, 
                                      ByVal e As System.EventArgs) 
                                      Handles btnGenerate.Click
            . . . No Change
            
    
            ' In case the user has changed the values, erase the previous chart
            GraphDrawingArea.Clear(BackColor)
    
            Dim rect As Rectangle = New Rectangle(10, 190, 300, 210)
            Dim linGradBrush As LinearGradientBrush = 
          		New LinearGradientBrush(rect, 
            Color.FromArgb(204, 102, 0), 
            Color.AntiqueWhite, 
                    LinearGradientMode.Vertical)
            GraphDrawingArea.FillRectangle(linGradBrush, rect)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), rect)
    
            Dim brushDiagCross As HatchBrush = 
                 New HatchBrush(HatchStyle.DiagonalCross, 
                          Color.White, Color.Blue)
            . . . No Change
    End Sub
  10. Execute the application to test it:
     
    Yearly Sales
  11. After using the form, close it

Download

 

Home Copyright © 2008-2016, FunctionX, Inc. Home