Sunday, September 22, 2013

Program to display pie chart accepting user input

/* This program displays a pie chart. This Program slices the pie as per the input given by the user. Make sure the total percentage do not exceed 100 % */
/* The program was compiled using Turbo C++ ver 3.0 */

Input :    Number of slices and % of each slice.

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
   int gd = DETECT, gm, midx, midy,i,dx,dy,value[20],n,degree=0;
   long start,end;

   char str[5];
   clrscr();
   printf("Enter the no of slices");
   scanf("%d",&n);

   initgraph(&gd, &gm, "..\\bgi");

   setcolor(MAGENTA);
   rectangle(0,40,639,450);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   setcolor(WHITE);
   outtextxy(275,10,"Pie Chart");

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   start = 0;
   end=0;
   value[0]=0;
   printf("Enter the percentage");
   for( i=1;i<=n;i++)
  {
   scanf("%d",&value[i]);
  degree= degree +((long)value[i] * 360)/100;
  if ( degree >360)
  {
    printf("Exceded 100 % ..Program terminating.. press any key to continue");
   getch();
   exit(0);
}
   for(i=0;i<n;i++)
   {

  end = start +(long)(value[i+1]*360)/100;
   setfillstyle(i+1,i+1);
   pieslice(midx, midy, start,end, 100);
   itoa(value[i+1],str,10);
 
   dx= midx+100*cos(((double)(start+end)/2)*(3.14f/180));
   dy= midy+100*sin((-1*(double)(start+end)/2)*(3.14f/180));
  outtextxy(dx, dy , str);
   start= start+((long)value[i+1]*360)/100;
  }
  getch();
closegraph();
   return 0;
}

7 comments:

  1. if this programme helped you somehow pls pls comment

    ReplyDelete
  2. This was a good program, though the offset part was a little confusing.
    Thank you for your help.

    ReplyDelete
    Replies
    1. the offset is not required. i tried to position the values using it but not required forgot to remove it . sorry . thanks for noting it.

      Delete
  3. Thanks sir.. this program helped me in understanding fundamentals of C graphics. My last attempt to draw a pie chart was a failure.

    ReplyDelete
  4. an error occur during run. resolve it. after one slice print it, exit.

    ReplyDelete