JMSL Chart Programmer's Guide
Charting 2D Types >> Pie Chart  Previous Page  Contents  Next Page

Pie Chart

A pie chart is a graphical way to organize data. This section describes the construction of a pie chart.

Example

The JFrameChart class is used to create a frame containing a Chart node. A Pie node is then created as a child of the Chart node. The Pie node constructor creates PieSlice nodes as its children. The number of PieSlice nodes created is y.length, here equal to 4.  After the PieSlice nodes are created they are retrieved from the object pie and customized by setting attributes.

The LabelType attribute is set in the pie node. The pie node itself does not use this attribute, but from there it is inherited by all of the PieSlice nodes.

The Title attribute is set in each PieSlice node. This is the slice label. It is used to label the slice only if the slice's LabelType attribute is LABEL_TYPE_TITLE.

The FillColor attribute is also set in each slice. This determines the color of the slice. Since the default value of FillColor is black, it is generally recommended that FillColor be set in each slice.

The FillOutlineColor attribute sets the border color of each slice. In this example it is set in the pie node to be blue and set in the slice[1] node to be yellow. All except slice[1] are outlined in blue, and slice[1] is outlined in yellow.

The Explode attribute moves a pie slice out from the center. Its default value is 0, which puts the slice vertex in the center. A value of 1.0 would put the slice vertex on the circumference.

(Download Code)
import com.imsl.chart.*;
import java.awt.Color;

public class SamplePieChart extends JFrameChart {
    
    public SamplePieChart() {
        Chart chart = getChart();
        
        double y[] = {35., 20., 30., 5.};
        Pie pie = new Pie(chart, y);
        pie.setLabelType(Pie.LABEL_TYPE_TITLE);
        pie.setFillOutlineColor(Color.blue);

        PieSlice[] slice = pie.getPieSlice();

        slice[0].setFillColor(Color.green);
        slice[0].setTitle("Green");
	 slice[0].setExplode(0.1);
        
        slice[1].setFillColor(Color.red);
        slice[1].setTitle("Red");
        slice[1].setFillOutlineColor(Color.yellow);
	 slice[1].setExplode(0.1);
        
        slice[2].setFillColor(Color.blue);
        slice[2].setTitle("Blue");
	 slice[2].setExplode(0.1);
        
        slice[3].setFillColor(Color.yellow);
        slice[3].setTitle("Yellow");
        slice[3].setExplode(0.15);
    }
    
    public static void main(String argv[]) {
        new SamplePieChart().setVisible(true);
    }
}



©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page