Java Program to Display a Pie Chart using Frame

This is a Java Program to Display a Pie Chart Using a Frame

Problem Description

We have to write a program in Java such that it displays a pie chart for a given data

Expected Input and Output

For displaying a pie chart, we can have the following set of input and output.

To View the Pie Chart :

When the program is executed,
it is expected that the frame contains a pie chart.
Problem Solution

1. Create an array of data type int to store the value of each data.
2. Create an array of data type Color to store the desired color in the pie chart for the respective data value.
3. Get the sum of all data values.
4. For each data value, calculate the percentage share in the pie chart.
5. Draw an arc for each data value.
6. Fill the arc with the respective color.

advertisement
Program/Source Code

Here is source code of the Java Program to create a pie chart. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to Create a Pie Chart using a Frame */
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Pie_Chart extends Applet
  5. {
  6.     int[] data_values;
  7.     Color[] data_clr;
  8.     int total;
  9.     //Function to create a data set
  10.     public void init()
  11.     {
  12. 	setBackground(Color.white);
  13. 	//Create a data set to represent in pie-chart
  14. 	data_values=new int[]{10,25,12,15,15,18,5};
  15. 	data_clr=new Color[]{Color.red,Color.blue,Color.green,Color.yellow,
  16.                              Color.orange,Color.black,Color.white};
  17.     }
  18.     //Function to get the sum of all data values
  19.     public void start()
  20.     {
  21. 	int n = data_values.length;
  22. 	int i;
  23. 	total=0;
  24. 	for(i=0;i<n;i++)
  25. 	{
  26. 	    total+=data_values[i];
  27. 	}
  28.     }
  29.     //Function to draw the pie chart
  30.     public void paint(Graphics g)
  31.     {
  32. 	int i;
  33. 	int start_angle = 0;
  34. 	for(i=0;i<data_values.length;i++)
  35. 	{
  36. 	    int arc_angle = (int)(data_values[i]*360 / total);
  37. 	    g.drawArc(100,100,300,300,start_angle,arc_angle);
  38. 	    g.setColor(data_clr[i]);
  39. 	    g.fillArc(100,100,300,300,start_angle,arc_angle);
  40. 	    start_angle+=arc_angle;
  41. 	}
  42.     }
  43. }
  44. /*
  45. <applet code = Pie_Chart.class width=500 height=500>
  46. </applet>
  47. */

To compile an execute the program use the following commands :

🎓 Register Today for Free C++ Certification - December 2025
>>>javac My_Cursor.java
>>>appletviewer My_Cursor.java
Program Explanation

1. Create arrays to store the data values and color for the respective data.
2. Get the sum of all data values.
3. Get the share of each data in the pie chart usin and draw an arc from the last position of arc to the calculated position.
4. Change the color of arc using setColor method.
5. Fill the arc using fillArc method.

Runtime Test Cases

Here’s the run time test cases for creating a pie chart.

advertisement

Test case – To View the Pie Chart:
java-program-pie-chart

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.