Author Topic: Turbo C Graphics - Pie Chart Program  (Read 2164 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Turbo C Graphics - Pie Chart Program
« on: January 30, 2012, 01:23:16 am »
Here is the Turbo C program for drawing a pie chart.

Just fill in the values in PieInfo structure and pie chart will be shown.

The program output would be a pie chart showing the portfolio of Gold, Stocks, Bonds, Options, ETFs and Cash with different colors.

It uses the formula xend = x + r cos(angle) and yend = y + r sin(angle).

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;
} PieInfo;

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

extern PieInfo thePieInfo[32];
extern int theSliceCount;

PieInfo thePieInfo[32];
int theSliceCount = 0;


void main()
{
	int grd, grm;
	int margin, xmax, ymax;
	double xCenter, yCenter, radius;
	double percent = 0;
	double angle, x, y;
	static const double PI = 3.1415926535;
	int i,j,k;
	double xpos,ypos;
	char buf[128];


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

	setbkcolor(BLUE);

	margin = 40;
	xmax = getmaxx() - margin;
	ymax = getmaxy() - margin;
	if(xmax > ymax)
		xmax = ymax;
	xCenter = xmax / 2 + margin / 2;
	yCenter = ymax / 2 + margin / 2;
	radius = ymax / 2;

	setcolor(WHITE);
	rectangle(margin / 2,margin / 2,xmax + margin / 2,ymax + margin / 2);
	circle(xCenter, yCenter, radius);

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

	for(i = 0; i < theSliceCount; i++)
	{
		percent += thePieInfo[i].percentage;
		if(percent > 100)
			percent = 100;
		angle = percent / 100 * 360;
		x = radius * cos(angle * PI / 180);
		y = radius * sin(angle * PI / 180);
		line(xCenter, yCenter, xCenter + x, yCenter + y);
	}

	radius = radius / 2;
	percent = 0;
	for(j = 0; j < theSliceCount; j++)
	{
		percent += thePieInfo[j].percentage;
		if(percent > 100)
			percent = 100;
		angle = (percent - thePieInfo[j].percentage / 2) / 100 * 360;
		x = radius * cos(angle * PI / 180);
		y = radius * sin(angle * PI / 180);
		setfillstyle(SOLID_FILL,thePieInfo[j].color);
		floodfill(xCenter + x, yCenter + y, WHITE);
	}

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


	getch();
	closegraph();
}

output:




 


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