Piyush Mattoo
Piyush Mattoo

Reputation: 16143

Generating URL's for Pie Chart's Section Label using JFree Chart API

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!

Upvotes: 1

Views: 3033

Answers (3)

Piyush Mattoo
Piyush Mattoo

Reputation: 16143

static class CustomLegendGenerator
        implements PieSectionLabelGenerator {

    public String generateSectionLabel(final PieDataset dataset, final Comparable key) {
        String temp = null;
        if (dataset != null) {
            temp = key.toString();
            if (key.toString().equalsIgnoreCase("abc")) {
                temp = temp + " (abc String)";
            }
            if (key.toString().equalsIgnoreCase("xyz")) {
                temp = temp + " (xyz description)";
            }
            if (key.toString().equalsIgnoreCase("klm")) {
                temp = temp + " (Klm description)";
            }
        }
        return temp;
    }

    public AttributedString generateAttributedSectionLabel(PieDataset pd, Comparable cmprbl) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

Upvotes: 0

Adam
Adam

Reputation: 3795

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

Upvotes: 0

trashgod
trashgod

Reputation: 205885

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.

Upvotes: 1

Related Questions