comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonRenderer.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.jfreechart;
10
11 import java.awt.BasicStroke;
12 import java.awt.Color;
13 import java.awt.Font;
14 import java.awt.FontMetrics;
15 import java.awt.Graphics2D;
16 import java.awt.Paint;
17 import java.awt.Shape;
18
19 import java.awt.geom.GeneralPath;
20
21 import java.awt.geom.Rectangle2D.Double;
22
23 import java.awt.geom.Rectangle2D;
24
25 import java.util.ArrayList;
26
27 import org.apache.log4j.Logger;
28
29 import org.jfree.chart.axis.ValueAxis;
30
31 import org.jfree.data.Range;
32
33 import org.jfree.text.TextUtilities;
34
35 import org.jfree.ui.RectangleEdge;
36
37 /**
38 * This renderer is used to draw polygons into a Graphics object.
39 *
40 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
41 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
42 */
43 public class PolygonRenderer
44 {
45 private static Logger log = Logger.getLogger(
46 PolygonRenderer.class);
47
48 /**
49 * This interfaces describes a single method to retrieve a Paint object
50 * for a given index.
51 */
52 public interface PaintLookup {
53
54 /**
55 *
56 * @param index Index.
57 * @return Paint
58 */
59 Paint getPaint(int index);
60
61 } // interface PaintLookup
62
63 /**
64 * This class is used to generate labels for a given series.
65 */
66 public static class DefaultLabelGenerator
67 implements PolygonSeriesLabelGenerator
68 {
69 /**
70 * Construts an empty DefaultLabelGenerator.
71 */
72 public DefaultLabelGenerator() {
73 }
74
75 /**
76 *
77 * @param series A PolygonSeries.
78 * @return The label of series.
79 */
80 public String generateLabel(PolygonSeries series) {
81 Object label = series.getAttribute("label");
82 return label != null
83 ? toString(label)
84 : null;
85 }
86
87 /**
88 *
89 * @param label Object
90 * @return String representaton of label.
91 */
92 protected String toString(Object label) {
93 return label.toString();
94 }
95 } // class DefaultLabelGenerator
96
97 /**
98 * Constructor.
99 */
100 public static final PolygonSeriesLabelGenerator
101 DEFAULT_LABEL_GENERATOR_INSTANCE = new DefaultLabelGenerator();
102
103 protected PaintLookup lookup;
104
105 protected PolygonSeriesLabelGenerator labelGenerator;
106
107
108 public PolygonRenderer(PaintLookup lookup) {
109 this(lookup, null);
110 }
111
112
113 public PolygonRenderer(
114 PaintLookup lookup,
115 PolygonSeriesLabelGenerator labelGenerator
116 ) {
117 this.lookup = lookup;
118 this.labelGenerator = labelGenerator;
119 }
120
121 /**
122 * This method draws polygons of each series in <code>dataset</code> into
123 * the given graphics object. If a polygon has no attribute 'fill', we
124 * expect that it is a line, otherwise the polygon is filled.
125 *
126 */
127 public void drawPolygons(
128 Graphics2D graphics,
129 PolygonPlot plot,
130 Rectangle2D area,
131 PolygonDataset dataset
132 ) {
133 int seriesCount = dataset.getSeriesCount();
134 for (int i = 0; i < seriesCount; i++) {
135 PolygonSeries series = dataset.getSeries(i);
136 Integer colorIdx = (Integer)series.getAttribute("fill");
137
138 if (colorIdx != null) {
139 Paint paint = lookup.getPaint(colorIdx.intValue());
140 graphics.setPaint(paint != null ? paint : Color.black);
141 graphics.fill(constructShape(plot, area, series, true));
142 }
143 else {
144 Number lineWidth = (Number)series.getAttribute("line.width");
145 BasicStroke stroke = new BasicStroke(
146 lineWidth != null ? lineWidth.floatValue() : 1f);
147 graphics.setStroke(stroke);
148 graphics.setPaint(Color.black);
149 graphics.draw(constructShape(plot, area, series, false));
150 }
151 }
152 }
153
154 /**
155 * Draw labels at each item of a series in the given dataset. If the series
156 * has no label attritue, no label is drawn.
157 *
158 */
159 public void drawLabels(
160 final Graphics2D graphics,
161 final PolygonPlot plot,
162 final Rectangle2D area,
163 PolygonDataset dataset
164 ) {
165 if (labelGenerator == null) {
166 return;
167 }
168
169 final ArrayList<Rectangle2D> bboxes = new ArrayList<Rectangle2D>();
170
171 Font font = graphics.getFont();
172 font = font.deriveFont(Font.PLAIN, Math.max(8, font.getSize()-3));
173 graphics.setFont(font);
174 FontMetrics metrics = graphics.getFontMetrics(font);
175
176 for (int i = dataset.getSeriesCount()-1; i >= 0; --i) {
177 PolygonSeries series = dataset.getSeries(i);
178
179 String label = labelGenerator.generateLabel(series);
180 if (label == null) {
181 continue;
182 }
183
184 final Rectangle2D box = TextUtilities.getTextBounds(
185 label, graphics, metrics);
186
187 for (int j = series.getItemCount()-1; j >= 0; --j) {
188 final CompactXYItems ring = series.getItem(j);
189 LevelOrderIndices loi = new LevelOrderIndices(ring.size()-1);
190 Rectangle2D r = (Rectangle2D)loi.visit(
191 new LevelOrderIndices.Visitor()
192 {
193 ValueAxis da = plot.getDomainAxis();
194 ValueAxis ra = plot.getRangeAxis();
195 RectangleEdge de = plot.getDomainAxisEdge();
196 RectangleEdge re = plot.getRangeAxisEdge();
197 Rectangle2D.Double r = new Rectangle2D.Double(
198 0d, 0d, box.getWidth(), box.getHeight());
199
200 public Object visit(int index) {
201 r.x = da.valueToJava2D(ring.getX(index), area, de)
202 - 0.5*box.getWidth();
203 r.y = ra.valueToJava2D(ring.getY(index), area, re)
204 + 0.5*box.getHeight();
205
206 for (Rectangle2D b: bboxes) {
207 if (b.intersects(r)) {
208 return null;
209 }
210 }
211 return r;
212 }
213 });
214
215 if (r != null) {
216 bboxes.add(r);
217 graphics.drawString(
218 label, (float)r.getX(), (float)r.getY());
219 }
220 } // for all items in series
221 } // for all series
222 }
223
224 /**
225 * Creates a shape made up of the CompactXYItems object stored in the given
226 * series.
227 *
228 * @param plot The plot.
229 * @param area The boundary.
230 * @param series The series storing the items.
231 * @param close Specifies if the polygon should be closed or not.
232 * @return the constructed shape.
233 */
234 protected Shape constructShape(
235 PolygonPlot plot,
236 Rectangle2D area,
237 PolygonSeries series,
238 boolean close
239 ) {
240 ValueAxis da = plot.getDomainAxis();
241 ValueAxis ra = plot.getRangeAxis();
242 RectangleEdge de = plot.getDomainAxisEdge();
243 RectangleEdge re = plot.getRangeAxisEdge();
244
245 CompactXYItems [] rings = series.getRings();
246 GeneralPath path = new GeneralPath();
247
248 for (int i = 0; i < rings.length; ++i) {
249
250 CompactXYItems ring = rings[i];
251
252 double [] data = ring.getData();
253
254 if (data.length >= 2) {
255 path.moveTo(
256 (float)da.valueToJava2D(data[0], area, de),
257 (float)ra.valueToJava2D(data[1], area, re));
258 }
259 for (int j = 2; j < data.length;) {
260 path.lineTo(
261 (float)da.valueToJava2D(data[j++], area, de),
262 (float)ra.valueToJava2D(data[j++], area, re));
263 }
264 if (close) {
265 path.closePath();
266 }
267 }
268 return path;
269 }
270
271 /**
272 * Retrieves the bounding box of a dataset.
273 */
274 public Rectangle2D getBoundingBox(PolygonDataset dataset) {
275 Rectangle2D bbox = null;
276
277 for (int i = 0, N = dataset.getSeriesCount(); i < N; i++) {
278 Range domain = dataset.getSeries(i).getDomainBounds();
279 Range range = dataset.getSeries(i).getRangeBounds();
280
281 double x = domain.getLowerBound();
282 double y = range.getLowerBound();
283 double w = Math.abs(domain.getUpperBound() - x);
284 double h = Math.abs(range.getUpperBound() - y);
285
286 if (bbox == null) {
287 bbox = new Rectangle2D.Double(x, y, w, h);
288 }
289 else {
290 bbox.add(new Rectangle2D.Double(x, y, w, h));
291 }
292 }
293
294 return bbox;
295 }
296
297 /**
298 *
299 * @return the bounds of a series.
300 */
301 public Rectangle2D getBounds(PolygonSeries series) {
302
303 Range domain = series.getDomainBounds();
304 Range range = series.getRangeBounds();
305
306 return new Rectangle2D.Double(
307 domain.getLowerBound(), range.getLowerBound(),
308 domain.getUpperBound(), range.getUpperBound()
309 );
310 }
311 }
312 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org