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:
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.
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