Example: Pie Chart
A simple Pie chart is constructed in this example. Pie slice labels and colors are set and one pie slice is exploded from the center. This class extends JFrameChart
, which manages the window.
import com.imsl.chart.*;
import java.awt.Color;
import java.applet.Applet;
public class PieEx1 extends javax.swing.JApplet {
private JPanelChart panel;
public void init() {
Chart chart = new Chart(this);
panel = new JPanelChart(chart);
getContentPane().add(panel, java.awt.BorderLayout.CENTER);
setup(chart);
}
static private void setup(Chart chart) {
// Create an instance of a Pie Chart
double y[] = {10., 20., 30., 40.};
Pie pie = new Pie(chart, y);
// Set the Pie Chart Title
chart.getChartTitle().setTitle("A Simple Pie Chart");
// Set the colors of the Pie Slices
PieSlice[] slice = pie.getPieSlice();
slice[0].setFillColor(Color.red);
slice[1].setFillColor(Color.blue);
slice[2].setFillColor(Color.black);
slice[3].setFillColor(Color.yellow);
// Set the Pie Slice Labels
pie.setLabelType(pie.LABEL_TYPE_TITLE);
slice[0].setTitle("Fish");
slice[1].setTitle("Pork");
slice[2].setTitle("Poultry");
slice[3].setTitle("Beef");
// Explode a Pie Slice
slice[0].setExplode(0.2);
}
public static void main(String argv[]) {
JFrameChart frame = new JFrameChart();
PieEx1.setup(frame.getChart());
frame.show();
}
}
Output
Link to Java source.