In the example below, Eden is the series at index 0
, Survivor at index 1
and Old at index 2
.
It the examle, Old is the top-most series in the chart, while Eden is at the bottom.
What I would like to achieve is that Eden is the top-most series in the stacked chart, Survivor the one in the middle and Old on the bottom. Still, Eden should be the first entry in the legend and Old should be the last one.
I currently use the following class to create my charts:
public class JFreeChartFactory {
private static String fontName = "Palatino";
private static Color[] chartSeriesColors = {
new Color(0.0f, 1.0f, 0.0f, 0.9f),
new Color(0.0f, 0.0f, 1.0f, 0.9f),
new Color(1.0f, 0.0f, 0.0f, 0.9f),
};
public static JFreeChart createStackedXYAreaChart(String title, String subTitle, String xAxis, String yAxis, TableXYDataset dataset) {
JFreeChart chart = ChartFactory.createStackedXYAreaChart(
title,
xAxis,
yAxis,
dataset);
chart.getTitle().setFont(new Font(fontName, Font.BOLD, 18));
if (subTitle != null) {
chart.addSubtitle(new TextTitle(subTitle, new Font(fontName, Font.PLAIN, 14)));
}
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setLabelFont(new Font(fontName, Font.BOLD, 14));
plot.getDomainAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12));
plot.getRangeAxis().setLabelFont(new Font(fontName, Font.BOLD, 14));
plot.getRangeAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font(fontName, Font.PLAIN, 14));
chart.getLegend().setFrame(BlockBorder.NONE);
chart.getLegend().setHorizontalAlignment(HorizontalAlignment.CENTER);
for (int i = 0; i < plot.getRendererCount(); i++) {
XYItemRenderer renderer = plot.getRenderer(i);
switch (renderer.getClass().getSimpleName()) {
case "StackedXYAreaRenderer2":
StackedXYAreaRenderer2 stackedRenderer = (StackedXYAreaRenderer2) renderer;
for (int cId = 0; cId < chartSeriesColors.length; cId++) {
stackedRenderer.setSeriesPaint(cId, chartSeriesColors[cId]);
}
break;
}
}
return chart;
}
}