import java.util.*;
import java.awt.*;
import java.applet.Applet;public class PieChart extends Applet {
int Width, Height;
String title;
Hashtable colors; public void init() {
double value; /* Get parameters */
String at = getParameter("width");
Width = (at != null) ? Integer.valueOf(at).intValue() : 100;
at = getParameter("height");
Height = (at != null) ? Integer.valueOf(at).intValue() : 100;
if ( Height < 80 ) {
Height = 80;
}
title = getParameter("title");
if ( title == null )
title = "";
at = getParameter("values"); setColors(); PieChartCanvas c = new PieChartCanvas( Math.min(Width,Height-60) );
setLayout(new BorderLayout()); PieLegend legend = new PieLegend( Math.min(Width,Height-60), 30 ); // Use a StringTokenizer to extract the slice values and colors
// They are comma separated value-color combinations
int j=1;
Color col;
for(StringTokenizer t = new StringTokenizer(at, ","); t.hasMoreTokens();) {
String str = t.nextToken();
int i = str.indexOf('-');
value = Double.valueOf(str.substring(0, i)).doubleValue();
col = getColor(str.substring(i + 1));
if ( value > 0 ) {
c.addSlice(value, col );
legend.addSlice( col, ""+j );
}
j++;
}
add("North", new Label( title, Label.CENTER ) );
add("South", legend );
add("Center", c);
} public void setColors() {
colors = new Hashtable();
colors.put("green", Color.green);
colors.put("red", Color.red);
colors.put("blue", Color.blue);
colors.put("yellow", Color.yellow);
colors.put("magenta", Color.magenta);
colors.put("cyan", Color.cyan);
colors.put("orange", Color.orange);
colors.put("pink", Color.pink);
colors.put("white", Color.white);
colors.put("black", Color.black);
} public Color getColor( String colorstr ) {
return (Color)colors.get( colorstr );
}
}class PieLegend extends Canvas {
int Width, Height, numSlices=0;
int MaxSlices = 20;
Color colors[] = new Color[20];
String labels[] = new String[20];
Font font;
FontMetrics fm; public PieLegend(int width, int height) {
font = new Font("Courier", Font.PLAIN, 12 );
fm = getFontMetrics(font); Width = width;
Height = height;
resize( Width, Height );
} public void addSlice( Color color, String text ) {
colors[numSlices] = color;
labels[numSlices] = text;
numSlices++;
} public void paint(Graphics g) {
int blocksize, strwid, strht, left, i; blocksize = Width/numSlices;
strht = fm.getAscent()+1;
for (i=0, left=0; i<numSlices; i++, left+=blocksize ) {
g.setColor( colors[i] );
g.fillRect( left+2, strht+1, blocksize-2, Height-strht-2 );
g.setColor( Color.black );
g.drawLine( left+blocksize, strht+1, left+blocksize, Height );
strwid = fm.stringWidth(labels[i]);
g.drawString(labels[i],left+(blocksize-strwid)/2,strht);
}
}
}class PieChartCanvas extends Canvas {
int Width, numSlices = 0;
double total = 0;
double value[] = new double[20];
Color color[] = new Color[20]; public PieChartCanvas(int width) {
Width = width;
resize(minimumSize().width, minimumSize().height);
} public void paint(Graphics g) {
int startAngle;
double angle;
Dimension d = size();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height); startAngle = -45;
for(int i = 0; i < numSlices; i++) {
g.setColor(color[i]);
angle = Math.round(360 * (value[i] / total));
g.fillArc(0, 0, Width, Width, startAngle, (int)angle);
startAngle += angle;
}
} public void addSlice(double value, Color color) {
this.value[numSlices] = value;
this.color[numSlices++] = color;
total += value;
} public Dimension preferredSize() {
return minimumSize();
} public Dimension minimumSize() {
int maxwidth;
return new Dimension( Width, Width );
}
}
import java.awt.*;
import java.applet.Applet;public class PieChart extends Applet {
int Width, Height;
String title;
Hashtable colors; public void init() {
double value; /* Get parameters */
String at = getParameter("width");
Width = (at != null) ? Integer.valueOf(at).intValue() : 100;
at = getParameter("height");
Height = (at != null) ? Integer.valueOf(at).intValue() : 100;
if ( Height < 80 ) {
Height = 80;
}
title = getParameter("title");
if ( title == null )
title = "";
at = getParameter("values"); setColors(); PieChartCanvas c = new PieChartCanvas( Math.min(Width,Height-60) );
setLayout(new BorderLayout()); PieLegend legend = new PieLegend( Math.min(Width,Height-60), 30 ); // Use a StringTokenizer to extract the slice values and colors
// They are comma separated value-color combinations
int j=1;
Color col;
for(StringTokenizer t = new StringTokenizer(at, ","); t.hasMoreTokens();) {
String str = t.nextToken();
int i = str.indexOf('-');
value = Double.valueOf(str.substring(0, i)).doubleValue();
col = getColor(str.substring(i + 1));
if ( value > 0 ) {
c.addSlice(value, col );
legend.addSlice( col, ""+j );
}
j++;
}
add("North", new Label( title, Label.CENTER ) );
add("South", legend );
add("Center", c);
} public void setColors() {
colors = new Hashtable();
colors.put("green", Color.green);
colors.put("red", Color.red);
colors.put("blue", Color.blue);
colors.put("yellow", Color.yellow);
colors.put("magenta", Color.magenta);
colors.put("cyan", Color.cyan);
colors.put("orange", Color.orange);
colors.put("pink", Color.pink);
colors.put("white", Color.white);
colors.put("black", Color.black);
} public Color getColor( String colorstr ) {
return (Color)colors.get( colorstr );
}
}class PieLegend extends Canvas {
int Width, Height, numSlices=0;
int MaxSlices = 20;
Color colors[] = new Color[20];
String labels[] = new String[20];
Font font;
FontMetrics fm; public PieLegend(int width, int height) {
font = new Font("Courier", Font.PLAIN, 12 );
fm = getFontMetrics(font); Width = width;
Height = height;
resize( Width, Height );
} public void addSlice( Color color, String text ) {
colors[numSlices] = color;
labels[numSlices] = text;
numSlices++;
} public void paint(Graphics g) {
int blocksize, strwid, strht, left, i; blocksize = Width/numSlices;
strht = fm.getAscent()+1;
for (i=0, left=0; i<numSlices; i++, left+=blocksize ) {
g.setColor( colors[i] );
g.fillRect( left+2, strht+1, blocksize-2, Height-strht-2 );
g.setColor( Color.black );
g.drawLine( left+blocksize, strht+1, left+blocksize, Height );
strwid = fm.stringWidth(labels[i]);
g.drawString(labels[i],left+(blocksize-strwid)/2,strht);
}
}
}class PieChartCanvas extends Canvas {
int Width, numSlices = 0;
double total = 0;
double value[] = new double[20];
Color color[] = new Color[20]; public PieChartCanvas(int width) {
Width = width;
resize(minimumSize().width, minimumSize().height);
} public void paint(Graphics g) {
int startAngle;
double angle;
Dimension d = size();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height); startAngle = -45;
for(int i = 0; i < numSlices; i++) {
g.setColor(color[i]);
angle = Math.round(360 * (value[i] / total));
g.fillArc(0, 0, Width, Width, startAngle, (int)angle);
startAngle += angle;
}
} public void addSlice(double value, Color color) {
this.value[numSlices] = value;
this.color[numSlices++] = color;
total += value;
} public Dimension preferredSize() {
return minimumSize();
} public Dimension minimumSize() {
int maxwidth;
return new Dimension( Width, Width );
}
}
<APPLET code="PieChart.class" width=200 height=250 align=center >
<param name=values value="20-red,100-green,40-magenta,50-yellow">
<param name=title value="Pie Chart">
</APPLET>