Share on Google+Share on Google+

Pie Chart
1 Answer(s)      8 years ago
Posted in : Java Beginners


View Answers

November 13, 2008 at 5:34 AM

Hi friend,

I am going to post complete running code.

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;

public class ChartPanel extends JPanel {
private double[] values;
private String[] names;
private String title;

public ChartPanel(double[] v, String[] n, String t) {
names = n;
values = v;
title = t;

public void paintComponent(Graphics g) {
if (values == null || values.length == 0)
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
System.out.println("i>>>>>>>>>" + i);
if(minValue > values[i])
minValue = values[i];
if(maxValue < values[i])
maxValue = values[i];

Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;
System.out.println("d>>>>>>>>>>>>>>>>" + d);
System.out.println("clientWidth>>>>>>" + clientWidth);
System.out.println("clientHeight>>>>>" + clientHeight);
System.out.println("barWidth>>>>>>>>>" + barWidth);

Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.drawString(title, x, y);

int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();

for (int i = 0; i < values.length; i++) {
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else {
valueY += (int) (maxValue * scale);
height = -height;

g.fillRect(valueX, valueY, barWidth - 2, height);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth)/ 2;
g.drawString(names[i], x, y);

public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(400, 300);
double[] values = new double[6];
String[] names = new String[6];
values[0] = 1;
names[0] = "Item 1";

values[1] = 2;
names[1] = "Item 2";

values[2] = 4;
names[2] = "Item 3";

values[3] = 6;
names[3] = "Item 4";

values[4] = 8;
names[4] = "Item 5";

values[5] = 12;
names[5] = "Item 6";

f.getContentPane().add(new ChartPanel(values, names, "Simple Pie Chart"));
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {

Read for more information.


Related Tutorials/Questions & Answers:
pie chart
pie chart  how to create pie chart using struts2
Pie chart
Pie chart  hi................. In my project i want to create a pie-chart by taking values at run time. As want to display it in pie chart form....   Java Swing PIE Chart
pie chart
pie chart to display data from database and show it in piechart according to different data that is present in database..plzz help
Create pie chart
Create pie chart  hi............. In my project i want to create a pie chart to display the result. i require a very simple code in java swings were i can edit the code and put my values... so plz help me
3d pie chart - Java3D
3d pie chart  Dear Sir, i want to create 3d pie chart using jsp, i have some code for 2d pie chart in jsp and 3d pie chart in applet. for 2d pie... for creating 3d pie chart but its showing error. i used following code
Pie chart on jsp
Pie chart on jsp  how can i create a pie chart on a jsp page using... that creates a pie chart using JFreechart api and takes the value from..."); dataset.executeQuery(query); JFreeChart chart = ChartFactory.createPieChart
How to draw pie chart in J2ME
How to draw pie chart in J2ME  Hi, I'm developing a program using J2ME and I want to draw a pie chart but it seems J2ME language hasn't available chart component like JFreeChart in Java! could any one help me how can I draw a pie
Pie Chart
Pie Chart       In this section we are providing you an example to create a Pie Chart. Description of Example   For creating a Pie Chart we use PieDataset
Pie Chart - Java Beginners
Pie Chart  How to Draw a Pie chart using graphics in java  Hi friend, I am going to post complete running code. import java.awt.... ChartPanel(values, names, "Simple Pie Chart")); WindowListener wndCloser = new
Pie chart Jfree create error
Pie chart Jfree create error  hi........... thanks 4 ur help but when i am using the code which has a import file import org.jfree.chart.*; its giving me an error that " package org.jfree.chart does not exists " can u help me
3D Pie Chart
3D Pie Chart       In this section we are providing you an example to create a 3D Pie Chart. Description of Program :    For creating a 3D Pie Chart we use
Draw Pie Chart
Draw Pie Chart       This Java Pie Chart example is going to show you how to draw Pie Charts in Java. This Java Pie Chart example is drawing a pie chart to show
Create a Pie Chart in jsp Page using JFree Chart
Create a Pie Chart in jsp Page using JFree Chart  Hello Sir I followed ur tutorial on "CREATE A PIE CHART IN JSP PAGE USING JFREE CHART... the pie chart report and after that save it to the desired location.Kindly help me
PHP GD Pie Chart
, 200, 100, 0, 45, $darknavy, IMG_ARC_PIE); imagefilledarc($image, 150, $i, 200, 100, 45, 75 , $darkgray, IMG_ARC_PIE); imagefilledarc($image, 150, $i, 200, 100, 75, 360 , $darkred, IMG_ARC_PIE); } imagefilledarc($image, 150
pie chart flex and sum from hql request
pie chart flex and sum from hql request  hi, i work with flex and j2ee project.i have to make a function wich return list of value: print("public... to show the result in the pie shart but the probleme is i don't have a field
Create a pie chart in jsp page using JFreeChart
Create a pie chart in jsp page using JFreeChart... to create a pie chart in jsp page using JFreeChart. Code given below creates a simple pie chart for given values. In the code given below we have extended class
Create Pie Chart using database values
Create Pie Chart using database values Java provides JFreeChart library which... a pie chart by retrieving the values from the database. Here is the code...*; import; public class Chart { public static
Create multiple pie chart in single frame using JFreeChart
Create multiple pie chart in single frame using JFreeChart... you how to create a multiple pie charts in a single frame in jsp page using JFreeChart. Code given below creates a simple pie charts for given values
jfree chart
jfree chart  i need donut chart using jfree
bar chart
bar chart  how to create a bar chart from values of the database and the string value should be the entities of the database??plzz help..needed badly
Chart & Graphs Tutorials in Java
Chart Example In this section we are providing you an example to create a Pie Chart.    3D Pie Chart Example In this section we are providing you an example to create a 3D Pie Chart.   Area Chart Example
draw chart in web application
draw chart in web application  how to draw bar chat from the record store in database? i.e. draw the bar chart according to selected record
JFreechart Stacked Bar Chart
chart, pie chart, line chart,area chart etc. Here we are going to create a Stacked Bar Chart. This chart actually displays the result of multiple queries stacks...JFreechart Stacked Bar Chart JFreechart provides a way to create various
chart tutorial - Java3D
chart tutorial  Hi I need a JFree Chart class library in order to design a chart with java  Hi friend, For JFree Chart class library... information,Tutorials and Examples on JFree Chart visit to :
Bar chart implementation in PHP
Bar chart implementation in PHP  How to implements reports in bar graphs by using PHP
Clickable chart with Tool Tip
Clickable chart with Tool Tip  Can we generate the charts with tooltip and onclick events using jfreechart in jsps and servlets
surface chart - Java3D
surface chart  i have to make a surface chart of a piece of land with data given for each point in the xy plane....the chart should be such that the areas with different data range should show up in different colour. seriously
line chart from database in jsp
line chart from database in jsp  how can i create line chart from database in jsp code
Jfree chart problem - Swing AWT
Jfree chart problem  hello i have a problem related to jfree chart i have some data and i want to display data in two different chart stacked area chart and line chart both in a single frame. That is hybrid combination
JFree chart package error
JFree chart package error  hi, I had asked u abt the jfree chart package and done what u have said. Its working but its giving me error that some... the jcommon jar file and jfree jar file for the bar chart code. but now its giving
create chart - JSP-Servlet
.jrxml report. I want to create a chart using ireport software. I m using sql...(primary key) title status I want to show chart of field "date" only... and free hours is green are filled that chart So pls help me to solve
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code


Advertisement null