Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java @ 767:79401c871da4
Added and repaired javadoc in de.intevation.gnv.chart package.
gnv-artifacts/trunk@823 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 24 Mar 2010 14:48:55 +0000 |
parents | b98d1adee7a6 |
children | d0a39efbfd96 |
comparison
equal
deleted
inserted
replaced
766:a23ce49423d5 | 767:79401c871da4 |
---|---|
27 import de.intevation.gnv.geobackend.base.Result; | 27 import de.intevation.gnv.geobackend.base.Result; |
28 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | 28 import de.intevation.gnv.state.describedata.KeyValueDescibeData; |
29 | 29 |
30 | 30 |
31 /** | 31 /** |
32 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | 32 * This abstract class defines some methods to adjust chart settings after its |
33 * creation. | |
34 * | |
35 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
33 */ | 36 */ |
34 public abstract class AbstractXYLineChart | 37 public abstract class AbstractXYLineChart |
35 extends AbstractChart | 38 extends AbstractChart |
36 { | 39 { |
40 /** | |
41 * Constant field used to expand the area between data and chart border. Its | |
42 * value is {@value}.<br> | |
43 * A value of 0.05 equals 5 percent. | |
44 */ | |
37 public static final double LOWER_MARGIN = 0.05D; | 45 public static final double LOWER_MARGIN = 0.05D; |
46 | |
47 /** | |
48 * Constant field used to expand the area between data and chart border. Its | |
49 * value is {@value}.<br> | |
50 * A value of 0.05 equals 5 percent. | |
51 */ | |
38 public static final double UPPER_MARGIN = 0.05D; | 52 public static final double UPPER_MARGIN = 0.05D; |
39 | 53 |
54 /** | |
55 * Logger used to log with log4j. | |
56 */ | |
40 private static Logger log = Logger.getLogger(AbstractXYLineChart.class); | 57 private static Logger log = Logger.getLogger(AbstractXYLineChart.class); |
41 | 58 |
59 /** | |
60 * Field of supported colors used for lines and data points in charts. | |
61 * Colors are: {@value}. | |
62 */ | |
42 protected static Color[] COLOR = { | 63 protected static Color[] COLOR = { |
43 Color.black, Color.red, Color.green, Color.blue, Color.yellow, | 64 Color.black, Color.red, Color.green, Color.blue, Color.yellow, |
44 Color.gray, Color.orange, Color.pink, Color.cyan | 65 Color.gray, Color.orange, Color.pink, Color.cyan |
45 }; | 66 }; |
46 | 67 |
68 /** | |
69 * Static field to remember the index of the previously used color. | |
70 */ | |
47 protected static int nextColor = 0; | 71 protected static int nextColor = 0; |
48 | 72 |
73 /** | |
74 * Default <code>PlotOrientation</code>. Its value is {@value}. | |
75 */ | |
49 protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; | 76 protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; |
50 | 77 |
51 /** Map to store datasets for each parameter */ | 78 /** |
79 * Map to store datasets for each parameter. | |
80 */ | |
52 protected Map datasets; | 81 protected Map datasets; |
53 | 82 |
54 /** Map to store max ranges of each parameter (axis.setAutoRange(true) | 83 /** |
84 * Map to store max ranges of each parameter (axis.setAutoRange(true) | |
55 * doesn't seem to work */ | 85 * doesn't seem to work */ |
56 protected Map ranges; | 86 protected Map ranges; |
57 | 87 |
88 /** | |
89 * This method is called by <code>Chart</code> to bring the data into the | |
90 * right form fitting to JFreeChart objects. | |
91 */ | |
58 protected abstract void initData(); | 92 protected abstract void initData(); |
93 | |
94 /** | |
95 * Add a value of <code>row</code> to <code>series</code>. | |
96 * | |
97 * @param row <code>Result</code> Object returned from database. Contains | |
98 * a value used to add to <code>series</code> | |
99 * @param series A JFreeChart Series object. | |
100 */ | |
59 protected abstract void addValue(Result row, Series series); | 101 protected abstract void addValue(Result row, Series series); |
102 | |
103 /** | |
104 * Add <code>series</code> to JFreeChart's Dataset object currently which is | |
105 * processing. | |
106 * | |
107 * @param series Series to add. | |
108 * @param label Label used show in legend. | |
109 * @param idx Currently not used. | |
110 */ | |
60 protected abstract void addSeries(Series series, String label, int idx); | 111 protected abstract void addSeries(Series series, String label, int idx); |
112 | |
113 /** | |
114 * Abstract method which is called by <code>Chart</code> interface after | |
115 * chart creation. It turns an axis' label into a locale specific format. | |
116 * | |
117 * @param axis Axis to adjust. | |
118 * @param local java.util.Locale object used specify the format. | |
119 */ | |
61 protected abstract void localizeDomainAxis(Axis axis, Locale locale); | 120 protected abstract void localizeDomainAxis(Axis axis, Locale locale); |
121 | |
122 /** | |
123 * Abstract method to create a label for a series of parameters. | |
124 * | |
125 * @param breakPoint1 Identifier returned from database. These identifier | |
126 * are used to identify the results from database which are all stored in | |
127 * one big java.util.Collection. | |
128 * @param breakPoint2 Identifier returned from database. | |
129 * @param breakPoint3 Identifier returned from database. | |
130 * | |
131 * @return Concatinated string of parameter name and measurement. | |
132 */ | |
62 protected abstract String createSeriesName( | 133 protected abstract String createSeriesName( |
63 String breakPoint1, | 134 String breakPoint1, |
64 String breakPoint2, | 135 String breakPoint2, |
65 String breakPoint3 | 136 String breakPoint3 |
66 ); | 137 ); |
67 | 138 |
68 | 139 |
140 /** | |
141 * @see de.intevation.gnv.chart.Chart#generateChart() | |
142 */ | |
69 public JFreeChart generateChart() { | 143 public JFreeChart generateChart() { |
70 log.debug("generate XYLineChart"); | 144 log.debug("generate XYLineChart"); |
71 nextColor = 0; | 145 nextColor = 0; |
72 | 146 |
73 if (chart != null) | 147 if (chart != null) |
84 | 158 |
85 return chart; | 159 return chart; |
86 } | 160 } |
87 | 161 |
88 | 162 |
163 /** | |
164 * @see de.intevation.gnv.chart.AbstractChart#initChart() | |
165 */ | |
89 protected void initChart() { | 166 protected void initChart() { |
90 chart = ChartFactory.createXYLineChart( | 167 chart = ChartFactory.createXYLineChart( |
91 labels.getTitle(), | 168 labels.getTitle(), |
92 labels.getDomainAxisLabel(), | 169 labels.getDomainAxisLabel(), |
93 null, | 170 null, |
98 false | 175 false |
99 ); | 176 ); |
100 } | 177 } |
101 | 178 |
102 | 179 |
180 /** | |
181 * Method used to adjust the axes after chart generation. Methods for i18n | |
182 * support ({@link #localizeDomainAxis and {@link #localizeRangeAxis) are | |
183 * called and axes of this series are expanded ({@link org.jfree.data.Range#expand( | |
184 * Range, double, double}). | |
185 * | |
186 * @param seriesKey Identifier of an axis which have to be adjusted. | |
187 * @param idx Set the axis identified by <code>seriesKey</code> to position | |
188 * <code>idx</code>. | |
189 * | |
190 * @see org.jfree.data.Range#expand(Range, double, double) | |
191 */ | |
103 protected void prepareAxis(String seriesKey, int idx) { | 192 protected void prepareAxis(String seriesKey, int idx) { |
104 log.debug("prepare axis of xychart"); | 193 log.debug("prepare axis of xychart"); |
105 | 194 |
106 XYPlot plot = chart.getXYPlot(); | 195 XYPlot plot = chart.getXYPlot(); |
107 Axis xAxis = plot.getDomainAxis(); | 196 Axis xAxis = plot.getDomainAxis(); |
136 | 225 |
137 plot.mapDatasetToRangeAxis(idx, idx); | 226 plot.mapDatasetToRangeAxis(idx, idx); |
138 } | 227 } |
139 | 228 |
140 | 229 |
230 /** | |
231 * Method to adjust the rendering of a series in a chart. Line color and | |
232 * symbols of vertices are configured here. | |
233 * | |
234 * @param idx Position of the renderer. | |
235 * @param seriesCount Maximum number of series in this chart. | |
236 * @param renderLines Lines are displayed if true, otherwise they are not. | |
237 * @param renderShapes Vertices are displayed if true, otherwise they are | |
238 * not. | |
239 */ | |
141 protected void adjustRenderer( | 240 protected void adjustRenderer( |
142 int idx, | 241 int idx, |
143 int seriesCount, | 242 int seriesCount, |
144 boolean renderLines, | 243 boolean renderLines, |
145 boolean renderShapes | 244 boolean renderShapes |
167 } | 266 } |
168 plot.setRenderer(idx, renderer); | 267 plot.setRenderer(idx, renderer); |
169 } | 268 } |
170 | 269 |
171 | 270 |
271 /** | |
272 * Method to adjust the plot rendering. Disable horizontal grid lines if | |
273 * <code>plot</code> contains only a single y-axis. | |
274 * | |
275 * @param plot JFreeChart Plot object to be adjusted. | |
276 */ | |
172 protected void adjustPlot(XYPlot plot) { | 277 protected void adjustPlot(XYPlot plot) { |
173 if (plot.getRangeAxisCount() > 1) | 278 if (plot.getRangeAxisCount() > 1) |
174 plot.setRangeGridlinesVisible(false); | 279 plot.setRangeGridlinesVisible(false); |
175 } | 280 } |
176 | 281 |
177 | 282 |
283 /** | |
284 * Abstract method which is called after chart creation. It turns an | |
285 * axis' label into a locale specific format. | |
286 * | |
287 * @param axis Axis to adjust. | |
288 * @param local java.util.Locale object used specify the format. | |
289 * | |
290 * @param axis | |
291 */ | |
178 protected void localizeRangeAxis(Axis axis, Locale locale) { | 292 protected void localizeRangeAxis(Axis axis, Locale locale) { |
179 if (locale == null) | 293 if (locale == null) |
180 return; | 294 return; |
181 | 295 |
182 log.debug( | 296 log.debug( |
187 NumberFormat format = NumberFormat.getInstance(locale); | 301 NumberFormat format = NumberFormat.getInstance(locale); |
188 ((NumberAxis) axis).setNumberFormatOverride(format); | 302 ((NumberAxis) axis).setNumberFormatOverride(format); |
189 } | 303 } |
190 | 304 |
191 | 305 |
306 /** | |
307 * Return the maximum y-range of <code>dataset</code>. | |
308 * | |
309 * @param dataset Dataset to be scaned. | |
310 * | |
311 * @return JFreeChart Range object containing min and max y-value. | |
312 */ | |
192 public Range getMaxRangeOfDataset(XYDataset dataset) { | 313 public Range getMaxRangeOfDataset(XYDataset dataset) { |
193 int seriesCount = dataset.getSeriesCount(); | 314 int seriesCount = dataset.getSeriesCount(); |
194 double upper = Double.NEGATIVE_INFINITY; | 315 double upper = Double.NEGATIVE_INFINITY; |
195 double lower = Double.POSITIVE_INFINITY; | 316 double lower = Double.POSITIVE_INFINITY; |
196 | 317 |
210 | 331 |
211 return new Range(lower, upper); | 332 return new Range(lower, upper); |
212 } | 333 } |
213 | 334 |
214 | 335 |
336 /** | |
337 * Return the maximum y-range of <code>dataset</code> with a margin of | |
338 * <code>percent</code> percent. | |
339 * | |
340 * @param dataset Dataset to be scaned. | |
341 * | |
342 * @return JFreeChart Range object containing min and max y-value with a | |
343 * margin. | |
344 */ | |
215 public Range getMaxRangeOfDatasetWithMargin( | 345 public Range getMaxRangeOfDatasetWithMargin( |
216 XYDataset dataset, | 346 XYDataset dataset, |
217 double percent | 347 double percent |
218 ) { | 348 ) { |
219 Range range = getMaxRangeOfDataset(dataset); | 349 Range range = getMaxRangeOfDataset(dataset); |
223 | 353 |
224 return new Range(lower, upper); | 354 return new Range(lower, upper); |
225 } | 355 } |
226 | 356 |
227 | 357 |
358 /** | |
359 * Method to find a parameter specified by its value. | |
360 * | |
361 * @param label Search string. | |
362 * | |
363 * @return Value of a parameter with the given label. | |
364 */ | |
228 protected String findParameter(String label) { | 365 protected String findParameter(String label) { |
229 Iterator iter = parameters.iterator(); | 366 Iterator iter = parameters.iterator(); |
230 | 367 |
231 while (iter.hasNext()) { | 368 while (iter.hasNext()) { |
232 KeyValueDescibeData data = (KeyValueDescibeData) iter.next(); | 369 KeyValueDescibeData data = (KeyValueDescibeData) iter.next(); |
238 | 375 |
239 return label; | 376 return label; |
240 } | 377 } |
241 | 378 |
242 | 379 |
380 /** | |
381 * Method to find a description of a given collection of values. | |
382 * | |
383 * @param values Collection to be scaned. | |
384 * @param id Identifier and search string of the searched value. | |
385 * | |
386 * @return title | |
387 */ | |
243 protected String findValueTitle(Collection values, String id) { | 388 protected String findValueTitle(Collection values, String id) { |
244 log.debug("find description of dataset"); | 389 log.debug("find description of dataset"); |
245 | 390 |
246 if (values != null){ | 391 if (values != null){ |
247 Iterator it = values.iterator(); | 392 Iterator it = values.iterator(); |
254 } | 399 } |
255 return ""; | 400 return ""; |
256 } | 401 } |
257 | 402 |
258 | 403 |
404 /** | |
405 * Method to store the maximum range. Since we need to adjust the range of | |
406 * each range axis, we have to memorize its range - each parameter uses an | |
407 * own range axis. | |
408 * | |
409 * @param ranges Map where ranges of each axis will be stored in. | |
410 * @param value A given value on an axis. | |
411 * @param parameter Parameter name which belongs to <code>value</code>. | |
412 */ | |
259 protected void storeMaxRange(Map ranges, double value, String parameter) { | 413 protected void storeMaxRange(Map ranges, double value, String parameter) { |
260 Range range = null; | 414 Range range = null; |
261 | 415 |
262 range = ranges.containsKey(parameter) | 416 range = ranges.containsKey(parameter) |
263 ? (Range) ranges.get(parameter) | 417 ? (Range) ranges.get(parameter) |