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!
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!
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.
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" />";
}