I have some graphs that I display in an report as images. I would like to align them but depending of the content of my legend, graphs have a different position. The more bigger my legend is, the more on the left my graph is, as you can see on the picture. And with a really huge legend, the graph is even smaller !
![Image](https://www.zupimages.net/up/20/51/8lxi.png)
I would like to align my graphs, that means if I put one below the other, the left part of the graphs starts on the same line.
As the problem comes from the size of my legend, is there any way to set a default width to a LegendTitle ? and if the content is bigger than this default width, it will be displayed with break lines.
Code: Select all
private static JFreeChart createChart(DefaultPieDataset pieDataset, String title, Locale locale) {
JFreeChart chart = ChartFactory.createRingChart(
title,
pieDataset,
true,
false,
locale
);
RingPlot rPlot = (RingPlot) chart.getPlot();
rPlot.setBackgroundPaint(Color.WHITE);
rPlot.setOutlineVisible(false);
rPlot.setShadowGenerator(null);
rPlot.setSeparatorsVisible(false);
rPlot.setSectionOutlinesVisible(false);
rPlot.setOuterSeparatorExtension(0);
rPlot.setInnerSeparatorExtension(0);
rPlot.setLabelGenerator(null);
rPlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {1} ({2})", locale));
LegendTitle legendTitle = chart.getLegend();
legendTitle.setPosition(RectangleEdge.RIGHT);
return chart;
}
private String writeChartAsPNG(DefaultPieDataset pieDataset, Locale locale, String title) {
File myFile = new File("image.png");
JFreeChart chart = createChart(pieDataset, title, locale);
byte[] fileContent = null;
try {
ChartUtils.saveChartAsPNG(
myFile,
chart,
500,
250
);
fileContent = FileUtils.readFileToByteArray(myFile);
} catch (IOException e) {
e.printStackTrace();
}
return java.util.Base64.getEncoder().encodeToString(fileContent);
}
Code: Select all
DefaultPieDataset defaultPieDataset = new DefaultPieDataset();
defaultPieDataset.setValue("Banana", 50);
defaultPieDataset.setValue("Apple", 10);
defaultPieDataset.setValue("Pear", 20);
defaultPieDataset.setValue("Orange", 70);
defaultPieDataset.setValue("Kiwi", 12);
this.writeChartAsPNG(defaultPieDataset, new Locale("FR"), "Fruits")
defaultPieDataset = new DefaultPieDataset();
defaultPieDataset.setValue("Banana Banana", 50);
defaultPieDataset.setValue("Apple Apple", 10);
defaultPieDataset.setValue("Pear Pear", 20);
defaultPieDataset.setValue("Orange Orange", 70);
defaultPieDataset.setValue("Kiwi Kiwi", 12);
this.writeChartAsPNG(defaultPieDataset, new Locale("FR"), "Fruits Fruits")
defaultPieDataset = new DefaultPieDataset();
defaultPieDataset.setValue("Banana Banana Banana", 50);
defaultPieDataset.setValue("Apple Apple Apple", 10);
defaultPieDataset.setValue("Pear Pear Pear", 20);
defaultPieDataset.setValue("Orange Orange Orange", 70);
defaultPieDataset.setValue("Kiwi Kiwi Kiwi", 12);
this.writeChartAsPNG(defaultPieDataset, new Locale("FR"), "Fruits Fruits Fruits")
defaultPieDataset = new DefaultPieDataset();
defaultPieDataset.setValue("Banana Banana Banana Banana Banana Banana Banana", 50);
defaultPieDataset.setValue("Apple Apple Apple Apple Apple Apple Apple", 10);
defaultPieDataset.setValue("Pear Pear Pear Pear Pear Pear Pear", 20);
defaultPieDataset.setValue("Orange Orange Orange Orange Orange Orange Orange", 70);
defaultPieDataset.setValue("Kiwi Kiwi Kiwi Kiwi Kiwi Kiwi Kiwi", 12);
this.writeChartAsPNG(defaultPieDataset, new Locale("FR"), "Fruits Fruits Fruits Fruits")