+ Reply to Thread
Results 1 to 2 of 2

Thread: Tutorial: VS2008 C# Pie Chart

  1. #1
    Jordan Guest

    Tutorial: VS2008 C# Pie Chart

    C# CREATE A PIE CHART FROM NUMERICAL DATA


    1. INTRODUCTION

    In this tutorial we will create a form in which the user can enter numerical data, integer or real numbers. When a button is pressed the user should see a pie-chart created by the numerical values entered. We will make use of the graphics capabilities the .Net provides to draw lines, shapes and text. For example, the pen class is used to create lines or curves while the brush class is useful for filling the internal region of shapes with colors. A variation of the SolidBrush class is the one named “LinearGradientBrush” which is a brush with a linear gradient between two colors and it can give a very aesthetic appearance to your shapes.

    All classes for our purpose derive from the System.Drawing.Drawing2D namespace so make sure you have added it to your project. To create the pie chart we must use a pen, but for the creation of the individual pie shapes we need a rectangle. We don’t actually draw a rectangle; instead we only define it as a structure. This is necessary because the size of the pie-chart is depended on an imaginary rectangle just outside of it. Finally, we use a SolidBrush object to fill the pie-chart with different colors for each region.


    2. GRAPHICAL USER INTERFACE

    Start a new project and name it “Pie-Chart”. Create a form just like the one in picture1. Make sure that the size of the form is 300x230. Insert four textboxes and a command button as they are depicted in the picture. When designing programs with integrated graphics on the form you should be careful with the location of the controls and the area in which the drawing will take place.



    3. CODE

    We need to extract the values of the numbers entered on the textboxes into integers (you can also try float variables). Since the textbox’s text property is a string, we need to convert it to its integer equivalent. This is accomplished with the “Parse ” method. To store each textbox in a separate variable copy this code on the button’s click event:


    Code:
    int i1 = int.Parse(textBox1.Text);
    int i2 = int.Parse(textBox2.Text);
    int i3 = int.Parse(textBox3.Text);
    int i4 = int.Parse(textBox4.Text);
    The next thing to do is to calculate the amount of degrees each region of the pie-chart should cover. This is based on the ratio of the specified number to the total. For conversion to Degrees we just multiply it by 360. Enter this code snippet after the previous one:


    Code:
    float total = i1 + i2 + i3 + i4;
    float deg1 = (i1/total) * 360 ;
    float deg2 = (i2/total) * 360 ;
    float deg3 = (i3/total) * 360 ;
    float deg4 = (i4/total) * 360 ;
    For the design of the pie-chart we first need to create (but not draw!) the rectangle that will form the boundary of the pie-shaped structures. To accomplish this, we need the x and y coordinates of its upper left corner, its width and its height. In order to avoid an overlapping of a textbox with the shape we will create, we set the rectangle’s upper left corner to be just after the right corner (or side) of the textboxes. The width and height is set to 150 pixels. To finish our drawing we need a graphics object that will be used to create the pie-shaped individual elements. For this purpose, we just call the “DrawPie” method entering as parameters the pen that will be used to draw the lines of the pie, the imaginary rectangle, the start angle and the number of angles it will occupy (not the end angle). After the creation of each pie we fill it with the SolidBrush object we created by using the “FillPie” method of the Graphics object.


    Definitions of the methods used are:

    DrawPie(Pen object used for the lines, imaginary Rectangle, start degree, number of degrees to occupy)

    FillPie(Brush object to fill the shape, imaginary Rectangle, start degree, number of degrees to occupy)


    The code for this function is given below:

    Code:
    Pen p = new Pen(Color.Black,2);
    
    Graphics g = this.CreateGraphics();
    
    Rectangle rec = new Rectangle(textBox1.Location.X + textBox1.Size.Width+10,12,150,150);
    
               
    Brush b1 = new SolidBrush(Color.Red);
    Brush b2 = new SolidBrush(Color.Blue);
    Brush b3 = new SolidBrush(Color.Black);
    Brush b4 = new SolidBrush(Color.BlueViolet);
    
            
    g.Clear(Form1.DefaultBackColor);
    g.DrawPie(p, rec, 0, deg1);
    g.FillPie(b1, rec, 0, deg1);
    g.DrawPie(p, rec, deg1,  deg2);
    g.FillPie(b2, rec, deg1, deg2);
    g.DrawPie(p, rec, deg2 + deg1, deg3);
    g.FillPie(b3, rec, deg2 + deg1, deg3);
    g.DrawPie(p, rec, deg3 + deg2 + deg1,  deg4);
    g.FillPie(b4, rec, deg3 + deg2 + deg1, deg4);
    Save your project and do a debug test to see if it functioning correctly. You should get something like the one in picture2.



    Questions/Comments?
    Post them here or create a new thread in the appropriate category.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: VS2008 C# Pie Chart

    This brings us back to the discussion about how important maths is to programming!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 06-01-2010, 01:00 PM
  2. Problem viewing ASP.NET pages in VS2008
    By jackolantern in forum ASP, ASP.NET and Coldfusion
    Replies: 6
    Last Post: 05-13-2010, 11:42 PM
  3. VS2008 compiling problem
    By A. Shuaib in forum C and C++
    Replies: 4
    Last Post: 04-06-2010, 03:54 PM
  4. AddFnction Dialog Boxe in MS VS2008
    By geoyar in forum Software Development Tools
    Replies: 0
    Last Post: 05-30-2009, 07:11 PM
  5. VS2008 C# Sequential Lists: The Queue And Stack Class
    By Jordan in forum CSharp Tutorials
    Replies: 5
    Last Post: 05-26-2008, 09:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts