C Program to Draw Bar Graph Using C Graphics

We shall draw a bar graph on the screen with this application. In this case, we’ll utilize the graphics functions line, setfillstyle, and bar. To draw horizontal and vertical axes and bars on the screen, use the h header file. void line(int x1, int y1, int x2, int y2);

It creates a straight line from (x1, y1) to (x2, y2). void setfillstyle(int pattern, int colour);

It will set the current fill pattern and fill color. void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);

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

C Program to Draw Bar Graph Using C Graphics

1. Concepts

Here is the concept of C Programs we need to learn to write C Program to Draw Bar Graph Using C Graphics:

xTopLeft: X coordinate of top left corner.
yTopLeft: Y coordinate of top left corner.
xBottomRight: X coordinate of the bottom right corner.
yBottomRight: Y coordinate of the bottom right corner.

After learning these concepts, you’re good to go. Let’s start writing the C Program to Draw Bar Graph Using C Graphics.

2. C Program to Draw Bar Graph Using C Graphics

#include <graphics.h>
#include <conio.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);
   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");
   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);
 
   getch();
   closegraph();
   return 0;
}

3. Ouput

C Program to Draw Bar Graph Using C Graphics

4. Resources

Here are the files and other resources for you to learn and practice this program:

Similar Posts

Leave a Reply

Your email address will not be published.