comparison artifacts/src/main/java/org/dive4elements/river/exports/ChartHelper.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/exports/ChartHelper.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.exports;
2
3 import org.jfree.data.Range;
4 import org.jfree.data.xy.XYDataset;
5 import org.jfree.data.xy.XYSeriesCollection;
6 import org.jfree.data.time.RegularTimePeriod;
7 import org.jfree.data.time.TimeSeriesCollection;
8 import org.jfree.data.time.TimeSeries;
9
10 import org.apache.log4j.Logger;
11
12 import org.dive4elements.river.jfree.Bounds;
13 import org.dive4elements.river.jfree.DoubleBounds;
14 import org.dive4elements.river.jfree.TimeBounds;
15
16
17 /**
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
19 */
20 public class ChartHelper {
21
22 private static final Logger logger = Logger.getLogger(ChartHelper.class);
23
24
25 /**
26 * This method returns the ranges of the XYDataset <i>dataset</i> as array
27 * with [xrange, yrange].
28 *
29 * @param dataset The dataset which should be evaluated.
30 *
31 * @return an array with x and y ranges.
32 */
33 public static Bounds[] getBounds(XYSeriesCollection dataset) {
34 int seriesCount = dataset != null ? dataset.getSeriesCount() : 0;
35
36 if (seriesCount == 0) {
37 logger.warn("Dataset is empty or has no Series set.");
38 return null;
39 }
40
41 boolean foundValue = false;
42
43 double minX = Double.MAX_VALUE;
44 double maxX = -Double.MAX_VALUE;
45 double minY = Double.MAX_VALUE;
46 double maxY = -Double.MAX_VALUE;
47
48 for (int i = 0, m = seriesCount; i < m; i++) {
49 for (int j = 0, n = dataset.getItemCount(i); j < n; j++) {
50 double x = dataset.getXValue(i, j);
51 double y = dataset.getYValue(i, j);
52
53 if (Double.isNaN(x) || Double.isNaN(y)) {
54 logger.warn("Item " + j + " in Series " + i + " is broken");
55 continue;
56 }
57
58 foundValue = true;
59
60 if (x < minX) {
61 minX = x;
62 }
63
64 if (x > maxX) {
65 maxX = x;
66 }
67
68 if (y < minY) {
69 minY = y;
70 }
71
72 if (y > maxY) {
73 maxY = y;
74 }
75 }
76 }
77
78 return foundValue
79 ? new Bounds[] {
80 new DoubleBounds(minX, maxX),
81 new DoubleBounds(minY, maxY) }
82 : null;
83 }
84
85
86 public static Bounds[] getBounds(XYDataset dataset) {
87 if (dataset instanceof XYSeriesCollection) {
88 return getBounds((XYSeriesCollection) dataset);
89 }
90 else if(dataset instanceof TimeSeriesCollection) {
91 return getBounds((TimeSeriesCollection) dataset);
92 }
93 else {
94 logger.warn("Unknown XYDataset instance: " + dataset.getClass());
95 return null;
96 }
97 }
98
99
100 public static Bounds[] getBounds(TimeSeriesCollection collection) {
101 int seriesCount = collection != null ? collection.getSeriesCount() : 0;
102
103 if (seriesCount == 0) {
104 logger.warn("TimeSeriesCollection is empty or has no Series set.");
105 return null;
106 }
107
108 boolean foundValue = false;
109
110 long lowerX = Long.MAX_VALUE;
111 long upperX = -Long.MAX_VALUE;
112
113 double lowerY = Double.MAX_VALUE;
114 double upperY = -Double.MAX_VALUE;
115
116 for (int i = 0, m = seriesCount; i < m; i++) {
117 TimeSeries series = collection.getSeries(i);
118
119 for (int j = 0, n = collection.getItemCount(i); j < n; j++) {
120 RegularTimePeriod rtp = series.getTimePeriod(j);
121
122 if (rtp == null) {
123 continue;
124 }
125
126 foundValue = true;
127
128 long start = rtp.getFirstMillisecond();
129 long end = rtp.getLastMillisecond();
130
131 if (start < lowerX) {
132 lowerX = start;
133 }
134
135 if (end > upperX) {
136 upperX = end;
137 }
138
139 double y = series.getValue(j).doubleValue();
140
141 lowerY = Math.min(lowerY, y);
142 upperY = Math.max(upperY, y);
143 }
144 }
145
146 if (foundValue) {
147 return new Bounds[] {
148 new TimeBounds(lowerX, upperX),
149 new DoubleBounds(lowerY, upperY)
150 };
151 }
152
153 return null;
154 }
155
156
157 /**
158 * Expand bounds by percent.
159 *
160 * @param bounds The bounds to expand.
161 * @param percent The percentage to expand.
162 *
163 * @return a new, expanded bounds.
164 */
165 public static Bounds expandBounds(Bounds bounds, double percent) {
166 if (bounds == null) {
167 return null;
168 }
169
170 double value = (Double) bounds.getLower();
171 double expand = Math.abs(value / 100 * percent);
172
173 return expand != 0
174 ? new DoubleBounds(value-expand, value+expand)
175 : new DoubleBounds(-0.01 * percent, 0.01 * percent);
176 }
177
178
179 /**
180 * Expand range by percent.
181 *
182 * @param range The range to expand.
183 * @param percent The percentage to expand.
184 *
185 * @return a new, expanded range.
186 */
187 public static Range expandRange(Range range, double percent) {
188 if (range == null) {
189 return null;
190 }
191
192 double value = range.getLowerBound();
193 double expand = Math.abs(value / 100 * percent);
194
195 return expand != 0
196 ? new Range(value-expand, value+expand)
197 : new Range(-0.01 * percent, 0.01 * percent);
198 }
199 }
200 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org