Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartHelper.java @ 2241:2b232871ba28
New helper functions in ChartHelper and improvements in EnhancedLineAndShapeRenderer.
flys-artifacts/trunk@3889 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 03 Feb 2012 09:35:46 +0000 |
parents | 60615235e951 |
children | 594885703687 |
comparison
equal
deleted
inserted
replaced
2240:e9173de1026c | 2241:2b232871ba28 |
---|---|
1 package de.intevation.flys.exports; | 1 package de.intevation.flys.exports; |
2 | 2 |
3 import org.jfree.data.Range; | 3 import org.jfree.data.Range; |
4 import org.jfree.data.xy.XYDataset; | 4 import org.jfree.data.xy.XYDataset; |
5 | |
6 import org.apache.log4j.Logger; | |
5 | 7 |
6 | 8 |
7 /** | 9 /** |
8 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 10 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
9 */ | 11 */ |
10 public class ChartHelper { | 12 public class ChartHelper { |
13 | |
14 private static final Logger logger = Logger.getLogger(ChartHelper.class); | |
15 | |
11 | 16 |
12 /** | 17 /** |
13 * This method returns the ranges of the XYDataset <i>dataset</i> as array | 18 * This method returns the ranges of the XYDataset <i>dataset</i> as array |
14 * with [xrange, yrange]. | 19 * with [xrange, yrange]. |
15 * | 20 * |
16 * @param dataset The dataset which should be evaluated. | 21 * @param dataset The dataset which should be evaluated. |
17 * | 22 * |
18 * @return an array with x and y ranges. | 23 * @return an array with x and y ranges. |
19 */ | 24 */ |
20 public static Range[] getRanges(XYDataset dataset) { | 25 public static Range[] getRanges(XYDataset dataset) { |
21 if (dataset == null) { | 26 int seriesCount = dataset != null ? dataset.getSeriesCount() : 0; |
27 | |
28 if (seriesCount == 0) { | |
29 logger.warn("Dataset is empty or has no Series set."); | |
22 return null; | 30 return null; |
23 } | 31 } |
32 | |
33 boolean foundValue = false; | |
24 | 34 |
25 double minX = Double.MAX_VALUE; | 35 double minX = Double.MAX_VALUE; |
26 double maxX = -Double.MAX_VALUE; | 36 double maxX = -Double.MAX_VALUE; |
27 double minY = Double.MAX_VALUE; | 37 double minY = Double.MAX_VALUE; |
28 double maxY = -Double.MAX_VALUE; | 38 double maxY = -Double.MAX_VALUE; |
29 | 39 |
30 for (int i = 0, m = dataset.getSeriesCount(); i < m; i++) { | 40 for (int i = 0, m = seriesCount; i < m; i++) { |
31 for (int j = 0, n = dataset.getItemCount(i); j < n; j++) { | 41 for (int j = 0, n = dataset.getItemCount(i); j < n; j++) { |
32 double x = dataset.getXValue(i, j); | 42 double x = dataset.getXValue(i, j); |
33 double y = dataset.getYValue(i, j); | 43 double y = dataset.getYValue(i, j); |
44 | |
45 if (Double.isNaN(x) || Double.isNaN(y)) { | |
46 logger.warn("Item " + j + " in Series " + i + " is broken"); | |
47 continue; | |
48 } | |
49 | |
50 foundValue = true; | |
34 | 51 |
35 if (x < minX) { | 52 if (x < minX) { |
36 minX = x; | 53 minX = x; |
37 } | 54 } |
38 | 55 |
48 maxY = y; | 65 maxY = y; |
49 } | 66 } |
50 } | 67 } |
51 } | 68 } |
52 | 69 |
53 return new Range[] { new Range(minX, maxX), new Range(minY, maxY) }; | 70 return foundValue |
71 ? new Range[] { new Range(minX, maxX), new Range(minY, maxY) } | |
72 : null; | |
73 } | |
74 | |
75 | |
76 /** | |
77 * Expand range by percent. | |
78 * | |
79 * @param range The range to expand. | |
80 * @param percent The percentage to expand. | |
81 * | |
82 * @param an expanded range. | |
83 */ | |
84 public static Range expandRange(Range range, double percent) { | |
85 if (range == null) { | |
86 return null; | |
87 } | |
88 | |
89 double value = range.getLowerBound(); | |
90 double expand = Math.abs(value / 100 * percent); | |
91 | |
92 return expand != 0 | |
93 ? new Range(value-expand, value+expand) | |
94 : new Range(-0.01 * percent, 0.01 * percent); | |
54 } | 95 } |
55 } | 96 } |
56 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 97 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |