ingo@369: package de.intevation.flys.exports; ingo@369: ingo@369: import java.awt.Color; ingo@369: ingo@369: import java.io.IOException; ingo@369: ingo@369: import org.apache.log4j.Logger; ingo@369: ingo@369: import org.jfree.chart.ChartFactory; ingo@369: import org.jfree.chart.JFreeChart; ingo@369: import org.jfree.chart.axis.NumberAxis; ingo@369: import org.jfree.chart.plot.PlotOrientation; ingo@369: import org.jfree.chart.plot.XYPlot; ingo@369: ingo@369: import de.intevation.flys.exports.ChartExportHelper; ingo@369: ingo@369: ingo@369: /** ingo@369: * An abstract base class for creating XY charts. ingo@369: * ingo@369: * @author Ingo Weinzierl ingo@369: */ ingo@369: public abstract class XYChartGenerator extends ChartGenerator { ingo@369: ingo@369: /** The logger that is used in this generator.*/ ingo@369: private static Logger logger = Logger.getLogger(ChartGenerator.class); ingo@369: ingo@369: ingo@369: /** ingo@369: * Returns the title of a chart. ingo@369: * ingo@369: * @return the title of a chart. ingo@369: */ ingo@369: protected abstract String getChartTitle(); ingo@369: ingo@369: /** ingo@369: * Returns the X-Axis label of a chart. ingo@369: * ingo@369: * @return the X-Axis label of a chart. ingo@369: */ ingo@369: protected abstract String getXAxisLabel(); ingo@369: ingo@369: /** ingo@369: * Returns the Y-Axis label of a chart. ingo@369: * ingo@369: * @return the Y-Axis label of a chart. ingo@369: */ ingo@369: protected abstract String getYAxisLabel(); ingo@369: ingo@369: /** ingo@375: * This method is called to add all datasets of a concrete XYChartGenerator ingo@375: * to the JFreeChart. ingo@369: * ingo@375: * @param chart The JFreeChart object. ingo@369: */ ingo@375: protected abstract void addDatasets(JFreeChart chart); ingo@369: ingo@369: ingo@369: public void generate() ingo@369: throws IOException ingo@369: { ingo@369: logger.debug("XYChartGenerator.generate"); ingo@369: ingo@369: JFreeChart chart = ChartFactory.createXYLineChart( ingo@369: getChartTitle(), ingo@369: getXAxisLabel(), ingo@369: getYAxisLabel(), ingo@375: null, ingo@369: PlotOrientation.VERTICAL, ingo@369: true, ingo@369: false, ingo@369: false); ingo@369: ingo@369: chart.setBackgroundPaint(Color.WHITE); ingo@369: chart.getPlot().setBackgroundPaint(Color.WHITE); ingo@369: ingo@375: addDatasets(chart); ingo@414: addSubtitles(chart); ingo@369: adjustAxes((XYPlot) chart.getPlot()); ingo@369: ingo@369: ChartExportHelper.exportImage( ingo@369: out, ingo@369: chart, ingo@369: "png", ingo@369: 600, 400); ingo@369: } ingo@369: ingo@369: ingo@369: /** ingo@369: * Adjusts the axes of a plot. ingo@369: * ingo@369: * @param plot The XYPlot of the chart. ingo@369: */ ingo@369: protected void adjustAxes(XYPlot plot) { ingo@369: NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); ingo@369: ingo@369: yAxis.setAutoRangeIncludesZero(false); ingo@369: } ingo@414: ingo@414: ingo@414: protected void addSubtitles(JFreeChart chart) { ingo@414: // override this method in subclasses that need subtitles ingo@414: } ingo@369: } ingo@369: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :