Author Topic: Turbo C Graphics Program for Bar Chart  (Read 2068 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Turbo C Graphics Program for Bar Chart
« on: February 10, 2012, 12:28:39 pm »
Here is the Turbo C Graphics program for bar chart.



Code:
//Source Code Listing

#include <stdio.h>
#include <string.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <math.h>

typedef struct _PieInfo
{
    char desc[128];
    double percentage;
    int color;
} BarInfo;

void Fill_BarInfo(BarInfo *p, const char *des, double percent, int clr)
{
    strcpy(p->desc, des);
    p->percentage = percent;
    p->color = clr;
}

extern BarInfo theBarInfo[32];
extern int theSliceCount;

BarInfo theBarInfo[32];
int theSliceCount = 0;


void main()
{
    const int SPACE_BAR = 40;
    const int BAR_WIDTH = 20;
    const double MAX_PERCENTAGE = 50.0;
    int grd, grm;
    int xmax, ymax;
    int left, top, right, bottom;
    double percent = 0;
    double x, y;
    int i,j,k;
    double xpos,ypos;
    char buf[128];


    detectgraph(&grd,&grm);
    initgraph(&grd, &grm, "");

    setbkcolor(BLUE);

    setcolor(WHITE);
    left = 40;
    top = 40;
    right =  getmaxy() - 20;
    bottom = getmaxy() - 60;

    xmax = right - left;
    ymax = bottom - top;

    rectangle(left, top, right, bottom);
    for(j = 0; j <= MAX_PERCENTAGE; j += 10)
    {
	ypos = j / MAX_PERCENTAGE * ymax;
	setlinestyle(DASHED_LINE, 1, 1);
	line(left, bottom - ypos, right, bottom - ypos);
	sprintf(buf, "%d%%", j);
	outtextxy(left - 30, bottom - ypos - 5, buf);
    }
    setlinestyle(SOLID_LINE, 1, 1);

    Fill_BarInfo(&theBarInfo[0], "Gold", 20, RED);
    Fill_BarInfo(&theBarInfo[1], "Stocks", 15, GREEN);
    Fill_BarInfo(&theBarInfo[2], "Bonds", 35, CYAN);
    Fill_BarInfo(&theBarInfo[3], "ETFs", 15, MAGENTA);
    Fill_BarInfo(&theBarInfo[4], "Oprions", 7.5, YELLOW);
    Fill_BarInfo(&theBarInfo[5], "Cash", 7.5, LIGHTRED);
    theSliceCount = 6;

    xpos = left + SPACE_BAR;
    for(i = 0; i < theSliceCount; i++)
    {
	percent = theBarInfo[i].percentage;
	if(percent > MAX_PERCENTAGE)
	    percent = MAX_PERCENTAGE;
	setfillstyle(SOLID_FILL, theBarInfo[i].color);
	ypos = percent / MAX_PERCENTAGE * ymax;
	bar(xpos, bottom, xpos + BAR_WIDTH, bottom - ypos);
	rectangle(xpos, bottom, xpos + BAR_WIDTH, bottom - ypos);

	sprintf(buf, "%3.0lf%%", theBarInfo[i].percentage);
	outtextxy(xpos - 5, bottom - ypos - 15, buf);

	sprintf(buf, "%s", theBarInfo[i].desc);
	outtextxy(xpos - 5, bottom + 15, buf);
	xpos += (SPACE_BAR + BAR_WIDTH);
    }

    xpos = right + SPACE_BAR;
    for(k = 0; k < theSliceCount; k++)
    {
	rectangle(xpos, ypos, xpos + 30, ypos + 25);
	setfillstyle(SOLID_FILL,theBarInfo[k].color);
	floodfill((xpos + xpos + 30) / 2, (ypos + ypos + 25) / 2, WHITE);
	sprintf(buf, "%3.0lf%% %s", theBarInfo[k].percentage, theBarInfo[k].desc);
	outtextxy(xpos + 35, ypos + 10, buf);
	ypos += 35;
    }


    getch();
    closegraph();
}


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1