.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Draw a Pie Chart in C#

Posted By:Ghanashyam Nayak       Posted Date: June 04, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

In this article we will see how we can draw a pie chart of our given value.

In this article we will see how we can draw a pie chart of our given value. 

I provided a description as a comment for each line of code below which will help you to draw a pie chart. 

Main Code:
using System; 
using System.Collections.Generic; 
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace WindowsFormsApplication1
    public partial class Form2 : Form
        Form frm1=new Form1();
        public Form2()
        private void button1_Click(object sender, EventArgs e)
     //Call Main Function Which Will Help To Draw a Pie Chartââ,¬Â¦
  public void DrawPieChartOnForm()
     //Take Total Five Values & Draw Chart Of These Values.
           int[] myPiePercent = { 10, 20, 25, 5, 40 };
           //Take Colors To Display Pie In That Colors Of Taken Five Values.
           Color[] myPieColors = {Color.Red,Color.Black,Color.Blue,Color.Green,Color.Maroon};
           using (Graphics myPieGraphic = this.CreateGraphics())
                //Give Location Which Will Display Chart At That Location.
                Point myPieLocation = new Point(10, 10);
                //Set Here Size Of The Chartââ,¬Â¦
                Size myPieSize = new Size(150, 150);
                //Call Function Which Will Draw Pie of Values.
                DrawPieChart(myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize);
  // Draws a pie chart.
  public void DrawPieChart(int[] myPiePerecents, Color[] myPieColors, Graphics myPieGraphic, Point
myPieLocation, Size myPieSize)
           //Check if sections add up to 100.
           int sum = 0;
           foreach (int percent_loopVariable in myPiePerecents)
                sum += percent_loopVariable;
           if (sum != 100)
                MessageBox.Show("Sum Do Not Add Up To 100.");
           //Check Here Number Of Values & Colors Are Same Or Not.They Must Be Same.
           if (myPiePerecents.Length != myPieColors.Length)
                MessageBox.Show("There Must Be The Same Number Of Percents And Colors.");
           int PiePercentTotal = 0;
           for (int PiePercents = 0; PiePercents


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend