comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonRenderer.java @ 422:f426f55d4f7a

Added Ingo Weinzierl's special JFreeChart classes. gnv-artifacts/trunk@470 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 21 Dec 2009 16:47:45 +0000
parents
children b624879d2902
comparison
equal deleted inserted replaced
421:fd71ee76fa58 422:f426f55d4f7a
1 package de.intevation.gnv.jfreechart;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.Paint;
6 import java.awt.Shape;
7 import java.awt.geom.GeneralPath;
8 import java.awt.geom.Rectangle2D;
9 import java.awt.geom.Rectangle2D.Double;
10
11 import org.jfree.data.Range;
12
13 /**
14 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
15 */
16 public class PolygonRenderer
17 {
18 public static final int AREA = 1;
19 public static final int LINES = 2;
20 public static final int AREA_AND_LINES = AREA | LINES;
21
22 public interface PaintLookup {
23
24 Paint getPaint(int index);
25
26 } // interface PaintLookup
27
28 protected int type;
29
30 protected PaintLookup lookup;
31
32 protected PolygonPlot plot;
33
34
35 public PolygonRenderer(PaintLookup lookup) {
36 this(lookup, AREA);
37 }
38
39 public PolygonRenderer(PaintLookup lookup, int type) {
40 this.lookup = lookup;
41 this.type = type;
42 }
43
44 public void draw(
45 Graphics2D graphics,
46 Rectangle2D rectangle,
47 PolygonDataset dataset
48 ) {
49 Rectangle2D bbox = getBoundingBox(dataset);
50
51 double sx = (double)rectangle.getWidth()/bbox.getWidth();
52 double sy = (double)rectangle.getHeight()/bbox.getHeight();
53 double tx = rectangle.getMinX();
54 double ty = rectangle.getMinY();
55
56 graphics.translate(tx, ty);
57 graphics.scale(sx, sy);
58
59 int seriesCount = dataset.getSeriesCount();
60 for (int i = 0; i < seriesCount; i++) {
61 PolygonSeries series = dataset.getSeries(i);
62 Integer colorIdx = (Integer)series.getAttribute("fill");
63
64 if (colorIdx != null) {
65 Paint paint = lookup.getPaint(colorIdx.intValue());
66 graphics.setPaint(paint != null ? paint : Color.black);
67 graphics.fill(constructShape(series));
68 }
69 else {
70 graphics.setPaint(Color.black);
71 graphics.draw(constructShape(series));
72 }
73 }
74 }
75
76 protected Shape constructShape(PolygonSeries series) {
77 CompactXYItems [] rings = series.getRings();
78 GeneralPath path = new GeneralPath();
79 for (int i = 0; i < rings.length; ++i) {
80 CompactXYItems ring = rings[i];
81 double [] data = ring.getData();
82 if (data.length >= 2) {
83 path.moveTo((float)data[0], (float)data[1]);
84 }
85 for (int j = 2; j < data.length;) {
86 float x = (float)data[j++];
87 float y = (float)data[j++];
88 path.lineTo(x, y);
89 }
90 path.closePath();
91 }
92 return path;
93 }
94
95 public Rectangle2D getBoundingBox(PolygonDataset dataset) {
96 Rectangle2D bbox = null;
97
98 for (int i = 0, N = dataset.getSeriesCount(); i < N; i++) {
99 Range domain = dataset.getSeries(i).getDomainBounds();
100 Range range = dataset.getSeries(i).getRangeBounds();
101
102 double x = domain.getLowerBound();
103 double y = range.getLowerBound();
104 double w = Math.abs(domain.getUpperBound() - x);
105 double h = Math.abs(range.getUpperBound() - y);
106
107 if (bbox == null) {
108 bbox = new Rectangle2D.Double(x, y, w, h);
109 }
110 else {
111 bbox.add(new Rectangle2D.Double(x, y, w, h));
112 }
113 }
114
115 return bbox;
116 }
117
118 public Rectangle2D getBounds(PolygonSeries series) {
119
120 Range domain = series.getDomainBounds();
121 Range range = series.getRangeBounds();
122
123 return new Rectangle2D.Double(
124 domain.getLowerBound(), range.getLowerBound(),
125 domain.getUpperBound(), range.getUpperBound()
126 );
127 }
128 }
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org