Jump to content
Changes to the Jaspersoft community edition download ×

Display percentage in pie chart


teodord

Recommended Posts

Hi,

 

There were several posts on this forum about how to display the percentage value corresponding to each slice in a pie chart.

 

You need to use a chart customizer and associate it with your chart element in order to control the content of the labels by setting a label generator.

The label expression inside the chart dataset cannot be used because there is no way to calculate percentages there.

So this can be achieved only by setting a specially configured label generator to the JFreeChart object like seen in the chart customizer implementation below:

 

Code:

import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;

public class PieChartCustomizer implements JRChartCustomizer
{

public void customize(JFreeChart jFreeChart, JRChart jrChart)
{
PiePlot piePlot = (PiePlot) jFreeChart.getPlot();
StandardPieSectionLabelGenerator labelGenerator
= new StandardPieSectionLabelGenerator("{0} {1} {2}"«»);
piePlot.setLabelGenerator(labelGenerator);
piePlot.setLegendLabelGenerator(labelGenerator);
}

}

 

Note that the place holders in the standard label generator have special meanings and {2} is for the percentage. They are all explained here:

http://www.jfree.org/jfreechart/api/gjdoc/org/jfree/chart/labels/StandardPieSectionLabelGenerator.html

 

Put the chart customizer class in the classpath of your application and associate it with your chart by setting customizerClass="PieChartCustomizer" on your chart element.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

  • 2 months later...
  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi Teodord,

 

Your solution works fine. I try to use the label expression inside the dataset, to avoid to change the custumizer every time I went to change the label format. With a little change in your code, it can be done.

 

Code:
  public void customize(JFreeChart jFreeChart, JRChart jrChart) {


JRFillPieDataset dataset = (JRFillPieDataset) jrChart.getDataset();

if (dataset.getLabelExpression() != null) {
String label = dataset.getLabelExpression().getText();
label = label.substring(1, label.length() - 1);
PiePlot piePlot = (PiePlot) jFreeChart.getPlot();
StandardPieSectionLabelGenerator labelGenerator =
new StandardPieSectionLabelGenerator(label);
piePlot.setLabelGenerator(labelGenerator);
piePlot.setLegendLabelGenerator(labelGenerator);
}

}

 

I hope this can help!

 

Luiz Augusto Ruiz

Link to comment
Share on other sites

  • 6 years later...

Hi Teodor,

I tried to use the Customizer as specified in this post but it isn't working for me. Here's the code I tried:

 

public class PieChartCustomizer implements JRChartCustomizer

{

@Override

public void customize(JFreeChart jFreeChart, JRChart jrChart)

{

PiePlot piePlot = (PiePlot) jFreeChart.getPlot();

StandardPieSectionLabelGenerator labelGenerator

= new StandardPieSectionLabelGenerator("{0} {2}");

piePlot.setLabelGenerator(labelGenerator);

piePlot.setLegendLabelGenerator(labelGenerator);

}

}

 

Then implementing the class like this:

 

JRDesignChart jrPieChart = new JRDesignChart(jasperDesign,net.sf.jasperreports.engine.JRChart.CHART_TYPE_PIE);

...

jrPieChart.setCustomizerClass("PieChartCustomizer");

...

 

As soon as that last line to setCustomizerClass runs that's the last I see of the method.

 

What am I doing wrong?

 

Thanks in advance for your help.

 

Link to comment
Share on other sites

Hi Teodor,
I tried to use the Customizer as specified in this post but it isn't working for me. Here's the code I tried:

public class PieChartCustomizer implements JRChartCustomizer
{
@Override
public void customize(JFreeChart jFreeChart, JRChart jrChart)
{
PiePlot piePlot = (PiePlot) jFreeChart.getPlot();
StandardPieSectionLabelGenerator labelGenerator
= new StandardPieSectionLabelGenerator("{0} {2}");
piePlot.setLabelGenerator(labelGenerator);
piePlot.setLegendLabelGenerator(labelGenerator);
}
}

Then implementing the class like this:

JRDesignChart jrPieChart = new JRDesignChart(jasperDesign,net.sf.jasperreports.engine.JRChart.CHART_TYPE_PIE);
...
jrPieChart.setCustomizerClass("PieChartCustomizer");
...

As soon as that last line to setCustomizerClass runs that's the last I see of the method.

What am I doing wrong?

Thanks in advance for your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...