Mercurial > dive4elements > gnv-client
changeset 364:2413273f1c13
Workarround: Store lower and upper bounds of data while iterating over all data and set the max range of axes with these information. JFreeCharts method NumberAxis.setAutoRange(true) doesn't seem to work properly.
gnv-artifacts/trunk@439 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 16 Dec 2009 11:58:44 +0000 |
parents | 22229249e9fc |
children | f66088a43ecc |
files | gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java |
diffstat | 4 files changed, 101 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog Wed Dec 16 08:05:25 2009 +0000 +++ b/gnv-artifacts/ChangeLog Wed Dec 16 11:58:44 2009 +0000 @@ -1,3 +1,16 @@ +2009-12-16 Ingo Weinzierl <ingo.weinzierl@intevation.de> + + Issue100 + + * src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java, + src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java, + src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java: + Workarround: Store information about max range of data for each parameter + while iterating over all data values. Set the max range at the end of + chart generation. + + NOTE: NumberAxis.setAutoRange(true) doesn't seem to work properly. + 2009-12-16 Ingo Weinzierl <ingo.weinzierl@intevation.de> * src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java Wed Dec 16 08:05:25 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java Wed Dec 16 11:58:44 2009 +0000 @@ -20,6 +20,8 @@ import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.chart.title.TextTitle; +import org.jfree.data.xy.XYDataset; +import org.jfree.data.Range; import org.jfree.data.general.Series; import de.intevation.gnv.geobackend.base.Result; @@ -32,6 +34,9 @@ public abstract class AbstractXYLineChart extends AbstractChart { + public static final double LOWER_MARGIN = 0.05D; + public static final double UPPER_MARGIN = 0.05D; + private static Logger log = Logger.getLogger(AbstractXYLineChart.class); protected static Color[] COLOR = { @@ -43,8 +48,13 @@ protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; + /** Map to store datasets for each parameter */ protected Map datasets; + /** Map to store max ranges of each parameter (axis.setAutoRange(true) + * doesn't seem to work */ + protected Map ranges; + protected abstract void initData(); protected abstract void addValue(Result row, Series series); protected abstract void addSeries(Series series, String label, int idx); @@ -100,24 +110,29 @@ localizeDomainAxis(xAxis, locale); localizeRangeAxis(yAxis, locale); + // litte workarround to adjust the max range of axes. + // NumberAxis.setAutoRange(true) doesn't seem to work properly. + Range yRange = (Range) ranges.get(seriesKey); + yAxis.setRange(Range.expand(yRange, LOWER_MARGIN, UPPER_MARGIN)); + log.debug("Max Range of dataset is: " + yRange.toString()); + if (seriesKey.contains("richtung")) { yAxis.setTickUnit(new NumberTickUnit(30.0)); yAxis.setUpperBound(360.0); yAxis.setLowerBound(0.0); - plot.setRangeAxis(idx, yAxis); } else { yAxis.setFixedDimension(10.0); yAxis.setAutoRangeIncludesZero(false); - plot.setRangeAxis(idx, yAxis); - yAxis.configure(); } + plot.setRangeAxis(idx, yAxis); + yAxis.configure(); + if (idx % 2 != 0) plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_RIGHT); else plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_LEFT); - plot.mapDatasetToRangeAxis(idx, idx); } @@ -172,6 +187,42 @@ } + public Range getMaxRangeOfDataset(XYDataset dataset) { + int seriesCount = dataset.getSeriesCount(); + double upper = Double.NEGATIVE_INFINITY; + double lower = Double.POSITIVE_INFINITY; + + for (int i = 0; i < seriesCount; i++) { + int itemCount = dataset.getItemCount(i); + + for (int j = 0; j < itemCount; j++) { + Number num = dataset.getY(i, j); + + if (num != null) { + double y = num.doubleValue(); + lower = y < lower ? y : lower; + upper = y > upper ? y : upper; + } + } + } + + return new Range(lower, upper); + } + + + public Range getMaxRangeOfDatasetWithMargin( + XYDataset dataset, + double percent + ) { + Range range = getMaxRangeOfDataset(dataset); + double length = range.getLength(); + double upper = range.getUpperBound() + length /100 * percent; + double lower = range.getLowerBound() - length /100 * percent; + + return new Range(lower, upper); + } + + protected String findParameter(String label) { Iterator iter = parameters.iterator(); @@ -201,5 +252,22 @@ } return ""; } + + + protected void storeMaxRange(double value, String parameter) { + Range range = null; + + range = ranges.containsKey(parameter) + ? (Range) ranges.get(parameter) + : new Range(value, value); + + double lower = range.getLowerBound(); + double upper = range.getUpperBound(); + + lower = value < lower ? value : lower; + upper = value > upper ? value : upper; + + ranges.put(parameter, new Range(lower, upper)); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java Wed Dec 16 08:05:25 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java Wed Dec 16 11:58:44 2009 +0000 @@ -67,6 +67,7 @@ this.linesVisible = linesVisible; this.shapesVisible = shapesVisible; this.datasets = new HashMap(); + this.ranges = new HashMap(); } @@ -93,6 +94,7 @@ Iterator iter = resultSet.iterator(); Result row = null; String seriesName = null; + String parameter = null; TimeSeries series = null; int idx = 0; @@ -119,7 +121,7 @@ startDate = results[startPos].getDate("XORDINATE"); endDate = results[endPos-1].getDate("XORDINATE"); addGaps(results,series,startDate,endDate,startPos,endPos); - addSeries(series, seriesName, idx); + addSeries(series, parameter, idx); startPos = endPos + 1; } @@ -134,12 +136,14 @@ breakPoint2, breakPoint3 ); + parameter = findParameter(seriesName); log.debug("next dataset is '" + seriesName + "'"); series = new TimeSeries(seriesName, Minute.class); } addValue(row, series); + storeMaxRange(row.getDouble("YORDINATE"), parameter); endPos++; } @@ -147,7 +151,7 @@ startDate = results[startPos].getDate("XORDINATE"); endDate = results[endPos-1].getDate("XORDINATE"); addGaps(results, series, startDate, endDate, startPos, endPos); - addSeries(series, seriesName, idx); + addSeries(series, parameter, idx); addDatasets(); } @@ -161,8 +165,8 @@ } - protected void addSeries(Series series, String label, int idx) { - log.debug("add series (" + label + ")to timeseries chart"); + protected void addSeries(Series series, String parameter, int idx) { + log.debug("add series (" + parameter + ")to timeseries chart"); if (series == null) { log.warn("no data to add"); @@ -171,7 +175,6 @@ TimeSeriesCollection tsc = null; - String parameter = findParameter(label); if (datasets.containsKey(parameter)) tsc = (TimeSeriesCollection) datasets.get(parameter); else
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java Wed Dec 16 08:05:25 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java Wed Dec 16 11:58:44 2009 +0000 @@ -56,6 +56,7 @@ this.linesVisible = linesVisible; this.shapesVisible = shapesVisible; this.datasets = new HashMap(); + this.ranges = new HashMap(); } @@ -69,6 +70,7 @@ Iterator iter = resultSet.iterator(); Result row = null; String seriesName = null; + String parameter = null; XYSeries series = null; int idx = 0; @@ -92,7 +94,7 @@ if(series != null) { gapDetection(results, series, startPos, endPos); - addSeries(series, seriesName, idx); + addSeries(series, parameter, idx); startPos = endPos +1; } @@ -107,12 +109,14 @@ breakPoint2, breakPoint3 ); + parameter = findParameter(seriesName); log.debug("next dataset is '" + seriesName + "'"); series = new XYSeries(seriesName); } addValue(row, series); + storeMaxRange(row.getDouble("YORDINATE"), parameter); endPos++; } @@ -120,7 +124,7 @@ return; gapDetection(results, series, startPos, endPos); - addSeries(series, seriesName, idx); + addSeries(series, parameter, idx); addDatasets(); } @@ -149,8 +153,8 @@ } - protected void addSeries(Series series, String label, int idx) { - log.debug("add series (" + label + ")to chart"); + protected void addSeries(Series series, String parameter, int idx) { + log.debug("add series (" + parameter + ")to chart"); if (series == null) { log.warn("no data to add"); @@ -158,7 +162,6 @@ } XYSeriesCollection xysc = null; - String parameter = findParameter(label); if (datasets.containsKey(parameter)) xysc = (XYSeriesCollection) datasets.get(parameter);