C and C++ Computer Graphics

C graphics program to draw bar chart

A pie chart is a circular statistical graphic divided into slices to illustrate numerical proportions. In C programming, using the graphics.h library, you can create visually appealing pie charts for data representation. Drawing a pie chart in C involves plotting arcs and filling them with colours to represent different segments.

Program Explanation

The graphics.h library provides the pieslice() function, which draws a filled pie-shaped sector between two given angles and a specified radius. Each slice corresponds to a category’s data value, making it an effective way to represent percentages or ratios.

#include <graphics.h>
#include <conio.h>

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

setcolor(WHITE);
pieslice(200, 200, 0, 90, 100); // First slice (90 degrees)
setfillstyle(SOLID_FILL, RED);
floodfill(200, 150, WHITE);

pieslice(200, 200, 90, 180, 100); // Second slice
setfillstyle(SOLID_FILL, GREEN);
floodfill(150, 200, WHITE);

pieslice(200, 200, 180, 360, 100); // Third slice
setfillstyle(SOLID_FILL, BLUE);
floodfill(250, 250, WHITE);

getch();
closegraph();
return 0;
}

Steps in the Program

  1. Initialize graphics mode using initgraph().

  2. Draw pie slices with pieslice() specifying:

    • Center coordinates (x, y)

    • Starting angle

    • Ending angle

    • Radius

  3. Set fill styles using setfillstyle() for different colours.

  4. Fill slices with floodfill() for better visualization.

  5. Close graphics mode after displaying the chart.

Advantages of Drawing Pie Charts in C

  • Enhances data visualization for console-based programs.

  • Useful in academic projects to demonstrate graphics concepts.

  • Helps in learning coordinate geometry and angles in programming.

Leave a comment

Your email address will not be published. Required fields are marked *