Вы находитесь на странице: 1из 4

Create Pie Chart Using Graphics in C# .

NET - CodeProject

http://www.codeproject.com/Articles/463284/Create-Pie-Chart-Using-...

Not quite what you are looking for? You may want to try: 3D Pie Chart A WPF Pie Chart with Data Binding Support
9,890,640 members (48,750 online) manuu

highlights off
2K Sign out

home

articles

quick answers

discussions

features

community

help

pie chart c#

Articles Multimedia General Graphics General

Next

Article Browse Code Stats Revisions (2) Alternatives

Create Pie Chart Using Graphics in C# .NET


By hari19113, 21 Sep 2012
4.67 (6 votes)

About Article
This article shows how to create a Pie chart using the Graphics class in C#. Type Licence First Posted Article CPOL 21 Sep 2012 13,009 1,053 13 times

Download source code - 20.4 KB


Comments & Discussions (4)

Download source and demo - 57.1 KB

Add your own alternative version

Introduction
This program is used to create a pie chart using the Graphics class in C#. I have made it as good as I can, but if anyone has any suggestions please share it so we can all learn.

Views Downloads Bookmarked

C# .NET Visual-Studio

Using the code


Recently I have been fascinated by the graphics class and I was just experimenting I came upon DrawPie() and FillPie() method of the Graphics class. For a simple demo I have created a form with five textboxes, a button, and a picturebox and we are going to create a pie chart in this picturebox.

GDI+ Beginner Forms

Before creating a pie chart we must always keep in mind that we can't create a circle out of normal measurements. To create a circle we need values in degrees. To convert into degrees first we will sum up all the values, i.e., calculate total sum of all values in all the textboxes and then divide the each values with total and then multiply with 360.
Collapse | Copy Code

int int int int int

i1 i2 i3 i4 i5

= = = = =

Int32.Parse(textBox1.Text); Int32.Parse(textBox2.Text); Int32.Parse(textBox3.Text); Int32.Parse(textBox4.Text); Int32.Parse(textBox5.Text);

float total = i1 + i2 + i3 + i4 + i5 ; float float float float float deg1 deg2 deg3 deg4 deg5 = = = = = (i1 (i2 (i3 (i4 (i5 / / / / / total) total) total) total) total) * * * * * 360; 360; 360; 360; 360;

Top News
The Next Version of Android - Some of What's Coming
Get the Insider News free each

After converting values we will create an instance of graphics class:

1 of 4

25/05/2013 01:17

Create Pie Chart Using Graphics in C# .NET - CodeProject

http://www.codeproject.com/Articles/463284/Create-Pie-Chart-Using-...

Collapse | Copy Code

morning.

Graphics graphics = pictureBox1.CreateGraphics();

After this we will create a rectangular area in which we will create the pie chart:
Collapse | Copy Code

Related Videos

float deg1 = (i1 / total) * 360; Rectangle rect = new Rectangle(0, 0, 150, 150);

First two parameters specify the drawing points. I.e., the X and Y co-ordinates and the third and fourth parameter specifies the size of the rectangle and in this case it will determine the size of our pie chart. If you have observed every sections in pie chart are indicated by different colors. So we are going to do the same we will create five different brush:
Collapse | Copy Code

C# .NET Programming - 1508 Exercise 2 - Bar Charts Using Array

Brush brush1 = new SolidBrush(Color.Red); Brush brush2 = new SolidBrush(Color.Blue); Brush brush3 = new SolidBrush(Color.Maroon); Brush brush4 = new SolidBrush(Color.Navy); Brush brush5 = new SolidBrush(Color.YellowGreen); graphics.Clear(pictureBox1.BackColor);

Beginners Java Programming - 1507 Exercise 2 - Displaying Bar Charts...

Now to create our pie chart. Now graphics.FillPie(); accepts four parameters: 1. 2. 3. 4. Brush: Brush to fill the section which we have specified in brush. Rectangle: Rectangle area where the particular pie will be created. StartAngle: The start point from where to start the pie chart. SweepAngle: The point till where pie chart will be created.

Related Articles
3D Pie Chart A Simple Pie Chart Control Improved 3-D Pie Chart A WPF Pie Chart with Data Binding Support Pie Chart Control
Collapse | Copy Code

Basically graphics.FillPie(); never creates a full pie diagram it just creates a section(arc) of pie diagram and we will create sequence of sections to make it look like pie chart.

graphics.FillPie(brush1, graphics.FillPie(brush2, graphics.FillPie(brush3, graphics.FillPie(brush4, graphics.FillPie(brush5,

rect, rect, rect, rect, rect,

0, deg1); deg1, deg2); deg1 + deg2, deg3); deg1 + deg2 + deg3, deg4); deg1 + deg2 + deg3 + deg4, deg5);

New Microsoft Chart Controls for Web and Windows Forms Applications NotifyIconChart Disk Size Explorer

In the above lines first line will create the first section with red color and its start point will be 0 and it will create an arc of calculated degrees. After that our section (arc) should start from where it left so the next line we have written deg1 at start angle and in third line we have written deg1+deg2 so that our next arc will start from where our previous arc has left. This particular code is not optimized one as you can see we have created five different brush and five different float variables as these things could been done using loops. I intentionally didn't do it till this point because it may complicate matters for some. Below is the optimized code doing the above task using loops and arrays.
Collapse | Copy Code

How to Rotate a Pie Chart in SSRS 2005 ASP.NET MVC Chart Control A Simple Pie Chart Control Chart and Pie for data with hole 2D Animated Charts Making SVG Charts with Common Objects A control to display pie charts with highly customizable formatting WPF Toolkit Charting Controls (Line, Bar, Area, Pie, Column Series) Demo Internal Supply Chain, Visibility via 200 plus 3D Chart Reports Part II QC DATA PULLER using C# 3D Pie Chart Web Control Cristi Potlog's Chart Control for .NET Fusion Charts Helper Class for ASP.NET 2.0 (C#)

private void createPie() { iarrPieValues = new int[lstValuesCount]; for (int iCnt = 0; iCnt < lstValuesCount; iCnt++) { iarrPieValues[iCnt] = Int32.Parse(lstValues.Items[iCnt].ToString()); sum += iarrPieValues[iCnt]; } Color[] color = { Color.Red, Color.Blue, Color.Maroon, Color.Yellow, Color.Green, Color.Indigo }; Rectangle rect = new Rectangle(30, 10, 130, 130); Graphics graphics = pictureBox1.CreateGraphics(); graphics.Clear(pictureBox1.BackColor); float fDegValue = 0.0f; float fDegSum = 0.0f; for (int iCnt = 0; iCnt < iarrPieValues.Length; iCnt++) { fDegValue = (iarrPieValues[iCnt] / sum) * 360; Brush brush = new SolidBrush(color[iCnt]); graphics.FillPie(brush, rect, fDegSum, fDegValue); fDegSum += fDegValue; } }

2 of 4

25/05/2013 01:17

Create Pie Chart Using Graphics in C# .NET - CodeProject

http://www.codeproject.com/Articles/463284/Create-Pie-Chart-Using-...

First I have created an array of int with range as the count of values in listbox. After that I have loop to calculate sum of all values. Second I have created an array of colors here I have restricted user to not create a pie chart with more than 6 values as more values means pie chart will not look like one. Thirdly we have used loop to create the pie chart conversion of numbers to degrees is done inside the loop fDegValue is created to calculate the degrees. In our previous example we have sumed the previous values as provided it as the third parameter here we have done it with a slight difference that fDegSum holds the sum values. This is just a small article to create a pie chart using the Graphics class. For those looking for creating charts professionally may use the chart controls provided by .NET and chart controls are part of .NET 4.0 and people using previous versions can download the controls from Microsoft's website.

Suggestion
Have any suggestion then do inform me at "hari19113@rediffmail.com". Thank you.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


hari19113
India Member

No Biography provided

Article Top

Like

10

Tweet

Rate this:

Poor

Excellent

Vote

Comments and Discussions


Add a Comment or Question
Profile popups Spacing Relaxed Search this forum Go

Noise Medium

Layout Open All

Per page 25

Update

First Prev Next

My vote of 3

VadimAlk

24 Sep '12 - 23:07

3 of 4

25/05/2013 01:17

Create Pie Chart Using Graphics in C# .NET - CodeProject

http://www.codeproject.com/Articles/463284/Create-Pie-Chart-Using-...

Does not contain anything new or anything you cannot find from MSDN within 5 minutes.
Reply Email View Thread Permalink Bookmark

Re: My vote of 3
still people refer codeproject than msdn.
Reply Email View Thread Permalink Bookmark

hari19113

25 Sep '12 - 6:34

My vote of 5
Simple and easy to understand.
Reply Email View Thread Permalink Bookmark

Azim Zahir

21 Sep '12 - 21:03

Re: My vote of 5
Thank you.
Reply Email View Thread Permalink Bookmark

hari19113

21 Sep '12 - 21:45

Last Visit: 23 May '13 - 6:50

Last Update: 23 May '13 - 19:42

Refresh

General

News

Suggestion

Question

Bug

Answer

Joke

Rant

Admin

Permalink | Advertise | Privacy | Mobile Web01 | 2.6.130523.1 | Last Updated 21 Sep 2012

Layout: fixed | fluid

Article Copyright 2012 by hari19113 Everything else Copyright CodeProject, 1999-2013 Terms of Use

4 of 4

25/05/2013 01:17

Вам также может понравиться