comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonRenderer.java @ 451:bc5901bb4525

Use JFreeCharts ValueAxis.valueToJava2D() to transform data values to Java2D space in polygon plot. gnv-artifacts/trunk@499 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 04 Jan 2010 12:42:16 +0000
parents 20a480753ff9
children 9a828e5a2390
comparison
equal deleted inserted replaced
450:20a480753ff9 451:bc5901bb4525
11 import java.awt.Font; 11 import java.awt.Font;
12 12
13 import java.awt.geom.GeneralPath; 13 import java.awt.geom.GeneralPath;
14 import java.awt.geom.Rectangle2D; 14 import java.awt.geom.Rectangle2D;
15 import java.awt.geom.Rectangle2D.Double; 15 import java.awt.geom.Rectangle2D.Double;
16 import java.awt.geom.AffineTransform;
17 16
18 import org.jfree.data.Range; 17 import org.jfree.data.Range;
19 18
20 import org.jfree.chart.axis.ValueAxis; 19 import org.jfree.chart.axis.ValueAxis;
21 20
77 this.labelGenerator = labelGenerator; 76 this.labelGenerator = labelGenerator;
78 } 77 }
79 78
80 public void drawPolygons( 79 public void drawPolygons(
81 Graphics2D graphics, 80 Graphics2D graphics,
82 Rectangle2D rectangle, 81 PolygonPlot plot,
82 Rectangle2D area,
83 PolygonDataset dataset 83 PolygonDataset dataset
84 ) { 84 ) {
85 Rectangle2D bbox = getBoundingBox(dataset);
86
87 double sx = (double)rectangle.getWidth()/bbox.getWidth();
88 double sy = (double)rectangle.getHeight()/bbox.getHeight();
89 double tx = rectangle.getMinX();
90 double ty = rectangle.getMinY();
91
92 // XXX: Little hack to draw correctly if data is
93 // below 0 in y direction.
94 if (bbox.getMinY() <= 0d && bbox.getMaxY() <= 0d) {
95 sy = -sy; // mirror
96 }
97
98 AffineTransform xform = graphics.getTransform();
99
100 graphics.translate(tx, ty);
101 graphics.scale(sx, sy);
102
103 int seriesCount = dataset.getSeriesCount(); 85 int seriesCount = dataset.getSeriesCount();
104 for (int i = 0; i < seriesCount; i++) { 86 for (int i = 0; i < seriesCount; i++) {
105 PolygonSeries series = dataset.getSeries(i); 87 PolygonSeries series = dataset.getSeries(i);
106 Integer colorIdx = (Integer)series.getAttribute("fill"); 88 Integer colorIdx = (Integer)series.getAttribute("fill");
107 89
108 if (colorIdx != null) { 90 if (colorIdx != null) {
109 Paint paint = lookup.getPaint(colorIdx.intValue()); 91 Paint paint = lookup.getPaint(colorIdx.intValue());
110 graphics.setPaint(paint != null ? paint : Color.black); 92 graphics.setPaint(paint != null ? paint : Color.black);
111 graphics.fill(constructShape(series, true)); 93 graphics.fill(constructShape(plot, area, series, true));
112 } 94 }
113 else { 95 else {
114 Number lineWidth = (Number)series.getAttribute("line.width"); 96 Number lineWidth = (Number)series.getAttribute("line.width");
115 BasicStroke stroke = new BasicStroke( 97 BasicStroke stroke = new BasicStroke(
116 lineWidth != null ? lineWidth.floatValue() : 1f); 98 lineWidth != null ? lineWidth.floatValue() : 1f);
117 graphics.setStroke(stroke); 99 graphics.setStroke(stroke);
118 graphics.setPaint(Color.black); 100 graphics.setPaint(Color.black);
119 graphics.draw(constructShape(series, false)); 101 graphics.draw(constructShape(plot, area, series, false));
120 } 102 }
121 } 103 }
122
123 graphics.setTransform(xform);
124 } 104 }
125 105
126 public void drawLabels( 106 public void drawLabels(
127 final Graphics2D graphics, 107 final Graphics2D graphics,
128 final PolygonPlot plot, 108 final PolygonPlot plot,
186 } 166 }
187 } // for all items in series 167 } // for all items in series
188 } // for all series 168 } // for all series
189 } 169 }
190 170
191 protected Shape constructShape(PolygonSeries series, boolean close) { 171 protected Shape constructShape(
172 PolygonPlot plot,
173 Rectangle2D area,
174 PolygonSeries series,
175 boolean close
176 ) {
177 ValueAxis da = plot.getDomainAxis();
178 ValueAxis ra = plot.getRangeAxis();
179 RectangleEdge de = plot.getDomainAxisEdge();
180 RectangleEdge re = plot.getRangeAxisEdge();
181
192 CompactXYItems [] rings = series.getRings(); 182 CompactXYItems [] rings = series.getRings();
193 GeneralPath path = new GeneralPath(); 183 GeneralPath path = new GeneralPath();
184
194 for (int i = 0; i < rings.length; ++i) { 185 for (int i = 0; i < rings.length; ++i) {
186
195 CompactXYItems ring = rings[i]; 187 CompactXYItems ring = rings[i];
188
196 double [] data = ring.getData(); 189 double [] data = ring.getData();
190
197 if (data.length >= 2) { 191 if (data.length >= 2) {
198 path.moveTo((float)data[0], (float)data[1]); 192 path.moveTo(
193 (float)da.valueToJava2D(data[0], area, de),
194 (float)ra.valueToJava2D(data[1], area, re));
199 } 195 }
200 for (int j = 2; j < data.length;) { 196 for (int j = 2; j < data.length;) {
201 float x = (float)data[j++]; 197 path.lineTo(
202 float y = (float)data[j++]; 198 (float)da.valueToJava2D(data[j++], area, de),
203 path.lineTo(x, y); 199 (float)ra.valueToJava2D(data[j++], area, re));
204 } 200 }
205 if (close) { 201 if (close) {
206 path.closePath(); 202 path.closePath();
207 } 203 }
208 } 204 }

http://dive4elements.wald.intevation.org