# HG changeset patch # User Ingo Weinzierl # Date 1328863386 0 # Node ID 02ac373b6d69f2fabcf45b5047f65effd1a68d44 # Parent d0e7afb3696be1190f37e22b1e969967f78e63f2 Added chart helper function to determine the min and max bounds (x and y) for TimeSeriesCollections. flys-artifacts/trunk@4016 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r d0e7afb3696b -r 02ac373b6d69 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Feb 10 08:41:07 2012 +0000 +++ b/flys-artifacts/ChangeLog Fri Feb 10 08:43:06 2012 +0000 @@ -1,3 +1,9 @@ +2012-02-09 Ingo Weinzierl + + * src/main/java/de/intevation/flys/exports/ChartHelper.java: Added a + helper function to determine the min and max bounds (x and y) for + TimeSeriesCollections. + 2012-02-09 Ingo Weinzierl * src/main/java/de/intevation/flys/jfree/TimeBounds.java, diff -r d0e7afb3696b -r 02ac373b6d69 flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java Fri Feb 10 08:41:07 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java Fri Feb 10 08:43:06 2012 +0000 @@ -2,9 +2,16 @@ import org.jfree.data.Range; import org.jfree.data.xy.XYDataset; +import org.jfree.data.time.RegularTimePeriod; +import org.jfree.data.time.TimeSeriesCollection; +import org.jfree.data.time.TimeSeries; import org.apache.log4j.Logger; +import de.intevation.flys.jfree.Bounds; +import de.intevation.flys.jfree.DoubleBounds; +import de.intevation.flys.jfree.TimeBounds; + /** * @author Ingo Weinzierl @@ -73,6 +80,63 @@ } + public static Bounds[] getBounds(TimeSeriesCollection collection) { + int seriesCount = collection != null ? collection.getSeriesCount() : 0; + + if (seriesCount == 0) { + logger.warn("TimeSeriesCollection is empty or has no Series set."); + return null; + } + + boolean foundValue = false; + + long lowerX = Long.MAX_VALUE; + long upperX = -Long.MAX_VALUE; + + double lowerY = Double.MAX_VALUE; + double upperY = -Double.MAX_VALUE; + + for (int i = 0, m = seriesCount; i < m; i++) { + TimeSeries series = collection.getSeries(i); + + for (int j = 0, n = collection.getItemCount(i); j < n; j++) { + RegularTimePeriod rtp = series.getTimePeriod(j); + + if (rtp == null) { + continue; + } + + foundValue = true; + + long start = rtp.getFirstMillisecond(); + long end = rtp.getLastMillisecond(); + + if (start < lowerX) { + lowerX = start; + } + + if (end > upperX) { + upperX = end; + } + + double y = series.getValue(j).doubleValue(); + + lowerY = Math.min(lowerY, y); + upperY = Math.max(upperY, y); + } + } + + if (foundValue) { + return new Bounds[] { + new TimeBounds(lowerX, upperX), + new DoubleBounds(lowerY, upperY) + }; + } + + return null; + } + + /** * Expand range by percent. *