Mercurial > dive4elements > river
changeset 2587:bece6f604899
Removed references to Range and replaced those with references to Bounds in ChartGenerators.
flys-artifacts/trunk@4143 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 15 Mar 2012 10:30:03 +0000 |
parents | 8cd6358eb7f8 |
children | 7dd45896e941 |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java |
diffstat | 7 files changed, 188 insertions(+), 185 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu Mar 15 10:30:03 2012 +0000 @@ -1,3 +1,27 @@ +2012-03-15 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/exports/ChartHelper.java: Added a new + method that returns the Bounds for a given XYDataset. Based on the + concrete type of the XYDataset, the call is dispatched to a more specific + method. + + * src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java: + Removed the whole "range" stuff and some methods that are implemented in + upper classes. + + * src/main/java/de/intevation/flys/exports/ChartGenerator.java: Removed the + setXRange() and setYRange() methods. In our own code, we will use Bounds + instead of JFreeChart's Range instances to save range/bounds information. + This is necessary to save information which is not not from type double + (which is the case in Timeseries charts). + + * src/main/java/de/intevation/flys/exports/XYChartGenerator.java: Replaced + usage of Range with Bounds in the whole class. + + * src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java, + src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java: + Adapted the method signature of zoom() and zoomX(). + 2012-03-14 Ingo Weinzierl <ingo@intevation.de> * src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java Thu Mar 15 10:30:03 2012 +0000 @@ -53,6 +53,7 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.resources.Resources; import de.intevation.flys.jfree.Bounds; +import de.intevation.flys.jfree.DoubleBounds; import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer; import de.intevation.flys.jfree.StableXYDifferenceRenderer; import de.intevation.flys.jfree.StyledAreaSeriesCollection; @@ -178,25 +179,6 @@ protected abstract Series getSeriesOf(XYDataset dataset, int idx); - - /** - * This method is used to set the range of the X axis at index <i>axis</i>. - * - * @param axis The index of an X axis. - * @param range The new range for the X axis at index <i>axis</i>. - */ - protected abstract void setXRange(int axis, Range range); - - - /** - * This method is used to set the range of the Y axis at index <i>axis</i>. - * - * @param axis The index of an Y axis. - * @param range The new range for the Y axis at index <i>axis</i>. - */ - protected abstract void setYRange(int axis, Range range); - - /** * Returns the default title of a chart. * @@ -240,7 +222,17 @@ * @param idx The index of the X axis that should be comined with * <i>range</i>. */ - protected abstract void combineXRanges(Range range, int idx); + protected abstract void combineXBounds(Bounds bounds, int idx); + + + /** + * Combines the ranges of the Y axis at index <i>idx</i>. + * + * @param range A new range. + * @param idx The index of the Y axis that should be comined with + * <i>range</i>. + */ + protected abstract void combineYBounds(Bounds bounds, int index); /** @@ -861,9 +853,9 @@ AxisDataset axisDataset = getAxisDataset(idx); - Range[] xyRanges = ChartHelper.getRanges(dataset); + Bounds[] xyBounds = ChartHelper.getBounds(dataset); - if (xyRanges == null) { + if (xyBounds == null) { logger.warn("Skip XYDataset for Axis (invalid ranges): " + idx); return; } @@ -871,21 +863,15 @@ if (visible) { if (logger.isDebugEnabled()) { logger.debug("Add new AxisDataset at index: " + idx); - logger.debug("X extent: " + xyRanges[0]); - logger.debug("Y extent: " + xyRanges[1]); + logger.debug("X extent: " + xyBounds[0]); + logger.debug("Y extent: " + xyBounds[1]); } axisDataset.addDataset(dataset); - combineXRanges(xyRanges[0], 0); } - else { - combineXRanges(xyRanges[0], 0); - // TODO - // Expand y range provided by 'timeseries' to have a proper range - // set which includes all data. - // iw: I am not sure if we still need this - } + combineXBounds(xyBounds[0], 0); + combineYBounds(xyBounds[1], idx); } @@ -1129,7 +1115,7 @@ Range.expandToInclude(axisDataset.getRange(), 0d)); } - setYRange(axisIndex, expandPointRange(axisDataset.getRange())); + setYBounds(axisIndex, expandPointRange(axisDataset.getRange())); // Add contained datasets, mapping to axis. for (XYDataset dataset: axisDataset.getDatasets()) { @@ -1280,11 +1266,19 @@ * * @param Range to be expanded if upper == lower bound. */ - private Range expandPointRange(Range range) { - if (range != null && range.getLowerBound() == range.getUpperBound()) { - return ChartHelper.expandRange(range, 5); + private Bounds expandPointRange(Range range) { + if (range == null) { + return null; } - return range; + else if (range.getLowerBound() == range.getUpperBound()) { + double hi = range.getUpperBound(); + double lo = range.getLowerBound(); + double add = (hi - lo) / 100 * 5; + + return new DoubleBounds(lo-add, hi+add); + } + + return new DoubleBounds(range.getLowerBound(), range.getUpperBound()); }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java Thu Mar 15 10:30:03 2012 +0000 @@ -2,6 +2,7 @@ import org.jfree.data.Range; import org.jfree.data.xy.XYDataset; +import org.jfree.data.xy.XYSeriesCollection; import org.jfree.data.time.RegularTimePeriod; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.time.TimeSeries; @@ -29,7 +30,7 @@ * * @return an array with x and y ranges. */ - public static Range[] getRanges(XYDataset dataset) { + public static Bounds[] getBounds(XYSeriesCollection dataset) { int seriesCount = dataset != null ? dataset.getSeriesCount() : 0; if (seriesCount == 0) { @@ -75,11 +76,27 @@ } return foundValue - ? new Range[] { new Range(minX, maxX), new Range(minY, maxY) } + ? new Bounds[] { + new DoubleBounds(minX, maxX), + new DoubleBounds(minY, maxY) } : null; } + public static Bounds[] getBounds(XYDataset dataset) { + if (dataset instanceof XYSeriesCollection) { + return getBounds((XYSeriesCollection) dataset); + } + else if(dataset instanceof TimeSeriesCollection) { + return getBounds((TimeSeriesCollection) dataset); + } + else { + logger.warn("Unknown XYDataset instance: " + dataset.getClass()); + return null; + } + } + + public static Bounds[] getBounds(TimeSeriesCollection collection) { int seriesCount = collection != null ? collection.getSeriesCount() : 0;
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Thu Mar 15 10:30:03 2012 +0000 @@ -24,6 +24,7 @@ import de.intevation.flys.artifacts.model.WQKms; import de.intevation.flys.utils.FLYSUtils; +import de.intevation.flys.jfree.Bounds; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.jfree.StyledXYSeries; @@ -115,8 +116,8 @@ /* TODO is this one really needed? */ @Override - protected boolean zoomX(XYPlot plot, ValueAxis axis, Range range, Range x) { - boolean zoomin = super.zoom(plot, axis, range, x); + protected boolean zoomX(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { + boolean zoomin = super.zoom(plot, axis, bounds, x); if (!zoomin) { axis.setLowerBound(0d);
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java Thu Mar 15 10:30:03 2012 +0000 @@ -20,6 +20,7 @@ import de.intevation.flys.artifacts.model.WQDay; import de.intevation.flys.artifacts.resources.Resources; +import de.intevation.flys.jfree.Bounds; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.jfree.StyledXYSeries; @@ -148,8 +149,8 @@ @Override - protected boolean zoomX(XYPlot plot, ValueAxis axis, Range range, Range x) { - boolean zoomin = super.zoom(plot, axis, range, x); + protected boolean zoomX(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { + boolean zoomin = super.zoom(plot, axis, bounds, x); if (!zoomin) { axis.setLowerBound(0d); @@ -167,8 +168,8 @@ * values on its own. */ @Override - protected boolean zoomY(XYPlot plot, ValueAxis axis, Range range, Range x) { - boolean zoomin = super.zoom(plot, axis, range, x); + protected boolean zoomY(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { + boolean zoomin = super.zoom(plot, axis, bounds, x); if (!zoomin && axis instanceof IdentifiableNumberAxis) { String id = ((IdentifiableNumberAxis) axis).getId();
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java Thu Mar 15 10:30:03 2012 +0000 @@ -130,9 +130,9 @@ public static final int AXIS_SPACE = 5; - protected Map<Integer, Bounds> xRanges; + protected Map<Integer, Bounds> xBounds; - protected Map<Integer, Bounds> yRanges; + protected Map<Integer, Bounds> yBounds; @@ -142,8 +142,8 @@ public TimeseriesChartGenerator() { super(); - xRanges = new HashMap<Integer, Bounds>(); - yRanges = new HashMap<Integer, Bounds>(); + xBounds = new HashMap<Integer, Bounds>(); + yBounds = new HashMap<Integer, Bounds>(); } @@ -183,34 +183,6 @@ } - // TODO DECLARE IN UPPER CLASS AND ADD OVERRIDE ANNOTATION - protected Bounds getXRange(int axis) { - return xRanges.get(Integer.valueOf(axis)); - } - - - @Override - // TODO setXRange should always await a Bounds instance! - // TODO SHOULD BE REMOVED WHEN DEFINED IN UPPER CLASS - protected void setXRange(int axis, Range range) { - // do nothing here, we will use setXRange(int, Bounds) now - } - - - @Override - // TODO setYRange should always await a Bounds instance! - protected void setYRange(int axis, Range range) { - if (range == null) { - logger.warn("Range is null!"); - return; - } - - setYBounds(Integer.valueOf(axis), new DoubleBounds( - range.getLowerBound(), - range.getUpperBound())); - } - - /** * This method creates new instances of TimeseriesAxisDataset. * @@ -223,52 +195,10 @@ } - // TODO THIS SHOULD BE DONE IN AN UPPER CLASS! @Override - public void addAxisDataset(XYDataset dataset, int idx, boolean visible) { - if (dataset == null || idx < 0) { - return; - } - - AxisDataset axisDataset = getAxisDataset(idx); - - Bounds[] bounds = ChartHelper.getBounds((TimeSeriesCollection)dataset); - - if (bounds == null) { - logger.warn("Skip XYDataset for Axis (invalid ranges): " + idx); - return; - } - - if (visible) { - if (logger.isDebugEnabled()) { - logger.debug("Add new AxisDataset at index: " + idx); - logger.debug("X extent: " + bounds[0]); - logger.debug("Y extent: " + bounds[1]); - } - - axisDataset.addDataset(dataset); - } - - combineXRanges(bounds[0], 0); - combineYRanges(bounds[1], idx); - } - - - /** - * Effect: extend range of x axis to include given limits. - * @param range the given ("minimal") range. - * @param index index of axis to be merged. - */ - @Override - protected void combineXRanges(Range range, int index) { - throw new RuntimeException( - "TimeseriesChartGenerator.combineXRanges is not implemented!"); - } - - - protected void combineXRanges(Bounds bounds, int index) { + protected void combineXBounds(Bounds bounds, int index) { if (bounds != null) { - Bounds old = getXRange(index); + Bounds old = getXBounds(index); if (old != null) { bounds = bounds.combine(old); @@ -279,7 +209,8 @@ } - protected void combineYRanges(Bounds bounds, int index) { + @Override + protected void combineYBounds(Bounds bounds, int index) { if (bounds != null) { Bounds old = getYBounds(index); @@ -311,25 +242,27 @@ @Override public Bounds getXBounds(int axis) { - return xRanges.get(axis); + return xBounds.get(axis); } @Override protected void setXBounds(int axis, Bounds bounds) { - xRanges.put(axis, bounds); + xBounds.put(axis, bounds); } @Override public Bounds getYBounds(int axis) { - return yRanges.get(axis); + return yBounds.get(axis); } @Override protected void setYBounds(int axis, Bounds bounds) { - yRanges.put(axis, bounds); + if (bounds != null) { + yBounds.put(axis, bounds); + } } @@ -415,7 +348,7 @@ protected void adaptZoom(XYPlot plot) { logger.debug("Adapt zoom of Timeseries chart."); - zoomX(plot, plot.getDomainAxis(), getXRange(0), getDomainAxisRange()); + zoomX(plot, plot.getDomainAxis(), getXBounds(0), getDomainAxisRange()); Bounds valueAxisBounds = getValueAxisRange();
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Wed Mar 14 15:12:45 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Thu Mar 15 10:30:03 2012 +0000 @@ -42,6 +42,7 @@ import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.jfree.Bounds; +import de.intevation.flys.jfree.DoubleBounds; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.jfree.StickyAxisAnnotation; import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation; @@ -173,6 +174,8 @@ protected abstract YAxisWalker getYAxisWalker(); + public static final int AXIS_SPACE = 5; + /** The logger that is used in this generator. */ private static Logger logger = Logger.getLogger(XYChartGenerator.class); @@ -180,17 +183,17 @@ protected List<FLYSAnnotation> annotations; /** The max X range to include all X values of all series for each axis. */ - protected Map<Integer, Range> xRanges; + protected Map<Integer, Bounds> xBounds; /** The max Y range to include all Y values of all series for each axis. */ - protected Map<Integer, Range> yRanges; + protected Map<Integer, Bounds> yBounds; public XYChartGenerator() { super(); - xRanges = new HashMap<Integer, Range>(); - yRanges = new HashMap<Integer, Range>(); + xBounds = new HashMap<Integer, Bounds>(); + yBounds = new HashMap<Integer, Bounds>(); } @@ -247,18 +250,6 @@ @Override - protected void setXRange(int axis, Range range) { - xRanges.put(Integer.valueOf(axis), range); - } - - - @Override - protected void setYRange(int axis, Range range) { - yRanges.put(Integer.valueOf(axis), range); - } - - - @Override protected AxisDataset createAxisDataset(int idx) { logger.debug("Create new XYAxisDataset for index: " + idx); return new XYAxisDataset(idx); @@ -358,24 +349,57 @@ /** * Effect: extend range of x axis to include given limits. + * * @param range the given ("minimal") range. * @param index index of axis to be merged. */ - protected void combineXRanges(Range range, int index) { - - if (range == null - || Double.isNaN(range.getLowerBound()) - || Double.isNaN(range.getUpperBound())) { + @Override + protected void combineXBounds(Bounds bounds, int index) { + if (!(bounds instanceof DoubleBounds)) { + logger.warn("Unsupported Bounds type: " + bounds.getClass()); return; } - Range old = xRanges.get(index); + DoubleBounds dBounds = (DoubleBounds) bounds; + + if (dBounds == null + || Double.isNaN((Double) dBounds.getLower()) + || Double.isNaN((Double) dBounds.getUpper())) { + return; + } + + Bounds old = getXBounds(index); if (old != null) { - range = Range.combine(old, range); + dBounds = (DoubleBounds) dBounds.combine(old); } - xRanges.put(index, range); + setXBounds(index, dBounds); + } + + + @Override + protected void combineYBounds(Bounds bounds, int index) { + if (!(bounds instanceof DoubleBounds)) { + logger.warn("Unsupported Bounds type: " + bounds.getClass()); + return; + } + + DoubleBounds dBounds = (DoubleBounds) bounds; + + if (dBounds == null + || Double.isNaN((Double) dBounds.getLower()) + || Double.isNaN((Double) dBounds.getUpper())) { + return; + } + + Bounds old = getYBounds(index); + + if (old != null) { + dBounds = (DoubleBounds) dBounds.combine(old); + } + + setYBounds(index, dBounds); } @@ -412,11 +436,16 @@ private void preparePointRanges(XYPlot plot) { for (int i = 0, num = plot.getDomainAxisCount(); i < num; i++) { logger.debug("Check whether to expand a x axis."); - Integer key = Integer.valueOf(i); - Range r = xRanges.get(key); - if (r != null && r.getLowerBound() == r.getUpperBound()) { - setXRange(key, ChartHelper.expandRange(r, 5)); + Integer key = Integer.valueOf(i); + Bounds b = getXBounds(key); + + if (b != null && b.getLower() == b.getUpper()) { + double lo = (Double) b.getLower(); + double hi = (Double) b.getUpper(); + double add = (hi - lo) / 100 * 5; + + setXBounds(key, new DoubleBounds(lo-add, hi+add)); } } } @@ -444,7 +473,7 @@ xAxis.setRange(fixedXRange); } else { - zoomX(plot, xAxis, xRanges.get(0), xrange); + zoomX(plot, xAxis, getXBounds(0), xrange); } for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) { @@ -466,7 +495,7 @@ } logger.debug("Prepare zoom settings for y axis at index: " + i); - zoomY(plot, yaxis, yRanges.get(Integer.valueOf(i)), yrange); + zoomY(plot, yaxis, getYBounds(Integer.valueOf(i)), yrange); } } @@ -537,13 +566,13 @@ } - protected boolean zoomX(XYPlot plot, ValueAxis axis, Range range, Range x) { - return zoom(plot, axis, range, x); + protected boolean zoomX(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { + return zoom(plot, axis, bounds, x); } - protected boolean zoomY(XYPlot plot, ValueAxis axis, Range range, Range x) { - return zoom(plot, axis, range, x); + protected boolean zoomY(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { + return zoom(plot, axis, bounds, x); } @@ -557,29 +586,37 @@ * * @return true, if a zoom range was specified, otherwise false. */ - protected boolean zoom(XYPlot plot, ValueAxis axis, Range range, Range x) { + protected boolean zoom(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) { - if (range == null) { + if (bounds == null) { return false; } if (x != null) { - double min = range.getLowerBound(); - double max = range.getUpperBound(); + double min = bounds.getLower().doubleValue(); + double max = bounds.getUpper().doubleValue(); + + if (logger.isDebugEnabled()) { + logger.debug("Minimum is: " + min); + logger.debug("Maximum is: " + max); + logger.debug("Lower zoom is: " + x.getLowerBound()); + logger.debug("Upper zoom is: " + x.getUpperBound()); + } + double diff = max > min ? max - min : min - max; - Range computed = new Range( + DoubleBounds computed = new DoubleBounds( min + x.getLowerBound() * diff, min + x.getUpperBound() * diff); - axis.setRangeWithMargins(computed); + computed.applyBounds(axis, AXIS_SPACE); logger.debug("Zoom axis to: " + computed); return true; } - axis.setRangeWithMargins(range); + bounds.applyBounds(axis, AXIS_SPACE); return false; } @@ -596,52 +633,48 @@ public Range[] getRangesForAxis(int index) { logger.debug("getRangesForAxis " + index); - Range rx = xRanges.get(Integer.valueOf(0)); - Range ry = yRanges.get(Integer.valueOf(index)); + Bounds rx = getXBounds(Integer.valueOf(0)); + Bounds ry = getYBounds(Integer.valueOf(index)); if (rx == null) { logger.warn("Range for x axis not set." + " Using default values: 0 - 1."); - rx = new Range(0, 1); + rx = new DoubleBounds(0, 1); } if (ry == null) { logger.warn("Range for y" + index + " axis not set. Using default values: 0 - 1."); - ry = new Range(0, 1); + ry = new DoubleBounds(0, 1); } - return new Range[] {rx, ry}; + + return new Range[] { + new Range(rx.getLower().doubleValue(), rx.getUpper().doubleValue()), + new Range(ry.getLower().doubleValue(), ry.getUpper().doubleValue()) + }; } @Override public Bounds getXBounds(int axis) { - // TODO IMPLEMENT ME - throw new RuntimeException( - "XYChartGenerator.getXBounds(int) not implemented"); + return xBounds.get(axis); } @Override protected void setXBounds(int axis, Bounds bounds) { - // TODO IMPLEMENT ME - throw new RuntimeException( - "XYChartGenerator.setXBounds(int,Bounds) not implemented"); + xBounds.put(axis, bounds); } @Override public Bounds getYBounds(int axis) { - // TODO IMPLEMENT ME - throw new RuntimeException( - "XYChartGenerator.getYBounds(int) not implemented"); + return yBounds.get(axis); } @Override protected void setYBounds(int axis, Bounds bounds) { - // TODO IMPLEMENT ME - throw new RuntimeException( - "XYChartGenerator.setYBounds(int,Bounds) not implemented"); + yBounds.put(axis, bounds); }