Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java @ 2424:092e519ff461
merged flys-artifacts/2.6.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:26 +0200 |
parents | 02ac373b6d69 |
children | bece6f604899 |
comparison
equal
deleted
inserted
replaced
2392:8112ec686a9a | 2424:092e519ff461 |
---|---|
1 package de.intevation.flys.exports; | |
2 | |
3 import org.jfree.data.Range; | |
4 import org.jfree.data.xy.XYDataset; | |
5 import org.jfree.data.time.RegularTimePeriod; | |
6 import org.jfree.data.time.TimeSeriesCollection; | |
7 import org.jfree.data.time.TimeSeries; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import de.intevation.flys.jfree.Bounds; | |
12 import de.intevation.flys.jfree.DoubleBounds; | |
13 import de.intevation.flys.jfree.TimeBounds; | |
14 | |
15 | |
16 /** | |
17 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
18 */ | |
19 public class ChartHelper { | |
20 | |
21 private static final Logger logger = Logger.getLogger(ChartHelper.class); | |
22 | |
23 | |
24 /** | |
25 * This method returns the ranges of the XYDataset <i>dataset</i> as array | |
26 * with [xrange, yrange]. | |
27 * | |
28 * @param dataset The dataset which should be evaluated. | |
29 * | |
30 * @return an array with x and y ranges. | |
31 */ | |
32 public static Range[] getRanges(XYDataset dataset) { | |
33 int seriesCount = dataset != null ? dataset.getSeriesCount() : 0; | |
34 | |
35 if (seriesCount == 0) { | |
36 logger.warn("Dataset is empty or has no Series set."); | |
37 return null; | |
38 } | |
39 | |
40 boolean foundValue = false; | |
41 | |
42 double minX = Double.MAX_VALUE; | |
43 double maxX = -Double.MAX_VALUE; | |
44 double minY = Double.MAX_VALUE; | |
45 double maxY = -Double.MAX_VALUE; | |
46 | |
47 for (int i = 0, m = seriesCount; i < m; i++) { | |
48 for (int j = 0, n = dataset.getItemCount(i); j < n; j++) { | |
49 double x = dataset.getXValue(i, j); | |
50 double y = dataset.getYValue(i, j); | |
51 | |
52 if (Double.isNaN(x) || Double.isNaN(y)) { | |
53 logger.warn("Item " + j + " in Series " + i + " is broken"); | |
54 continue; | |
55 } | |
56 | |
57 foundValue = true; | |
58 | |
59 if (x < minX) { | |
60 minX = x; | |
61 } | |
62 | |
63 if (x > maxX) { | |
64 maxX = x; | |
65 } | |
66 | |
67 if (y < minY) { | |
68 minY = y; | |
69 } | |
70 | |
71 if (y > maxY) { | |
72 maxY = y; | |
73 } | |
74 } | |
75 } | |
76 | |
77 return foundValue | |
78 ? new Range[] { new Range(minX, maxX), new Range(minY, maxY) } | |
79 : null; | |
80 } | |
81 | |
82 | |
83 public static Bounds[] getBounds(TimeSeriesCollection collection) { | |
84 int seriesCount = collection != null ? collection.getSeriesCount() : 0; | |
85 | |
86 if (seriesCount == 0) { | |
87 logger.warn("TimeSeriesCollection is empty or has no Series set."); | |
88 return null; | |
89 } | |
90 | |
91 boolean foundValue = false; | |
92 | |
93 long lowerX = Long.MAX_VALUE; | |
94 long upperX = -Long.MAX_VALUE; | |
95 | |
96 double lowerY = Double.MAX_VALUE; | |
97 double upperY = -Double.MAX_VALUE; | |
98 | |
99 for (int i = 0, m = seriesCount; i < m; i++) { | |
100 TimeSeries series = collection.getSeries(i); | |
101 | |
102 for (int j = 0, n = collection.getItemCount(i); j < n; j++) { | |
103 RegularTimePeriod rtp = series.getTimePeriod(j); | |
104 | |
105 if (rtp == null) { | |
106 continue; | |
107 } | |
108 | |
109 foundValue = true; | |
110 | |
111 long start = rtp.getFirstMillisecond(); | |
112 long end = rtp.getLastMillisecond(); | |
113 | |
114 if (start < lowerX) { | |
115 lowerX = start; | |
116 } | |
117 | |
118 if (end > upperX) { | |
119 upperX = end; | |
120 } | |
121 | |
122 double y = series.getValue(j).doubleValue(); | |
123 | |
124 lowerY = Math.min(lowerY, y); | |
125 upperY = Math.max(upperY, y); | |
126 } | |
127 } | |
128 | |
129 if (foundValue) { | |
130 return new Bounds[] { | |
131 new TimeBounds(lowerX, upperX), | |
132 new DoubleBounds(lowerY, upperY) | |
133 }; | |
134 } | |
135 | |
136 return null; | |
137 } | |
138 | |
139 | |
140 /** | |
141 * Expand range by percent. | |
142 * | |
143 * @param range The range to expand. | |
144 * @param percent The percentage to expand. | |
145 * | |
146 * @param an expanded range. | |
147 */ | |
148 public static Range expandRange(Range range, double percent) { | |
149 if (range == null) { | |
150 return null; | |
151 } | |
152 | |
153 double value = range.getLowerBound(); | |
154 double expand = Math.abs(value / 100 * percent); | |
155 | |
156 return expand != 0 | |
157 ? new Range(value-expand, value+expand) | |
158 : new Range(-0.01 * percent, 0.01 * percent); | |
159 } | |
160 } | |
161 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |