FileDocCategorySizeDatePackage
Chart.javaAPI DocExample2429Sun Dec 27 15:20:56 GMT 1998com.darwinsys.charts

Chart

public class Chart extends Component
Simple charting bean. This version just draws a Pie Chart. It doesn't even label the pie slices; that is left as a (non-trivial) exercise for the reader. Please read the 1987 Technical Report "How Hard can it be to draw a Pie Chart?" by D. L. Souvaine and Chris van Wyck, Rutgers Computer Science, LCSR-TR-90, before you decide how easy the work is going to be!

Fields Summary
protected String
title
The title to print on the chart
protected ChartData[]
data
the data to draw
protected ChartData[]
demoData
The data to draw a Demo
public static final int
CIRCLE
degrees in a circle
protected Color[]
colors
a set of colors to draw the pies in
Constructors Summary
public Chart(String s)
Construct a Chart with a title


	       
	   
		title = s;
		setBackground(Color.white);
	
public Chart()
Construct a Chart with no title (no-arg constructor required for Beans).

	 	this("DarwinSys ChartBean");
	
Methods Summary
public voiddoDemo()

		setData(demoData);
	
public java.lang.StringgetLabel()

		return title;
	
public java.awt.DimensiongetMinimumSize()

		return new Dimension(100, 120);
	
public java.awt.DimensiongetPreferredSize()

		return new Dimension(200, 240);
	
public voidpaint(java.awt.Graphics g)

		Dimension sz = getSize();
		int w = sz.width, h = sz.width;

		if (title != null)
			g.drawString(title, w/9, (int)(h*1.1));

		if (data == null || data.length == 0) {
			g.drawOval(0, 0, w, h);
			g.drawString("Please provide some data!", w/10, h/2);
			return;
		}

		int total = 0;
		int angle = 0;
		int rad = 0;	// "radians" (actually degrees) to draw
		int colNum = 0;

		for (int i=0; i<data.length; i++)
			total += data[i].value;
		for (int i=0; i<data.length; i++) {
			rad = (int)(CIRCLE * ((float)data[i].value / (float)total));
			// System.out.println("data: "+data[i].name+";"+data[i].value+
			//	",rad="+rad);
			g.setColor(colors[colNum++]);
			colNum%=colors.length;	// keep it in bounds
			g.fillArc(0, 0, w, h, angle, rad);
			angle += rad;
		}
	
public voidsetData(ChartData[] newStuff)

		data = newStuff;
		repaint();
	
public voidsetLabel(java.lang.String s)

		title = s;