Create a pie chart using graphics.h in C

In this article, we are going to learn about the pieslice() and setfillstyle() functions of graphics.h header file in C programming language and use them to create a pie chart.
Submitted by Manu Jemini, on March 20, 2018

Creating a pie chart in C with graphics library can be used when showing a result which is connected to each other. To have a chart in your program, the first thing to do is to initialize a graph.

Second thing is, initialize max-X and max-Y of the chart. This can be done by calling the grtmaxx() and getmaxy() functions and divide them by 2. The values should be stored for the future use.

The third step is to set a style of the slice of the chart. Now to create the chart you can easily choose a styling for the slice by calling the function setfillstyle(style, color), where style defines the style and color define the color of the pie slice.

Fourth and the last step is to make the pieslice(int x, int y, int stangle, int endangle, int radius) with six parameters as which should numbers. These numbers will define the geometry of the slice.

These functions need graphic.h file to be included in the program before using any of these functions.

graphics.h - pieslice() and other functions Example in C

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

int main() 
{
	//initilizing graphic driver and 
	//graphic mode variable
	int graphicdriver=DETECT,graphicmode;

	//calling initgraph function with 
	//certain parameters
	initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

	//Printing message for user
	outtextxy(10, 10 + 10, "Program to draw a pie chart in C graphics");

	//initilizing max x and y
	int  mx = getmaxx()/2;
	int  my = getmaxy()/2;

	//setting fill style in  pie slices
	setfillstyle(WIDE_DOT_FILL,YELLOW);
	pieslice(mx, my, 0, 75, 100);


	setfillstyle(WIDE_DOT_FILL,BROWN);
	pieslice(mx, my, 75, 225, 100);


	setfillstyle(WIDE_DOT_FILL,BLUE);
	pieslice(mx, my, 225, 360, 100);

	return 0;
}

Output

graphics.h - create pie chart example in C




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.