views:

109

answers:

2

How to generate URL's for labels for Pie Charts using JFree Chart package.We can extend the PieSectionLabelGenerator but i would need examples to show how. Please advice!

Thanks in Advance!

A: 

Just invoke setLabelGenerator() on your PiePlot. The MessageFormat ArgumentIndex values correspond to the series name, value and percentage. You can reference them in your label generator, as shown below:

PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}"));

Addendum:

I am looking for a URL/Hyperlink.

Add a ChartMouseListener to your ChartPanel; you can obtain the link from the ChartEntity of the corresponding ChartMouseEvent. You can use java.awt.Desktop to open the URL in a browser.

trashgod
I would imagine that would give me a String Label. I am looking for a URL/Hyperlink.
gpmattoo
@gpmattoo: Just set the URL to the same value as the label.
trashgod
If you can share the code snippet of the ChartMouseListener thing you said above, that would help. Note that I am using JDK 1.4.
gpmattoo
@gpmattoo: There's a `ChartMouseListener` example here, http://stackoverflow.com/questions/3309045. It looks like `java.awt.Desktop` requires Java SE 6.
trashgod
A: 

Note this answer is targeted for those making urls and maps for charts used in web pages

For Making the Pie Segments Themselves URLs by using an HTML Map: I would advise that you actually extend the StandardPieURLGenerator. Then you only need to do two things:

Add The Data

Either through constructor arguments or setters, make a way to add the data into fields within your class.

Override generateURL

generateURL will be called when the JFreeChart is wanting the generator to make a URL. If you are wanting to add parameters then I would do something like this:

public String generateURL(PieDataset dataset, Comparable key, int pieIndex)
{
  return super.generateURL(dataset, key, pieIndex) + "&" + yourParameters;
}

To Add URLs in the Label

Extend the StandardPieSectionLabelGenerator and override generateAttributedSectionLabel instead for the same steps above. Your function will now look more like this:

public String generateAttributedSectionLabel(PieDataset dataset, Comparable key)
{
  return super.generateAttributedSectionLabel(dataset, key) + "<a href="YOUR_URL_HERE" />";
}
Adam
I would imagine that would be for URL generator for PIE Charts. However, what i am looking for is URL for Pie Labels i.e. custom class implementing PieSectionLabelGenerator interface http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/labels/PieSectionLabelGenerator.html or something of that sort.
gpmattoo
@gpmatto Ahh sorry, misread. Let me just write up a new answer for that one.
Adam