comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalCrossSectionChart.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 31f63a14a2c3
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.chart;
2
3 import de.intevation.gnv.jfreechart.PolygonDataset;
4 import de.intevation.gnv.jfreechart.PolygonPlot;
5 import de.intevation.gnv.jfreechart.PolygonRenderer;
6 import de.intevation.gnv.jfreechart.PolygonSeries;
7
8 import de.intevation.gnv.math.AttributedXYColumns;
9
10 import de.intevation.gnv.raster.Palette;
11
12 import java.awt.Color;
13 import java.awt.Paint;
14
15 import java.text.NumberFormat;
16
17 import java.util.HashSet;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import org.jfree.chart.JFreeChart;
22
23 import org.jfree.chart.axis.NumberAxis;
24 import org.jfree.chart.axis.SymbolAxis;
25 import org.jfree.chart.axis.ValueAxis;
26
27 import org.jfree.chart.plot.PlotOrientation;
28
29 import org.jfree.chart.renderer.LookupPaintScale;
30
31 import org.jfree.chart.title.PaintScaleLegend;
32 import org.jfree.chart.title.TextTitle;
33
34 import org.jfree.data.Range;
35
36 import org.jfree.ui.RectangleEdge;
37 import org.jfree.ui.RectangleInsets;
38
39 /**
40 * This class represents a 2D chart containing polygon data.
41 *
42 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
43 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
44 */
45 public class VerticalCrossSectionChart
46 implements Chart
47 {
48 /**
49 * Lookup class for retrieving a color which corresponds to a specific
50 * integer value.
51 */
52 public static final class PalettePaintLookup
53 implements PolygonRenderer.PaintLookup
54 {
55 /**
56 * Object storing information about value ranges and its coresponding
57 * colors and descriptions.
58 */
59 private Palette palette;
60
61 /**
62 * Map containing some special <code>Paint</code> like ground fillcolor.
63 */
64 private Map<Integer, Paint> special;
65
66 /**
67 * Constructor
68 *
69 * @param palette See {@link #palette}
70 */
71 public PalettePaintLookup(Palette palette) {
72 this(palette, null);
73 }
74
75 /**
76 * Constructor
77 *
78 * @param palette See {@link #palette}
79 * @param special See {@link #special}
80 */
81 public PalettePaintLookup(
82 Palette palette,
83 Map<Integer, Paint> special
84 ) {
85 this.palette = palette;
86 this.special = special;
87 }
88
89 /**
90 * @param index Index of a <code>Paint</code>
91 *
92 * @return <code>Paint</code> object for a given index.
93 */
94 public Paint getPaint(int index) {
95 if (special != null) {
96 Paint paint = special.get(index);
97 if (paint != null) {
98 return paint;
99 }
100 }
101 return index < 0
102 ? Color.black
103 : palette.getColor(index);
104 }
105 } // class PalettePaintLookup
106
107 /**
108 * This class is used to turn labels which represent a number into a
109 * specific format.
110 */
111 public static class LocalizedLabelGenerator
112 extends PolygonRenderer.DefaultLabelGenerator
113 {
114 /**
115 * <code>NumberFormat</code> which is used to turn a number into a
116 * specific format.
117 */
118 protected NumberFormat format;
119
120 /**
121 * Constructor
122 */
123 public LocalizedLabelGenerator() {
124 }
125
126 /**
127 * Constructor
128 *
129 * @param format See {@link #format}
130 */
131 public LocalizedLabelGenerator(NumberFormat format) {
132 this.format = format;
133 }
134
135 /**
136 * If label is a <code>Number</code>, it is turned into a format
137 * specified by {@link #format}.
138 *
139 * @param label Label to format.
140 *
141 * @return String representation of label.
142 */
143 @Override
144 protected String toString(Object label) {
145 return label instanceof Number
146 ? format.format(((Number)label).doubleValue())
147 : super.toString(label);
148 }
149 } // class LocalizedLabelGenerator
150
151 public static double MARGIN_TOP = 0.05d;
152 public static double MARGIN_BOTTOM = 0.00d;
153 public static double MARGIN_LEFT = 0.00d;
154 public static double MARGIN_RIGHT = 0.05d;
155
156 /**
157 * JFreeChart object stored at this place after chart creation.
158 */
159 protected JFreeChart chart;
160
161 /**
162 * Stores {@link de.intevation.gnv.jfreechart.PolygonDataset} which is used
163 * to create a vertical cross chart.
164 */
165 protected AttributedXYColumns columns;
166
167 /**
168 * Map which contains colors to fill polygons draw by this chart.
169 */
170 protected Map<Integer, Paint> special;
171
172 /**
173 * Object used to map value intervals to specific colors and descriptions.
174 */
175 protected Palette palette;
176
177 /**
178 * Locale object used for i18n support.
179 */
180 protected Locale locale;
181
182 /**
183 * Labels for decorating the chart.
184 */
185 protected ChartLabels labels;
186
187 /**
188 * Default Constructor
189 */
190 public VerticalCrossSectionChart() {
191 }
192
193 /**
194 * Constructor for VerticalCrossSectionChart creation.
195 *
196 * @param columns See {@link #columns}
197 * @param palette See {@link #palette}
198 * @param locale See {@link #locale}
199 * @param labels See {@link #labels}
200 */
201 public VerticalCrossSectionChart(
202 AttributedXYColumns columns,
203 Palette palette,
204 Locale locale,
205 ChartLabels labels
206 ) {
207 this(columns, palette, null, locale, labels);
208 }
209
210 /**
211 * Constructor for VerticalCrossSectionChart creation.
212 *
213 * @param columns See {@link #columns}
214 * @param palette See {@link #palette}
215 * @param special See {@link #special}
216 * @param locale See {@link #locale}
217 * @param labels See {@link #labels}
218 */
219 public VerticalCrossSectionChart(
220 AttributedXYColumns columns,
221 Palette palette,
222 Map<Integer, Paint> special,
223 Locale locale,
224 ChartLabels labels
225 ) {
226 this.columns = columns;
227 this.palette = palette;
228 this.special = special;
229 this.locale = locale;
230 this.labels = labels;
231 }
232
233 /**
234 * This method is used to create a JFreeChart from this object. A 2D plot is
235 * drawn and a legend panel is created containing each value range.
236 *
237 * @return JFreeChart object
238 */
239 protected JFreeChart createChart() {
240
241 boolean legendB = false;
242 boolean tooltips = false;
243 boolean urls = false;
244
245 PlotOrientation po = PlotOrientation.HORIZONTAL;
246 PolygonDataset data = columns.getPolygonDataset();
247
248 HashSet<Integer> usedColors = new HashSet<Integer>();
249
250 for (int i = data.getSeriesCount()-1; i >= 0; --i) {
251 PolygonSeries ps = data.getSeries(i);
252 Integer fill = (Integer)ps.getAttribute("fill");
253 if (fill != null
254 && (special != null && !special.containsKey(fill))) {
255 usedColors.add(fill);
256 }
257 }
258
259 NumberFormat format = NumberFormat.getInstance(locale);
260 format.setMinimumFractionDigits(0);
261 format.setMaximumFractionDigits(2);
262
263 PolygonRenderer renderer = new PolygonRenderer(
264 new PalettePaintLookup(palette, special),
265 new LocalizedLabelGenerator(format));
266
267 ValueAxis domainAxis = new NumberAxis(this.labels.getDomainAxisLabel());
268 ValueAxis rangeAxis = new NumberAxis(this.labels.getRangeAxisLabel());
269
270 PolygonPlot plot = new PolygonPlot(
271 data,
272 renderer,
273 domainAxis,
274 rangeAxis,
275 null);
276
277 plot.setOutlinePaint(Color.WHITE);
278
279 String [] labels = new String[usedColors.size()];
280
281 int colors = palette.getSize();
282 LookupPaintScale lookupPaint =
283 new LookupPaintScale(-0.5d, labels.length-0.5d, Color.white);
284
285 Color color = null;
286
287 for (int i = 0, j = labels.length-1; i < colors && j >= 0; i++) {
288 if (usedColors.contains(i)) {
289 Palette.Entry entry = palette.getEntryByIndex(i);
290 color = entry.getColor();
291 labels[j] = entry.getDescription();
292 lookupPaint.add(j-0.5d, color);
293 --j;
294 }
295 }
296
297 JFreeChart chart = new JFreeChart(
298 this.labels.getTitle(),
299 JFreeChart.DEFAULT_TITLE_FONT,
300 plot,
301 legendB);
302
303 chart.removeLegend();
304 chart.addSubtitle(new TextTitle(this.labels.getSubtitle()));
305
306 SymbolAxis scale = new SymbolAxis(this.labels.getParameterName(), labels);
307 scale.setRange(-1.5d, labels.length+0.5d);
308 scale.setGridBandsVisible(false);
309 scale.setPlot(plot);
310
311 PaintScaleLegend legend = new PaintScaleLegend(
312 lookupPaint, scale);
313 legend.setMargin(new RectangleInsets(3d, 10d, 3d, 10d));
314 legend.setPosition(RectangleEdge.LEFT);
315 legend.setAxisOffset(5d);
316
317 chart.addSubtitle(legend);
318
319 // XXX Workaround, because Axes labels are cut at the
320 // left/right/top/bottom edge. The following lines add a white border
321 // between data area and plot border.
322 // see http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=22177&start=0&hilit=axis+labels+cut
323 ValueAxis xAxis = plot.getDomainAxis();
324 Range xRange = xAxis.getRange();
325 xRange = Range.expand(xRange, MARGIN_LEFT, MARGIN_RIGHT);
326 xAxis.setRange(xRange);
327 plot.setDomainAxis(xAxis);
328
329 ValueAxis yAxis = plot.getRangeAxis();
330 Range yRange = yAxis.getRange();
331 yRange = Range.expand(yRange, MARGIN_BOTTOM, MARGIN_TOP);
332 yAxis.setRange(yRange);
333 plot.setRangeAxis(yAxis);
334
335 chart.setPadding(new RectangleInsets(10d, 10d, 10d, 10d));
336
337 return chart;
338 }
339
340 /**
341 * @see de.intevation.gnv.chart.Chart#generateChart()
342 */
343 public JFreeChart generateChart() {
344 if (chart == null) {
345 chart = createChart();
346 }
347
348 return chart;
349 }
350
351 /**
352 * Set the background paint of {@link #chart}.
353 * @param paint
354 */
355 public void setBackgroundPaint(Paint paint) {
356 chart.setBackgroundPaint(paint);
357 }
358 }
359 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org