James Gralton Guest
|
Posted: Tue Jan 27, 2004 6:07 pm Post subject: Pie Chart Help |
|
|
I have the following code in the appropiate classes in order to display a
Pie Chart. However when the chart should be displayed the window opens but
the chart isn't displayed. There and no exceptions thrown though.
Can anyone help me here please. Maybe I am missing something obvious.
private void jbInit() throws Exception {
this.setTitle("Matching Statistics");
setResizable(true);
statBoxPanel.setLayout(borderLayout1);
/////
Color bgcolor = Color.white;
PieItem[] itemArray = new PieItem[4]; // allocate storage
itemArray[0] = new PieItem(this.totalNew, "New", Color.red); // parse
argument
itemArray[1] = new PieItem(this.totalMatch, "Full Match", Color.blue);
// parse argument
itemArray[2] = new PieItem(this.totalStrongMatch, "Strong Match",
Color.green); // parse argument
itemArray[3] = new PieItem(this.totalWeakMatch, "Weak Match",
Color.yellow); // parse argument
int h = this.size().height;
int w = this.size().width;
the_pie = new PieView(h-50, itemArray, bgcolor); // shd be min(h-50, w)?
this.getContentPane().setLayout(new BorderLayout(0, 0));
this.setBackground(Color.white);
this.getContentPane().add("Center", the_pie);
tvClassLogo = new ImageIcon(GUI.class.getResource("TVClass400.jpg"));
}
package gui;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.lang.Math;
//The view of the pie.
public class PieView extends Canvas {
PieItem[] wedges; // The data for the pie
double total = 0.0; // Total of all
wedges
Color wedgeColor[] = new Color[7];
Color textColor = Color.black;
Color otherColor = Color.gray;
Color shadowColor = Color.lightGray;
Color bgColor = Color.white;
int pieViewSize; // size of square to incise pie into
static final int pieBorderWidth = 10; // pixels from circle edge to side
int pieDiameter; // derived from the view size
int pieRadius; // ..
int pieCenterPos; // ..
int minLabeledWedge = 3; // Min size of a wedge for labeling,
degrees.
int shadowOffset = 3; // offset of shadow in pixels
public PieView(int asize, PieItem[] avec, Color bc) { // constructor
this.pieViewSize = asize; // copy args
this.wedges = avec;
this.bgColor = bc;
pieDiameter = pieViewSize - 2*pieBorderWidth;
pieRadius = pieDiameter/2;
pieCenterPos = pieBorderWidth + pieRadius;
this.setFont(new Font("Helvetica", Font.BOLD, 12));
this.setBackground(bgColor);
//this.setBackground(new Color(255, 255, 183));
for (int i = 0; i < wedges.length; i++) {
total += wedges[i].frac;
}
wedgeColor[0] = Color.green; // colors that black looks good
on
wedgeColor[1] = Color.yellow;
wedgeColor[2] = Color.red;
wedgeColor[3] = Color.orange;
wedgeColor[4] = Color.cyan;
wedgeColor[5] = Color.pink;
wedgeColor[6] = Color.magenta;
} // constructor
public void paint(Graphics g) {
int startDeg = 0;
int arcDeg;
int arcDegCount = 0;
int x, y;
double angleRad;
g.setColor(shadowColor); // shadow, down and to the right 3
pixels
g.fillOval(pieBorderWidth+shadowOffset,
pieBorderWidth+shadowOffset, pieDiameter, pieDiameter);
g.setColor(otherColor); // "other" is gray
g.fillOval(pieBorderWidth, pieBorderWidth, pieDiameter,
pieDiameter);
int wci = 0;
int i;
for (i = 0; i
arcDeg = (int)Math.round(((this.wedges[i].frac / total)
* 360));
arcDegCount += arcDeg;
if ((i+1)==this.wedges.length & arcDegCount != 360) {
arcDeg += (360 - arcDegCount); // Avoid a
gray wedge due to roundoff.
}
if (this.wedges[i].color != null) {
g.setColor(this.wedges[i].color);
} else {
g.setColor(wedgeColor[wci++]);
}
g.fillArc(pieBorderWidth, pieBorderWidth, pieDiameter,
pieDiameter, startDeg, arcDeg);
if (wci >= wedgeColor.length) {
wci = 1; // Rotate colors. Don't reuse 0 in
case first and last are same.
}
startDeg += arcDeg;
} // draw wedges
startDeg = 0; // Do labels so they go on top of the wedges.
for (i = 0; i<this.wedges.length; i++) {
arcDeg = (int)Math.round(((this.wedges[i].frac / total)
* 360));
if (arcDeg > minLabeledWedge) { // Don't label small
wedges.
g.setColor(textColor);
angleRad = (float) (startDeg+
(arcDeg/2))*java.lang.Math.PI / 180.0;
x = pieCenterPos +
(int)((pieRadius/1.3)*java.lang.Math.cos(angleRad));
y = pieCenterPos -
(int)((pieRadius/1.3)*java.lang.Math.sin(angleRad))
+ 5; // 5 is about half the height of
the text
g.drawString(this.wedges[i].label, x, y);
} // don't label small wedges
startDeg += arcDeg;
} // for
} // paint()
public Dimension preferredSize () {
return new Dimension (pieViewSize, pieViewSize);
} // preferredSize
} // PieView
package gui;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.lang.Math;
// ================================================================
// A struct class to hold data for one wedge
public class PieItem {
public double frac; // each one has a
number
public String label; // and a label
public Color color; // and an optional
color
public PieItem (double d, String s, Color c) { // constructor
frac = d;
label = s;
color = c;
} // constructor
} // PieItem
|
|