Monday, October 21, 2013

How To Draw a Pie Chart using System.Drawing

 Hi friends, recently I was trying to draw pie chart which can be send through mail option were to using charting API and save chart as image,the  other option was to draw pie chart.I decided explore possibility of drawing a pie chart using System.Drawing APIs as it is more fun & mind teaser.

Here is code:

 private void DrawPieChart()
        {
            string FileName = Server.MapPath("Images/" + "MyDrawing.jpeg");
            string ResultFileName = Server.MapPath("Images/" + "MyResultDrawing.jpeg");

            Bitmap bmp = new Bitmap(1024, 768);
            Graphics g = Graphics.FromImage(bmp);

            g.Clear(Color.White);
                      

            int PenWidth = 1;
            Pen RectPen = new Pen(Color.IndianRed, PenWidth);
           // Pen CirclePen = new Pen(Color.Wheat, PenWidth);

            //Some Properies to enhance image
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //form height & width
            int FormWidth = 1024;
            int FormHeight =768 ;
           


            //Required Sizes of outer Rectange containing pie chart
            int OrgRecWidth = 800;
            int OrgRectHeight = 600;

            //adjusted size of  outer Rectange containing pie chart due to border width
            int RecWidth = OrgRecWidth - PenWidth * 2;
            int RecHeight = OrgRectHeight - PenWidth * 2;

            //top diagonal point for rectange to be drawn
            int RectTopDigonalX = FormWidth / 2 - RecWidth / 2;
            int RectTopDigonalY = FormHeight / 2 - RecHeight / 2;

            Rectangle rec = new Rectangle(RectTopDigonalX,RectTopDigonalY,RecWidth,RecHeight);
            g.DrawRectangle(RectPen, rec);

            //center of pie
            int CenterCircleX = RectTopDigonalX + OrgRecWidth / 2 ;//809
            int CenterCircleY = RectTopDigonalY + OrgRectHeight / 2;//429

            //height & width Circle  ellipse in itself
            int EllipseWidth = 350;

            int CircleEnclosingRectX = CenterCircleX - EllipseWidth / 2;//559
            int CircleEnclosingRectY = CenterCircleY - EllipseWidth / 2;//189

            Rectangle rec1 = new Rectangle(CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth, EllipseWidth);
            //g.DrawRectangle(RectPen, rec1);


            Color DarkColorRed = (Color)System.Drawing.ColorTranslator.FromHtml("#900000");
            SolidBrush DarkbrushRed = new SolidBrush(DarkColorRed);


            Color DarkColorGray = (Color)System.Drawing.ColorTranslator.FromHtml("#909090");
            SolidBrush DarkbrushGray = new SolidBrush(DarkColorGray);

            Color DarkColorNavy = (Color)System.Drawing.ColorTranslator.FromHtml("#000050");
            SolidBrush DarkbrushNavy = new SolidBrush(DarkColorNavy);

            for (int j = 1; j < 20; j++)
            {
                CircleEnclosingRectY = (CenterCircleY - EllipseWidth / 2) - j;

                g.FillPie(DarkbrushRed, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth - PenWidth * 2, EllipseWidth - PenWidth * 2, 0, 180);
                g.FillPie(DarkbrushGray, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth - PenWidth * 2, EllipseWidth - PenWidth * 2, 180, 90);
                g.FillPie(DarkbrushNavy, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth - PenWidth * 2, EllipseWidth - PenWidth * 2, 270, 90);
            }


            Color LightColorRed = (Color)System.Drawing.ColorTranslator.FromHtml("#FF0000");
            SolidBrush LightbrushRed = new SolidBrush(LightColorRed);

            Color LightColorGray = (Color)System.Drawing.ColorTranslator.FromHtml("#C0C0C0");
            SolidBrush LightbrushGray = new SolidBrush(LightColorGray);

            Color LightColorNavy = (Color)System.Drawing.ColorTranslator.FromHtml("#000080");
            SolidBrush LightbrushNavy = new SolidBrush(LightColorNavy);

            CircleEnclosingRectX = CenterCircleX - EllipseWidth / 2;
            CircleEnclosingRectY = CenterCircleY - EllipseWidth / 2;

            SolidBrush WhiteBrush = new SolidBrush(Color.Black);

           
            g.FillPie(LightbrushRed, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth, EllipseWidth, 0, 180);
            g.FillPie(LightbrushGray, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth, EllipseWidth, 180, 90);
            g.FillPie(LightbrushNavy, CircleEnclosingRectX, CircleEnclosingRectY, EllipseWidth, EllipseWidth, 270, 90);

          


            double x1 = CenterCircleX + ((EllipseWidth / 2) * Math.Cos(180));
            double y1 = CenterCircleY + ((EllipseWidth / 2) * Math.Sin(180));
            g.DrawLine(RectPen, Convert.ToInt16(x1), Convert.ToInt16(y1), Convert.ToInt16(x1), Convert.ToInt16(y1) + 5);
            //g.DrawString("   Red", new Font(SystemFonts.DefaultFont, FontStyle.Regular), WhiteBrush,Convert.ToInt16(x1),Convert.ToInt16(y1));
            bmp.Save(ResultFileName);

            Image1.ImageUrl = FileName;
            g.Dispose();
            bmp.Dispose(); 

        }

Code above draws an image of pie into a jpeg file.This code can be used to draw pie chart with some modification.
 I am not taking into consideration the pixels width used  by drawing brushes & pens.

Resultant Image

No comments:

Post a Comment