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
public JFreeChart createCommissionsPercentagePerLevelPieChart()
PieDataset pieData = null;
final String xmlData = databaseAccess.getCommissionPercentageBreakdownAsXml();
final DatasetReader reader = new DatasetReader();
pieData = reader.readPieDatasetFromXML(
new ByteArrayInputStream(xmlData.getBytes()) );
catch (IOException ioEx)
JFreeChart chart =
ChartFactory.createPieChart3D( "Commissioned Employees at Each Commission Level",
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.setForegroundAlpha(0.5f); // Declare explicitly as float (50%)
In the above example, a
PiePlot3Dwas 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
There are a couple interesting side notes to make here. First, this also works with
PiePlot3Dwas used in the example above. Second, passing
setSimpleLabelsmethod 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.