Home » C programs » graphics.h header file functions

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

	//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
	pieslice(mx, my, 0, 75, 100);

	pieslice(mx, my, 75, 225, 100);

	pieslice(mx, my, 225, 360, 100);

	return 0;


graphics.h - create pie chart example in C



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.