C Program to Draw Pie Chart Using C Graphics

Here is a C graphics program to draw pie chart using graphics.h header file. In this program, we will draw a pie chart on screen having centre at mid of the screen and radius of 120 pixels. We will use outtextxy and pieslice functions of graphics.h header file. Below is the detailed descriptions of graphics functions used in this program.

Function Description
initgraph It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode.
getmaxx It returns the maximum X coordinate in current graphics mode and driver.
getmaxy It returns the maximum Y coordinate in current graphics mode and driver.
outtextxy It displays a string at a particular point (x,y) on screen.
pieslice It draws only a sector of circle having radius r and centre at (x, y), it takes two additional arguments start-angle and end-angle. It also fills the slice with pattern and color set using setfillstyle function.
setfillstyle It sets the current fill pattern and fill color.
closegraph It unloads the graphics drivers and sets the screen back to text mode.
void pieslice(int xCenter, int yCenter, int startAngle, int endAngle, int radius);

C program to draw pie chart using graphics

In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). Then we will draw multiple pie slices having same center coordinates and radius but varying the start angle and end angle. Before drawing a pie slice we change the fill color using setfillstyle function.

#include<graphics.h>

int main() {
   int gd = DETECT, gm, x, y;
   initgraph(&gd, &gm, "C:\\TC\\BGI");

   settextstyle(BOLD_FONT,HORIZ_DIR,2);
   outtextxy(220,10,"PIE CHART");
   /* Setting cordinate of center of circle */
   x = getmaxx()/2;
   y = getmaxy()/2;

   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
   setfillstyle(SOLID_FILL, RED);
   pieslice(x, y, 0, 60, 120);
   outtextxy(x + 140, y - 70, "FOOD");

   setfillstyle(SOLID_FILL, YELLOW);
   pieslice(x, y, 60, 160, 120);
   outtextxy(x - 30, y - 170, "RENT");

   setfillstyle(SOLID_FILL, GREEN);
   pieslice(x, y, 160, 220, 120);
   outtextxy(x - 250, y, "ELECTRICITY");

   setfillstyle(SOLID_FILL, BROWN);
   pieslice(x, y, 220, 360, 120);
   outtextxy(x, y + 150, "SAVINGS");

   closegraph();
   return 0;
}
Program Output C graphics program to draw pie chart

Conclusion

In conclusion, this tutorial has guided you through the creation of a dynamic and visually appealing pie chart using Turbo C graphics. By exploring fundamental graphics functions, you've gained insights into setting up the Turbo C environment, drawing sectors to construct a pie chart, and enhancing the visual presentation with colors and labels. The addition of dynamic user input adds a layer of interactivity, allowing the program to adapt to varying data values, making it a versatile tool for data representation.

Graphics programming offers a unique blend of mathematical precision and creative expression, and Turbo C provides an accessible platform for mastering these concepts. The knowledge gained from this tutorial extends beyond drawing a pie chart; it forms a foundation for understanding graphics principles that are applicable in diverse programming scenarios.

As you embark on your graphics programming journey, consider extending this project further. Experiment with different chart styles, explore animation techniques, or integrate real-time data updates for a more immersive experience. Turbo C graphics, though rooted in the past, continues to be a valuable canvas for learning and experimenting with graphical concepts.

Remember, the essence of graphics programming lies in the synergy of accuracy and artistic interpretation. Turbo C has been a reliable companion for countless programmers, fostering creativity and sparking curiosity. Embrace the skills acquired in this tutorial, let your imagination run wild, and continue to explore the boundless possibilities that graphics programming unfolds. Whether you are illustrating data, creating visual simulations, or designing user interfaces, Turbo C graphics provides a nostalgic yet robust foundation for your creative programming endeavors.


Related Topics
C program to draw 3D bar graph on screen
C Program to draw bar graph on screen
C program to draw sine wave graph on screen
C Program to draw cosine(cos) wave graph on screen
C graphics program to draw digital clock
C Program to draw concentric circles screen
C graphics program for moving car animation
List of all C Graphics Programs