How to create bar chart in computer graphics (16-03-2020)


Creating a bar chart in C with graphics.h library is very decent to show the result in a graphical form. To have a graph in your program first thing to do is to initialize a graph.

Second thing is, initialize X axis and Y axis of the graph. This can be done by calling the line function twice with the co-ordinate of the endpoints of both the axis.
The third step is to create bars. Now to create bars, you can easily choose a styling for the bar by calling the function setfillstyle(LINE_FILL, RED) before making the bar.
Fourth and the last step is to make the bar with four parameters which should be numbered. These numbers will be the co-ordinates of the bars.
These functions need the graphic.h file to be included in the program before using any of these functions.

graphics.h - line(), bar() and setfillstyle() functions 


#include <graphics.h>
#include <conio.h>
int main()
{
   int gd = DETECT, gm;
// detect graphic mode and initialize graphic display
initgraph(&gd, &gm, "..//BGI");
//initialize graph by checking graphic display, graphic mode and loading graphic driver from disk
setcolor(YELLOW);
// set color of the outer rectangle
rectangle(0,30,639,450);
//Outer rectangle or outer box
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
// Upper heading font style, font direction, and font size
setcolor(WHITE);
//font color
outtextxy(275,0,"Bar Chart");
//outtextxy is used to display text at specific position
 setlinestyle(SOLID_LINE,0,1);
//y-axis line style, pattern and thickness
line(100,420,100,60);
// line co-ordinates x1,y1,x2,y2
line(100,420,600,420); 
// x-axis line
line(90,70,100,60);
//y-axis arrow left side
line(110,70,100,60);
// y-axis arrow-right-side
line(590,410,600,420);
//x-axis arrow-left-side
 line(590,430,600,420);
//x-axis arrow right side
outtextxy(95,35,"Y");  
// write Y for  y-axis
   outtextxy(610,405,"X");
//write X for x-axis
   outtextxy(85,415,"O");     
// write O for origin
setfillstyle(LINE_FILL,BLUE);
// first bar filling style and color
bar(150,100,200,419);
// bar (left,top,right, bottom)
setfillstyle(XHATCH_FILL,RED); 
//second bar filling sytle and color
bar(225,150,275,419);        
//bar (left,top,right, bottom)
setfillstyle(WIDE_DOT_FILL,GREEN);
// third bar filling style and color
bar(300,200,350,419);           
//bar (left,top,right,bottom)
setfillstyle(INTERLEAVE_FILL,MAGENTA);
//forth barfilling style and color
bar(375,125,425,419);      
//bar (left,top,right,bottom)
setfillstyle(HATCH_FILL,BROWN); 
//fifth bar filling sytle and color
   bar(450,175,500,419);          
// bar (left, top, right, bottom)
   getch();
   return 0;
}


Comments