Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 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 | 5d5457a1bd5f |
children | d75b427da50a |
line wrap: on
line diff
--- 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); }