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:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    
    namespace YearlySales2
    {
        public partial class Form1 : Form
        {
            Graphics graphDrawingArea;
            Bitmap bmpDrawingArea;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bmpDrawingArea = new Bitmap(Width, Height);
                graphDrawingArea = Graphics.FromImage(bmpDrawingArea);
            }
        }
    }
  4. Return to the form and click an empty area on it. In the Properties window, click the Events button Events
  5. Double-click the Paint field and implement its event as follows:
     
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
                e.Graphics.DrawImage(bmpDrawingArea, 0, 0);
    }
  6. Return to the form and double-click the Generate button
  7. Implement the event as follows:
     
    private void btnGenerate_Click(object sender, EventArgs e)
    {
                // Retrieve the values of the current year's sales
                int curQtr1 = 0;
                int curQtr2 = 0;
                int curQtr3 = 0;
                int curQtr4 = 0;
    
                try
                {
                    curQtr1 = int.Parse(txtCurrentQtr1.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    curQtr2 = int.Parse(txtCurrentQtr2.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    curQtr3 = int.Parse(txtCurrentQtr3.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    curQtr4 = int.Parse(txtCurrentQtr4.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                // Create an array of Rectangle objects for the current year
                Rectangle[] rectCurrentYear =
                {
                    new Rectangle(this.txtCurrentQtr1.Left+20,
    			                  380-curQtr1, 40, curQtr1),
                    new Rectangle(this.txtCurrentQtr2.Left+20,
    			                   380-curQtr2, 40, curQtr2),
    		        new Rectangle(this.txtCurrentQtr3.Left+20,
    			                  380-curQtr3, 40, curQtr3),
    		        new Rectangle(this.txtCurrentQtr4.Left+20,
    			                  380-curQtr4, 40, curQtr4)
                };
    
                // Retrieve the values of last year's sales
                int prvQtr1 = 0;
                int prvQtr2 = 0;
                int prvQtr3 = 0;
                int prvQtr4 = 0;
    
                try
                {
                    prvQtr1 = int.Parse(txtPreviousQtr1.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    prvQtr2 = int.Parse(txtPreviousQtr2.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    prvQtr3 = int.Parse(txtPreviousQtr3.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    prvQtr4 = int.Parse(txtPreviousQtr4.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                // Create an array of Rectangle objects for the previous year
                Rectangle[] rectPreviousYear =
                {
                    new Rectangle(this.txtPreviousQtr1.Left+30,
    			                  380-prvQtr1, 40, prvQtr1),
     		        new Rectangle(this.txtPreviousQtr2.Left+30,
    			                  380-prvQtr2, 40, prvQtr2),
    	            new Rectangle(this.txtPreviousQtr3.Left+30,
    			                  380-prvQtr3, 40, prvQtr3),
    		        new Rectangle(this.txtPreviousQtr4.Left+30,
    			                  380-prvQtr4, 40, prvQtr4)
                };
    
        // In case the user has changed the values, erase the previous chart
                graphDrawingArea.Clear(this.BackColor);
    
                HatchBrush brushDiagCross =
                 new HatchBrush(HatchStyle.DiagonalCross,
                          Color.White, Color.Blue);
                HatchBrush brushDotDiamond =
                 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,
                                this.lblCurYear.Left - 30,
                                this.lblCurYear.Top, 20, 14);
                graphDrawingArea.DrawRectangle(new Pen(Color.Red),
                                this.lblCurYear.Left - 30,
                                this.lblCurYear.Top, 20, 14);
                graphDrawingArea.FillRectangle(brushDiagCross,
                                this.lblLastYear.Left - 30,
                                this.lblLastYear.Top, 20, 14);
                graphDrawingArea.DrawRectangle(new Pen(Color.Blue),
                                this.lblLastYear.Left - 30,
                                this.lblLastYear.Top, 20, 14);
    
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                                25, 380, Width - 220, 1);
                Invalidate();
    }
  8. Return to the form. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
                Close();
    }
  9. Execute the application and test the form
     
     Yearly Sales
  10. After using it, close the form
  11. Change the Paint event of the form as follows:
     
    private void btnGenerate_Click(object sender, EventArgs e)
    {
            // Retrieve the values of the current year's sales
            int curQtr1 = 0;
            int curQtr2 = 0;
            int curQtr3 = 0;
            int curQtr4 = 0;
    
            try
            {
                    curQtr1 = int.Parse(txtCurrentQtr1.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    curQtr2 = int.Parse(txtCurrentQtr2.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    curQtr3 = int.Parse(txtCurrentQtr3.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    curQtr4 = int.Parse(txtCurrentQtr4.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            // Create an array of Rectangle objects for the current year
            Rectangle[] rectCurrentYear =
            {
                    new Rectangle(this.txtCurrentQtr1.Left+20,
    			                  380-curQtr1, 40, curQtr1),
                    new Rectangle(this.txtCurrentQtr2.Left+20,
    			                   380-curQtr2, 40, curQtr2),
    		        new Rectangle(this.txtCurrentQtr3.Left+20,
    			                  380-curQtr3, 40, curQtr3),
    		        new Rectangle(this.txtCurrentQtr4.Left+20,
    			                  380-curQtr4, 40, curQtr4)
            };
    
            // Retrieve the values of last year's sales
            int prvQtr1 = 0;
            int prvQtr2 = 0;
            int prvQtr3 = 0;
            int prvQtr4 = 0;
    
            try
            {
                    prvQtr1 = int.Parse(txtPreviousQtr1.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    prvQtr2 = int.Parse(txtPreviousQtr2.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    prvQtr3 = int.Parse(txtPreviousQtr3.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            try
            {
                    prvQtr4 = int.Parse(txtPreviousQtr4.Text) / 100;
            }
            catch (FormatException)
            {
                    MessageBox.Show("Invalid Value");
            }
    
            // Create an array of Rectangle objects for the previous year
            Rectangle[] rectPreviousYear =
            {
                    new Rectangle(this.txtPreviousQtr1.Left+30,
    			                  380-prvQtr1, 40, prvQtr1),
     		        new Rectangle(this.txtPreviousQtr2.Left+30,
    			                  380-prvQtr2, 40, prvQtr2),
    	            new Rectangle(this.txtPreviousQtr3.Left+30,
    			                  380-prvQtr3, 40, prvQtr3),
    		        new Rectangle(this.txtPreviousQtr4.Left+30,
    			                  380-prvQtr4, 40, prvQtr4)
            };
    
            // In case the user has changed the values, erase the previous chart
            graphDrawingArea.Clear(this.BackColor);
    
            Rectangle rect = new Rectangle(10, 190, 300, 210);
            LinearGradientBrush linGradBrush =
    		new LinearGradientBrush(rect,
    		  Color.FromArgb(204, 102, 0),
    		  Color.AntiqueWhite,
    		          LinearGradientMode.Vertical);
            graphDrawingArea.FillRectangle(linGradBrush, rect);
            graphDrawingArea.DrawRectangle(new Pen(Color.Black), rect);
    
            HatchBrush brushDiagCross =
                 new HatchBrush(HatchStyle.DiagonalCross,
                          Color.White, Color.Blue);
            HatchBrush brushDotDiamond =
                 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,
                                this.lblCurYear.Left - 30,
                                this.lblCurYear.Top, 20, 14);
            graphDrawingArea.DrawRectangle(new Pen(Color.Red),
                                this.lblCurYear.Left - 30,
                                this.lblCurYear.Top, 20, 14);
            graphDrawingArea.FillRectangle(brushDiagCross,
                                this.lblLastYear.Left - 30,
                                this.lblLastYear.Top, 20, 14);
            graphDrawingArea.DrawRectangle(new Pen(Color.Blue),
                                this.lblLastYear.Left - 30,
                                this.lblLastYear.Top, 20, 14);
    
            graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                                25, 380, Width - 220, 1);
            Invalidate();
    }
  12. Execute the application to test it:
     
    Yearly Sales
  13. After using the form, close it
 

Home Copyright © 2007 FunctionX, Inc.