my chart is supposed to contain three dataseries. Two should be rendered with a XYLineAndShapeRenderer but one of them with an area using an XYAreaRenderer. With the below code the two lines and one area are painted as expected, however the legend contains the series twice which is my problem. It seems this is caused by adding the dataset twice - once in the ChartFactory.createTimeSeriesChart(...) and again in my own code with plot.setDataset(1, dataset).
How could I draw two lines and one area within the same chart and at the same time have the legend contain only one entry for each series?
Regards,
Jumbochart
Code: Select all
public static JFreeChart createChart(final String title, final String xLabel, final String yLabel, final XYDataset dataset,
final List<Color> plotColors, final List<String> storkeStyles, final Collection<String> areaPlots) {
boolean hasAreaPlots = areaPlots != null && areaPlots.size() > 0;
final JFreeChart chart = hasAreaPlots ? ChartFactory.createTimeSeriesChart(title, xLabel, yLabel, dataset, true, true, true) :
ChartFactory.createXYStepChart(title, xLabel, yLabel, dataset, PlotOrientation.VERTICAL, true, true, true);
final XYPlot plot = chart.getXYPlot();
final DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd.MM.yy,EE"));
axis.setAutoRange(true);
AbstractXYItemRenderer ar = null;
final XYItemRenderer rr = plot.getRenderer();
if (hasAreaPlots) {
plot.setDataset(1, dataset);
ar = new XYAreaRenderer();
plot.setRenderer(1, ar);
}
for (int iSeries = 0; iSeries < dataset.getSeriesCount(); iSeries++) {
if (plotColors != null && plotColors.size() > iSeries) {
Color color = plotColors.get(iSeries);
rr.setSeriesPaint(iSeries, color);
}
boolean isAreaPlot = false;
if (ar != null) {
isAreaPlot = areaPlots.contains(dataset.getSeriesKey(iSeries));
rr.setSeriesVisibleInLegend(iSeries, Boolean.valueOf(!isAreaPlot));
ar.setSeriesVisible(iSeries, Boolean.valueOf(isAreaPlot));
ar.setSeriesPaint(iSeries, rr.getSeriesPaint(iSeries));
ar.setSeriesFillPaint(iSeries, rr.getSeriesPaint(iSeries));
}
}
return chart;
}