Saturday, November 17, 2007

Simple Pie Chart Labels in JFreeChart 1.0.7

In the OTN article Visualize Your Oracle Database Data with JFreeChart, we showed multiple examples using JFreeChart to generate pie charts. However, in all the pie charts in this article and its accompanying source code, the labels for the pie chart sections were displayed outside of the actual pie and had lines connecting the labels to the appropriate pie section.

Recently released JFreeChart 1.0.7 makes it easy to place these pie section labels directly on the applicable pie slices. The following code snippet is taken from Listing 18 in the OTN article and the one additional line necessary to use "simple" labels rather than "extended" labels for a JFreeChart pie chart is highlighted:

/**
* Create 3D pie chart displaying the number of employees at each
* commission level. This method demonstrates use of DatasetReader to
* translate data from XML format to JFreeChart data format.
*
* @return 3D Pie Chart showing number of employees at each commission
* level.
*/
public JFreeChart createCommissionsPercentagePerLevelPieChart()
{
PieDataset pieData = null;

final String xmlData = databaseAccess.getCommissionPercentageBreakdownAsXml();

final DatasetReader reader = new DatasetReader();
try
{
pieData = reader.readPieDatasetFromXML(
new ByteArrayInputStream(xmlData.getBytes()) );
}
catch (IOException ioEx)
{
ioEx.printStackTrace();
}

JFreeChart chart =
ChartFactory.createPieChart3D( "Commissioned Employees at Each Commission Level",
pieData,
false, true, false );

// Chart specifically provides getCategoryPlot() and getXYPlot() methods,
// but not a getPiePlot() or getPiePlot3D() method.
final PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setDepthFactor(0.35); // pie depth 35% of plot height
plot.setCircular(false);
plot.setForegroundAlpha(0.5f); // Declare explicitly as float (50%)

plot.setLabelGenerator(new HrCommissionsPieGenerator());

plot.setSimpleLabels(true);

return chart;
}


In the above example, a PiePlot3D was already being used, so only one line of new code was needed to move the pie section labels onto the sections themselves. This line was plot.setSimpleLabels(true).

There are a couple interesting side notes to make here. First, this also works with PiePlot even though PiePlot3D was used in the example above. Second, passing false to the setSimpleLabels method is the equivalent of not calling this method at all. In other words, not using simple labels (using "extended" labels) is the default. This makes sense because this was the case for JFreeChart 1.0.5 and JFreeChart 1.0.6 (the versions we wrote our article against) and the behavior remains the default.

I also like the default being "extended" pie section labels rather than "simple" pie section labels because the labels start appearing on top of each other fairly quickly when there are numerous or very small pie sections. The following image (click on it to see larger version) shows what the chart shown in the article's Figure 15 looks like with "simple" labels rather than the extended labels used (by default) in the article.



The simple labels look pretty good here because of the relatively small number of pie sections and the relatively large size of each section that provides sufficient room to place the labels within the section. However, even in this example, a few of the labels do overlap each other.

No comments: