0

I am using JasperReports to generate a report with charts embedded and I am facing a problem with the legend. It is too close to the axis and it covers the text of the category as you can see in the picture:

enter image description here

I have no space enough in horizontal but when I introduce a line feed the legend hides the text.

I was searching about this issue but the other users asking about the same issue didn't get any answer.

If instead of relocating the legend, there is another way to push it down automatically it will be fine for me too.

This is my customizer:

    public class LineChartCustomizer extends JRAbstractChartCustomizer {

    @Override
    public void customize(JFreeChart jFreeChart, JRChart jrChart) {

        Locale.setDefault(Locale.TAIWAN);
        CategoryPlot plot = (CategoryPlot) jFreeChart.getPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        rangeAxis.setNumberFormatOverride(numberFormat);    
    }
}

EDIT:

I made some tests and I found the point is not really to relocate the legend but expand the space for the category labels.

enter image description here

EDIT:

I already solved myproblem, however I didn't relocate the legend. There is a parameter to increase the lines a category label can use.

    public class LineChartCustomizer extends JRAbstractChartCustomizer {

    @Override
    public void customize(JFreeChart jFreeChart, JRChart jrChart) {

        CategoryPlot plot = (CategoryPlot) jFreeChart.getPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        rangeAxis.setNumberFormatOverride(numberFormat);
        JRPropertiesMap pm = jrChart.getPropertiesMap();
        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setMaximumCategoryLabelLines(3);

    }
}

As usual, the final solution will be included in the sample code located in my GitHub repository: https://github.com/MichaelKnight/jaspertest.git

Michael Knight
  • 648
  • 2
  • 10
  • 26
  • Just move it the left,right, above?, http://stackoverflow.com/questions/35866274/how-to-position-legend-chart-labels/ if not you need a customizer... – Petter Friberg Mar 22 '16 at 11:24
  • I cannot change the position, that's why I am asking a way to push it down or relocate. As you said, I will need a customizer. Where can I find any example? Is it just adding any line to my existing customizer? – Michael Knight Mar 22 '16 at 11:26
  • I see you edit, why not set font size on labels or rotated them? – Petter Friberg Mar 22 '16 at 11:26
  • Even setting the font size to an smaller one, still not enough. The text will take 2 lines. Rotating is not an option for me in this case. So, I still need a way to move that legend box. – Michael Knight Mar 22 '16 at 11:28
  • Check out this code http://grepcode.com/file/repo1.maven.org/maven2/jfree/jfreechart-experimental/1.0.4/org/jfree/experimental/chart/annotations/XYTitleAnnotation.java – Petter Friberg Mar 22 '16 at 11:31
  • and this http://stackoverflow.com/questions/11320360/embed-the-legend-into-the-plot-area-of-jfreechart (note it is inside,but you should be able to move it around., search on XYTitleAnnotation – Petter Friberg Mar 22 '16 at 11:32
  • I will check that solution immediately. I will come back with the outcome. – Michael Knight Mar 22 '16 at 11:33
  • After some more tests and worthless efforts I confirm the issue is not about relocating the legend (or not only) but about expanding the space for the category label. – Michael Knight Mar 23 '16 at 10:57

0 Answers0