Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 710:cded0924193d
Added generator and facet for error reports.
flys-artifacts/trunk@2166 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 20 Jun 2011 13:47:59 +0000 |
parents | b22f21b173a7 |
children | be4709640aac |
line wrap: on
line source
package de.intevation.flys.exports; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Stroke; import java.io.IOException; import org.apache.log4j.Logger; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.Range; import org.jfree.data.xy.XYDataset; import org.jfree.ui.RectangleInsets; import de.intevation.flys.exports.ChartExportHelper; /** * An abstract base class for creating XY charts. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public abstract class XYChartGenerator extends ChartGenerator { /** The logger that is used in this generator.*/ private static Logger logger = Logger.getLogger(XYChartGenerator.class); public static final Color DEFAULT_GRID_COLOR = Color.GRAY; public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; /** * Returns the title of a chart. * * @return the title of a chart. */ protected abstract String getChartTitle(); /** * Returns the X-Axis label of a chart. * * @return the X-Axis label of a chart. */ protected abstract String getXAxisLabel(); /** * Returns the Y-Axis label of a chart. * * @return the Y-Axis label of a chart. */ protected abstract String getYAxisLabel(); /** * This method is called to add all datasets of a concrete XYChartGenerator * to the JFreeChart. * * @param chart The JFreeChart object. */ protected abstract void addDatasets(JFreeChart chart); public void generate() throws IOException { logger.debug("XYChartGenerator.generate"); JFreeChart chart = generateChart(); int[] size = getSize(); ChartExportHelper.exportImage( out, chart, "png", size[0], size[1]); } public JFreeChart generateChart() { logger.debug("XYChartGenerator.generateChart"); JFreeChart chart = ChartFactory.createXYLineChart( getChartTitle(), getXAxisLabel(), getYAxisLabel(), null, PlotOrientation.VERTICAL, true, false, false); chart.setBackgroundPaint(Color.WHITE); chart.getPlot().setBackgroundPaint(Color.WHITE); XYPlot plot = (XYPlot) chart.getPlot(); addDatasets(chart); addSubtitles(chart); adjustPlot(plot); adjustAxes(plot); autoZoom(plot); return chart; } /** * This method zooms the plot to the specified ranges in the attribute * document or to the ranges specified by the min/max values in the * datasets. <b>Note:</b> We determine the range manually if no zoom ranges * are given, because JFreeCharts auto-zoom adds a margin to the left and * right of the data area. * * @param plot The XYPlot. */ protected void autoZoom(XYPlot plot) { logger.debug("Zoom to specified ranges."); Range xrange = getDomainAxisRange(); Range yrange = getValueAxisRange(); for (int i = 0, num = plot.getDatasetCount(); i < num; i++) { XYDataset dataset = plot.getDataset(i); Range[] ranges = getRangesForDataset(dataset); if (i == 0) { ValueAxis xaxis = plot.getDomainAxis(); zoom(plot, xaxis, ranges[0], xrange); } ValueAxis yaxis = plot.getRangeAxis(i); if (yaxis == null) { continue; } zoom(plot, yaxis, ranges[1], yrange); } } /** * Zooms the x axis to the range specified in the attribute document. * * @param plot The XYPlot. * @param axis The axis the shoud be modified. * @param range The whole range specified by a dataset. * @param x A user defined range (null permitted). * * @return true, if a zoom range was specified, otherwise false. */ protected boolean zoom(XYPlot plot, ValueAxis axis, Range range, Range x) { if (x != null) { double min = range.getLowerBound(); double max = range.getUpperBound(); double diff = max > min ? max - min : min - max; Range computed = new Range( min + x.getLowerBound() * diff, min + x.getUpperBound() * diff); axis.setRange(computed); logger.debug("Zoom axis to: " + computed); return true; } axis.setRange(range); return false; } /** * This method extracts the minimum and maximum values for x and y axes. * * @param dataset The dataset that should be observed. * * @return a Range[] as follows: [x-Range, y-Range]. */ public static Range[] getRangesForDataset(XYDataset dataset) { double[] xr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; double[] yr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; int sCount = dataset.getSeriesCount(); for (int i = 0; i < sCount; i++) { int iCount = dataset.getItemCount(i); for (int j = 0; j < iCount; j++) { double x = dataset.getX(i, j).doubleValue(); double y = dataset.getY(i, j).doubleValue(); if (!Double.isNaN(x)) { xr[0] = xr[0] < x ? xr[0] : x; xr[1] = xr[1] > x ? xr[1] : x; } if (!Double.isNaN(y)) { yr[0] = yr[0] < y ? yr[0] : y; yr[1] = yr[1] > y ? yr[1] : y; } } } // this is only required, if there are no items in the dataset. xr[0] = xr[0] < xr[1] ? xr[0] : xr[1]; xr[1] = xr[1] > xr[0] ? xr[1] : xr[0]; yr[0] = yr[0] < yr[1] ? yr[0] : yr[1]; yr[1] = yr[1] > yr[0] ? yr[1] : yr[0]; return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])}; } /** * Adjusts the axes of a plot. * * @param plot The XYPlot of the chart. */ protected void adjustAxes(XYPlot plot) { NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setAutoRangeIncludesZero(false); } protected void adjustPlot(XYPlot plot) { Stroke gridStroke = new BasicStroke( DEFAULT_GRID_LINE_WIDTH, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, new float[] { 3.0f }, 0.0f); plot.setDomainGridlineStroke(gridStroke); plot.setDomainGridlinePaint(DEFAULT_GRID_COLOR); plot.setDomainGridlinesVisible(true); plot.setRangeGridlineStroke(gridStroke); plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR); plot.setRangeGridlinesVisible(true); plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); } protected void addSubtitles(JFreeChart chart) { // override this method in subclasses that need subtitles } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :