comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java @ 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 4ac3c1c1c060
children d41c155db337
comparison
equal deleted inserted replaced
363:22229249e9fc 364:2413273f1c13
18 import org.jfree.chart.axis.AxisLocation; 18 import org.jfree.chart.axis.AxisLocation;
19 import org.jfree.chart.plot.PlotOrientation; 19 import org.jfree.chart.plot.PlotOrientation;
20 import org.jfree.chart.plot.XYPlot; 20 import org.jfree.chart.plot.XYPlot;
21 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 21 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
22 import org.jfree.chart.title.TextTitle; 22 import org.jfree.chart.title.TextTitle;
23 import org.jfree.data.xy.XYDataset;
24 import org.jfree.data.Range;
23 import org.jfree.data.general.Series; 25 import org.jfree.data.general.Series;
24 26
25 import de.intevation.gnv.geobackend.base.Result; 27 import de.intevation.gnv.geobackend.base.Result;
26 import de.intevation.gnv.state.describedata.KeyValueDescibeData; 28 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
27 29
30 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> 32 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
31 */ 33 */
32 public abstract class AbstractXYLineChart 34 public abstract class AbstractXYLineChart
33 extends AbstractChart 35 extends AbstractChart
34 { 36 {
37 public static final double LOWER_MARGIN = 0.05D;
38 public static final double UPPER_MARGIN = 0.05D;
39
35 private static Logger log = Logger.getLogger(AbstractXYLineChart.class); 40 private static Logger log = Logger.getLogger(AbstractXYLineChart.class);
36 41
37 protected static Color[] COLOR = { 42 protected static Color[] COLOR = {
38 Color.black, Color.red, Color.green, Color.blue, Color.yellow, 43 Color.black, Color.red, Color.green, Color.blue, Color.yellow,
39 Color.gray, Color.orange, Color.pink, Color.cyan 44 Color.gray, Color.orange, Color.pink, Color.cyan
41 46
42 protected static int nextColor = 0; 47 protected static int nextColor = 0;
43 48
44 protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; 49 protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL;
45 50
51 /** Map to store datasets for each parameter */
46 protected Map datasets; 52 protected Map datasets;
53
54 /** Map to store max ranges of each parameter (axis.setAutoRange(true)
55 * doesn't seem to work */
56 protected Map ranges;
47 57
48 protected abstract void initData(); 58 protected abstract void initData();
49 protected abstract void addValue(Result row, Series series); 59 protected abstract void addValue(Result row, Series series);
50 protected abstract void addSeries(Series series, String label, int idx); 60 protected abstract void addSeries(Series series, String label, int idx);
51 protected abstract void localizeDomainAxis(Axis axis, Locale locale); 61 protected abstract void localizeDomainAxis(Axis axis, Locale locale);
98 NumberAxis yAxis = new NumberAxis(seriesKey); 108 NumberAxis yAxis = new NumberAxis(seriesKey);
99 109
100 localizeDomainAxis(xAxis, locale); 110 localizeDomainAxis(xAxis, locale);
101 localizeRangeAxis(yAxis, locale); 111 localizeRangeAxis(yAxis, locale);
102 112
113 // litte workarround to adjust the max range of axes.
114 // NumberAxis.setAutoRange(true) doesn't seem to work properly.
115 Range yRange = (Range) ranges.get(seriesKey);
116 yAxis.setRange(Range.expand(yRange, LOWER_MARGIN, UPPER_MARGIN));
117 log.debug("Max Range of dataset is: " + yRange.toString());
118
103 if (seriesKey.contains("richtung")) { 119 if (seriesKey.contains("richtung")) {
104 yAxis.setTickUnit(new NumberTickUnit(30.0)); 120 yAxis.setTickUnit(new NumberTickUnit(30.0));
105 yAxis.setUpperBound(360.0); 121 yAxis.setUpperBound(360.0);
106 yAxis.setLowerBound(0.0); 122 yAxis.setLowerBound(0.0);
107 plot.setRangeAxis(idx, yAxis);
108 } 123 }
109 else { 124 else {
110 yAxis.setFixedDimension(10.0); 125 yAxis.setFixedDimension(10.0);
111 yAxis.setAutoRangeIncludesZero(false); 126 yAxis.setAutoRangeIncludesZero(false);
112 plot.setRangeAxis(idx, yAxis); 127 }
113 yAxis.configure(); 128
114 } 129 plot.setRangeAxis(idx, yAxis);
130 yAxis.configure();
115 131
116 if (idx % 2 != 0) 132 if (idx % 2 != 0)
117 plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_RIGHT); 133 plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_RIGHT);
118 else 134 else
119 plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_LEFT); 135 plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_LEFT);
120 plot.mapDatasetToRangeAxis(idx, idx);
121 } 136 }
122 137
123 138
124 protected void adjustRenderer( 139 protected void adjustRenderer(
125 int idx, 140 int idx,
170 NumberFormat format = NumberFormat.getInstance(locale); 185 NumberFormat format = NumberFormat.getInstance(locale);
171 ((NumberAxis) axis).setNumberFormatOverride(format); 186 ((NumberAxis) axis).setNumberFormatOverride(format);
172 } 187 }
173 188
174 189
190 public Range getMaxRangeOfDataset(XYDataset dataset) {
191 int seriesCount = dataset.getSeriesCount();
192 double upper = Double.NEGATIVE_INFINITY;
193 double lower = Double.POSITIVE_INFINITY;
194
195 for (int i = 0; i < seriesCount; i++) {
196 int itemCount = dataset.getItemCount(i);
197
198 for (int j = 0; j < itemCount; j++) {
199 Number num = dataset.getY(i, j);
200
201 if (num != null) {
202 double y = num.doubleValue();
203 lower = y < lower ? y : lower;
204 upper = y > upper ? y : upper;
205 }
206 }
207 }
208
209 return new Range(lower, upper);
210 }
211
212
213 public Range getMaxRangeOfDatasetWithMargin(
214 XYDataset dataset,
215 double percent
216 ) {
217 Range range = getMaxRangeOfDataset(dataset);
218 double length = range.getLength();
219 double upper = range.getUpperBound() + length /100 * percent;
220 double lower = range.getLowerBound() - length /100 * percent;
221
222 return new Range(lower, upper);
223 }
224
225
175 protected String findParameter(String label) { 226 protected String findParameter(String label) {
176 Iterator iter = parameters.iterator(); 227 Iterator iter = parameters.iterator();
177 228
178 while (iter.hasNext()) { 229 while (iter.hasNext()) {
179 KeyValueDescibeData data = (KeyValueDescibeData) iter.next(); 230 KeyValueDescibeData data = (KeyValueDescibeData) iter.next();
199 return data.getValue(); 250 return data.getValue();
200 } 251 }
201 } 252 }
202 return ""; 253 return "";
203 } 254 }
255
256
257 protected void storeMaxRange(double value, String parameter) {
258 Range range = null;
259
260 range = ranges.containsKey(parameter)
261 ? (Range) ranges.get(parameter)
262 : new Range(value, value);
263
264 double lower = range.getLowerBound();
265 double upper = range.getUpperBound();
266
267 lower = value < lower ? value : lower;
268 upper = value > upper ? value : upper;
269
270 ranges.put(parameter, new Range(lower, upper));
271 }
204 } 272 }
205 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 273 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org