PieChart Label Distribution Post 1.0.6

A free public discussion forum for the JFreeChart class library.

PieChart Label Distribution Post 1.0.6

Postby svicheck » Thu May 22, 2008 7:28 pm

I realize there have been a number of changes/improvements to piechart label distribution since the 1.0.6 release, but I have been struggling to get 1.0.6 behavior in the post 1.0.6 releases.

The following three images best illustrate the problem:

1.0.6 behavior

Image

1.0.9 behavior

Image

1.0.10 behavior (from SVN source at revision 1009)

Image

Specifically, I noticed that the post 1.0.6 releases all seem to reserve a larger area on the left and right edges of the plot area which causes most longer labels to wrap. I have played with every inset, margin, padding option I can find for the chart/labels and I cannot seem to force the labels to use the extra whitespace that exists on the edges.

The following is the simple source file that creates the pie chart shown in the images. Does anyone have any suggestions to correct the problem or is this just a bug in the new label distribution?

Code: Select all
package com.chart;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class PieTest {

   public static void main(String[] args) throws IOException {
      String fileName = args.length > 0 ? args[0] : "piechart.png";
      JFreeChart pieChart = createChart(createDataset());
      File chartFile = new File(fileName);
      ChartUtilities.saveChartAsPNG(chartFile, pieChart, 300, 200);
   }

   private static PieDataset createDataset() {
      DefaultPieDataset dataset = new DefaultPieDataset();

      dataset.setValue("Tribbey, Chester", new Double(160.0));
      dataset.setValue("Pittari, Cyril", new Double(122.0));
      dataset.setValue("Noffer, Jacky", new Double(118.0));
      dataset.setValue("Kochevar, Branden", new Double(76.0));
      dataset.setValue("Juhnke, Fred", new Double(48.0));
      dataset.setValue("Gardner, Jon", new Double(44.75));
      dataset.setValue("Engle, Sheldon", new Double(44.5));
      dataset.setValue("Bruend, Pierre", new Double(40.0));
      dataset.setValue("Belgrade, Del", new Double(33.0));
      dataset.setValue("Aughen, Quentin", new Double(8.0));
      dataset.setValue("Abington, Bill", new Double(8.0));

      return dataset;
  }

   private static JFreeChart createChart(PieDataset dataset) {

      JFreeChart chart = ChartFactory.createPieChart(
         "Pie Test",
         dataset,
         false,
         false,
         false);

      chart.setBorderVisible(true);
      chart.setBackgroundPaint(Color.WHITE);

      TextTitle title = chart.getTitle();
      title.setFont(new Font("SansSerif", Font.BOLD, 12));
      title.setPaint(Color.GRAY);

      PiePlot plot = (PiePlot)chart.getPlot();
      plot.setNoDataMessage("No data available");
      plot.setIgnoreZeroValues(true);
      plot.setCircular(false);
      plot.setOutlinePaint(null);
      plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
      plot.setLabelGap(0.02);
      plot.setLabelOutlinePaint(null);
      plot.setLabelShadowPaint(null);
      plot.setLabelBackgroundPaint(Color.WHITE);
      plot.setBackgroundPaint(Color.WHITE);

      return chart;
  }

}
svicheck
 
Posts: 19
Joined: Fri Nov 17, 2006 10:21 pm

Postby david.gilbert » Thu May 22, 2008 8:42 pm

Thankyou! At last someone has asked a question the *right* way:

- good subject title;
- version number(s) specified;
- sample images to describe the problem;
- a small self contained demo that reproduces the problem easily.

Clearly, I haven't got the pie chart labelling quite right yet. But here is some code to try:

Code: Select all
      plot.setInteriorGap(0.02);
      plot.setMaximumLabelWidth(0.20);
      plot.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);


The first line reduces the white space around the whole plot, allowing the labels to get closer to the edges before they wrap around. The default value for the interior gap is 0.08 (eight percent), so we're reducing it to two percent. [I'm actually thinking of changing this attribute to an instance of RectangleInsets, because it would be better if we could reduce the whitespace at the left and right of the plot, without necessarily reducing it at the top and bottom of the plot].

The second line increases the amount of space that can be allocated to the section labels before they get wrapped. The default value here is 0.14 (fourteen percent) and I've increased it to twenty percent. You can experiment with larger/smaller values.

The last line will only compile with the code from Subversion. It makes the linking lines curved rather than angular. This is optional, depending on whether or not you like the effect. I like the curves.

Here's the result (using JFreeChart code from Subversion):

Image

I think the chart still looks a little odd - it's maybe too tall (calling setCircular(true) or perhaps plot.setInsets() to add a little more whitespace at the top might improve it). Or perhaps a larger font for the title?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11324
Joined: Fri Mar 14, 2003 10:29 am

Postby RichardWest » Thu May 22, 2008 8:55 pm

david.gilbert wrote:Thankyou! At last someone has asked a question the *right* way:

- good subject title;
- version number(s) specified;
- sample images to describe the problem;
- a small self contained demo that reproduces the problem easily.


I agree, thank you svicheck. David, we should make a sticky-thread that details what we desire in a forum post (using this thread as an example). We should also include a list of what not to do as well as reasons why some post get answered faster than others if at all. Some of my recent posts highlight some of the reasons.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA
RichardWest
 
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Postby david.gilbert » Thu May 22, 2008 9:55 pm

Richard,

Good idea! I wrote up this - it might need editing, but it's a start:

http://www.jfree.org/phpBB2/viewtopic.php?t=24715

I'll take a look through your recent posts for more ideas.

Regards,

Dave
david.gilbert
JFreeChart Project Leader
 
Posts: 11324
Joined: Fri Mar 14, 2003 10:29 am

PieChart.drawLabels 1.0.6 vs 1.0.10 labelWidth

Postby svicheck » Fri May 23, 2008 5:24 pm

Ahhh..... thanks for the praise on my original question :oops:


I tried the changes you suggested: interiorGap=0.02, maxLabelWidth=0.20, circular=true and they greatly improved the appearance. However, I have a more fundamental question about label size in 1.0.6 vs 1.0.10.

Obviously there have been a number of changes to the calculations, but it seems that 1.0.10 allocates less space to the label width than 1.0.6. Using the previous example code, I traced execution through the PieChart.drawLabels() method and in 1.0.6 this method calculates maxLabelWidth=58.0, while the equivalent in 1.0.10 results in labelWidth=36.6. That is a significant reduction in size. I am not sure why this is happening, but might that explain why the labels are more prone to wrap in 1.0.10 than in 1.0.6?

Is this intentional behavior or just a side-effect of some other change?
svicheck
 
Posts: 19
Joined: Fri Nov 17, 2006 10:21 pm

Overlapping Labels

Postby kate.fox@anchorintelligen » Thu Jun 12, 2008 10:24 pm

I guess I don't have to go through the effort of putting together a well detailed email. I have a similar problem that is displayed on at least some of the chart images.

When there are a few narrow slices the lines overlap so it's hard to tell which label corresponds to which slice. This problem is even worse on the simple labels that are on the chart itself, the labels there are one on top of the other.

I will try the circular line style, but it doesn't look as pretty.
kate.fox@anchorintelligen
 
Posts: 2
Joined: Thu Jun 12, 2008 10:18 pm

Postby kate.fox@anchorintelligen » Fri Jun 13, 2008 6:55 pm

Here is one of the pie charts that I generated:

//img524.imageshack.us/my.php?image=pieuv3.png

With simple labels:
//img134.imageshack.us/my.php?image=pie2ul3.png
kate.fox@anchorintelligen
 
Posts: 2
Joined: Thu Jun 12, 2008 10:18 pm

Re: PieChart Label Distribution Post 1.0.6

Postby adityakanitkar » Thu Jan 20, 2011 11:40 am

Hi kate.fox@anchorintelligen,

I saw those images in the link....
In second pie chart, How did you put labels on the slices instead of beside them...

Thanks,
Aditya
adityakanitkar
 
Posts: 4
Joined: Thu Jan 20, 2011 11:15 am

Re: PieChart Label Distribution Post 1.0.6

Postby Robert Webber » Wed Oct 23, 2013 1:06 pm

I agree that the 1.0.6 releases seem to reserve a larger area on the left and right edges of the plot. All of the longer labels are wrapped because of this feature. I have absolutely no idea on how to resolve this. The above code is not working for me.

______________________________
www.microsoftwindowssupport.com
Robert Webber
 
Posts: 1
Joined: Wed Oct 23, 2013 6:13 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests