|
10. Using Pie Chart
The Pie Chart is different from the other charts in terms of the input data that it receives. It accepts only a single dimensional array of data for plotting the graph. Each value is plotted as a slice of a complete circle.
Significant features of Pie Chart
A particular slice can be exploded and highlighted.
The values get automatically adjusted.
A 3-D view can be shown.
An example to Represent the Percentage of Revenues generated by different Products, is given below that illustrates plotting of sample data in the Pie chart. The plotting of data can be achieved in two ways:
The Input Data : The input data is the percentage of revenues generated by different products for a particular company (Maruthi Udyog Pvt. Ltd.). The table below shows the revenue generated by the different products of Maruti Udyog Pvt.Ltd., that are to be plotted.
|
Cars |
Zen |
800 |
Esteem |
Balino |
Alto |
Others |
|
% revenue |
27.40 |
15.74 |
8.25 |
13.08 |
3.94 |
31.61 |
Viewing the Output : The data representing the revenues generated by the different products are plotted in the below shown Pie chart.

Implementing a Scatter Chart by manual coding
The steps to write a code to plot the required chart are given below.
Import the required packages / classes.
import java.awt.*;
import javax.swing.*;
import com.adventnet.beans.charts.*;
Write a class extending Jpanel and declare a variable for the AdventNetPieChart.
AdventNetPieChart ch =null;
In the pie() method set a layout (say border layout) and instantiate the AdventNetPieChart.
setLayout(new BorderLayout());
ch = new AdventNetPieChart();
Set the number of arcs to be shown in the pie (say 6).
ch.setNumberOfArcs(6);
Use the setItemValues(double[]) method to set the input data.
ch.setItemValues(new double[] {27.4,15.74,8.25,13.08,3.94,31.61});
Use the setItemLabels(String[]) method to set the labels for the pie values.
ch.setItemLabels(new String[] {"Zen","800","Esteem","Balino","Alto","Others",});
To show the legend values, first set its visibility to true, then set the title value and then set the series labels.
ch.setLegendVisible(true);
ch.setLegendTitle("Cars List");
ch.setView3D("10,45,45");
Finally add the chart instance in the border layout.
The main method in the class must create a new instance of the chart and a frame to accommodate the chart. Add the chart to the frame, set the size of the frame and set it to be visible.
Compile and run the file to get the required chart. (For setting the classpath refer Using AdventNet Charts Programmatically section).
The complete code
to construct the Pie chart can be seen for better understanding of the
parameter settings. You can compile and run the code to see the above
screen.
Implementing a Scatter Chart using Bean Builder IDE
Start Bean Builder
To create a new screen: Select File menu --> New --> Screen. You will see a wizard. Click on the application icon and click finish. Thus the new screen is created.
To drop the Chart Bean: Click the AdventNetCharts.jar tab in the bean palette. Click and drop the AdventNetPieChartBean in the draw area.
To configure the chart properties: Double-click the chart to get the property sheet. Then set the value for the following properties as given below.
headerText: "Maruthi Udyog Pvt. Ltd.".
footerText: "Contribution of Revenue by different Cars for the Year 2001-2002". (You can change the font of these texts using the headerFont and footerFont properties.)
legendVisible: True (To show the legends for the chart)
legentAnchor: West (from the values in the combo box). The legend orientation in this case will be Vertical. You can set it to Horizontal when the orientation is set to north or south.
NumberOfArcs: 6 (It shows the total number of values to be shown in the chart).
LegendTitle: Cars List (The legend title can be changed)
itemValues[]: 27.4, 15.74, 8.25, 13.08, 3.94, 31.61 (Click on the edit button in the right of the combo box showing the values to get the editor. Enter your values in the editor.)
itemLable[]: Zen, 800, Esteem, Balino, Alto and Others (To set the car names.)
To set the 3-D view: Select the view3D property and give you values for Depth, Elevation and Rotation. (say 10, 45, and 45).
Close the property sheet and you will have a Pie Chart with the required values plotted.
Now compile and run the screen to see the output of your configured Pie Chart.
Thus the Pie chart can be plotted and the methods / properties in it can be used for much more customization. You can refer to the Javadocs to use the other methods in the Pie chart.
|