C Program To Display 3D Bar Chart

A bar chart is used to display statistical data. The 3d bar chart is a type of bar chart in which each bar represents a value. The greater the value, the longer the 3d bar in the 3d bar chart. In this article, you will learn to write a program that will draw a 3d bar chart on the screen.

This program is written using Turbo C++ 3 compiler on a windows 7 64-bit computer.

Advertisements

Problem Definition

We want to write a program that will display a 3d bar chart on the screen. Each bar must have a value associated with it. The program to display 3d bar chart goes through the following steps.

Advertisements
  1. Declare all graphics and non-graphics variables.
  2. Initialize the variables.
  3. Initialize the graphics.
  4. Set the fill style for 3d bars.
  5. Set values for each 3d bar.
  6. Draw the 3d bar.
  7. Close the graph.

Program Code – 3D Bar Chart

/* Program to create a 3d bar-chart in C */

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

int main()
{
/* Variable declarations */

int gdriver, gmode,i,j;
int xs,xe, ys, ye, x, y;

/* Variable initialization */
gdriver = DETECT;

/* Graph initialization */
initgraph(&gdriver, &gmode, "d:\\turboc3\\bgi");

/* set the fill style */

setfillstyle(SOLID_FILL,LIGHTRED);

/* Draw the 3d bar chart */
x = 180;
y = 25;
outtextxy(x , y , "75000");
outtextxy(x + 50 , y + 50, "55000");
outtextxy(x + 100 , y + 100 , "45000");
outtextxy(x + 170, y + 170 , "30000");
outtextxy(x + 230, y + 230 , "20000");
j = 55;
xs = 100;xe = 150, ys = 10, ye = 320;
for(i = 0; i<5;i++)
{

xs = xs + j;
ys = ys + j;
xe = xe + j;

bar3d(xs, ys, xe, ye, (xe - xs)/4, 1);
}
getch();

/* close the graph */

closegraph();
return 0;
}

The following code section gives the values to each bar in the graph. The graphics text is displayed on the top of each 3d bar.

outtextxy(x , y , "75000");
outtextxy(x + 50 , y + 50, "55000");
outtextxy(x + 100 , y + 100 , "45000");
outtextxy(x + 170, y + 170 , "30000");
outtextxy(x + 230, y + 230 , "20000");

Output – 3D Bar Chart

Output – 3D Bar-chart
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version