C Program to Draw Bar Graph Using C Graphics

Here is a C graphics program to draw bar chart on screen using graphics.h header file. In this program, we will draw a bar graph on screen. Here, we will use line, setfillstyle and bar functions of graphics.h header file to draw horizontal and vertical axis and bars on screen.

void line(int x1, int y1, int x2, int y2);

It draws a line from (x1, y1) to (x2, y2).

void setfillstyle(int pattern, int color);

It sets the current fill pattern and fill color.

void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);

bar function draws a rectangle and fill it with current fill pattern and color.

Function Argument Description
xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.

C program to draw bar graph using graphics

#include <graphics.h>

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

   settextstyle(BOLD_FONT,HORIZ_DIR,2);
   outtextxy(275,0,"BAR GRAPH");

   setlinestyle(SOLID_LINE,0,2);
   /* Draw X and Y Axis */
   line(90,410,90,50);
   line(90,410,590,410);
   line(85,60,90,50);
   line(95,60,90,50);
   line(585,405,590,410);
   line(585,415,590,410);

   outtextxy(65,60,"Y");
   outtextxy(570,420,"X");
   outtextxy(70,415,"O");
   /* Draw bars on screen */
   setfillstyle(XHATCH_FILL, RED);
   bar(150,80,200,410);
   bar(225,100,275,410);
   bar(300,120,350,410);
   bar(375,170,425,410);
   bar(450,135,500,410);

   closegraph();
   return 0;
}
Output C graphics program to draw bar graph

Conclusion

Congratulations! You've successfully created a C program to draw a dynamic bar graph using Turbo C graphics. This tutorial covered the basics of setting up the Turbo C environment, drawing bars, adding colors, and dynamically adjusting the graph based on user input. Graphics programming introduces a visual dimension to data representation, and Turbo C provides an accessible platform for mastering these concepts.

As you continue your exploration into graphics programming, consider expanding this project by incorporating more categories, experimenting with different bar styles, or even adding interactivity. Turbo C graphics, though nostalgic, imparts valuable insights into graphics principles that remain relevant in contemporary programming environments.

Remember, the beauty of graphics programming lies in the seamless fusion of precision and artistic expression. Turbo C has been a reliable companion for countless programmers, fostering creativity and sparking curiosity. Embrace the skills gained in this tutorial, let your creativity flow freely, and explore the boundless possibilities that graphics programming unfolds. Whether you're sculpting data, designing informative interfaces, or crafting interactive applications, Turbo C graphics provides a timeless foundation for your creative programming endeavors.


Related Topics
C program to draw 3D bar graph
C Program to draw a rectangle and bar
C Program to draw digital counter on screen
C program for bouncing ball animation
C graphics program to draw digital clock
C program to draw pie chart using graphics
C graphics program for moving car animation
List of all C Graphics Programs