Jump to content
We've recently updated our Privacy Statement, available here ×

matthew.harrison

Members
  • Posts

    4
  • Joined

  • Last visited

Community Answers

  1. matthew.harrison's post in Ordering Series Legend in stacked bar chart was marked as the answer   
    I ended up finding an answer for this in the end.  If its of any use to people in the future, I created a chart customizer, which allowed me to define a property to define the legend labels:
    import java.util.Iterator;import org.jfree.chart.JFreeChart;import org.jfree.chart.LegendItem;import org.jfree.chart.LegendItemCollection;import org.jfree.chart.plot.CategoryPlot;import net.sf.jasperreports.engine.JRAbstractChartCustomizer;import net.sf.jasperreports.engine.JRChart;import net.sf.jasperreports.engine.JRPropertiesMap;/** * Provides customisation of the chart display that are not possible with JasperReport properties. */public class ChartCustomiser extends JRAbstractChartCustomizer { @Override public void customize(final JFreeChart chart, final JRChart jasperChart) { final String[] legendLabels = findLegendLabelProperty(jasperChart); if (legendLabels.length > 0 && chart.getPlot() instanceof CategoryPlot) { reorderLegendLabels(legendLabels, chart); } } private void reorderLegendLabels(final String[] legendLabels, final JFreeChart chart) { final LegendItemCollection newLegendItemCollection = new LegendItemCollection(); for (final String label : legendLabels) { @SuppressWarnings("unchecked") final Iterator existingLegendItemIterator = chart.getLegend().getSources()[0].getLegendItems().iterator(); while (existingLegendItemIterator.hasNext()) { final LegendItem item = existingLegendItemIterator.next(); if (item.getDescription().equals(label)) { newLegendItemCollection.add(item); break; } } } if (newLegendItemCollection.getItemCount() > 0) { ((CategoryPlot) chart.getPlot()).setFixedLegendItems(newLegendItemCollection); } } private String[] findLegendLabelProperty(final JRChart jasperChart) { final JRPropertiesMap pm = jasperChart.getPropertiesMap(); final String legend = pm.getProperty("LegendLabels"); if (legend != null && !legend.isEmpty()) { return legend.split("\|"); } return new String[0]; }}[/code]This does mean if any items are missed from the LegendLabels property they don't get displayed, but it works for me.Hope this may be of use to someone if they hit the same issue, cheers,Matt
×
×
  • Create New...