Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/EnhancedLineAndShapeRenderer.java @ 2424:092e519ff461
merged flys-artifacts/2.6.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:26 +0200 |
parents | 2b232871ba28 |
children | 74c02dbf17ca |
comparison
equal
deleted
inserted
replaced
2392:8112ec686a9a | 2424:092e519ff461 |
---|---|
1 package de.intevation.flys.jfree; | |
2 | |
3 import java.awt.Graphics2D; | |
4 import java.awt.Shape; | |
5 import java.awt.geom.Rectangle2D; | |
6 import java.util.HashMap; | |
7 import java.util.Map; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import org.jfree.chart.axis.ValueAxis; | |
12 import org.jfree.chart.entity.EntityCollection; | |
13 import org.jfree.chart.plot.CrosshairState; | |
14 import org.jfree.chart.plot.PlotOrientation; | |
15 import org.jfree.chart.plot.XYPlot; | |
16 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; | |
17 import org.jfree.data.xy.XYDataset; | |
18 import org.jfree.ui.RectangleEdge; | |
19 import org.jfree.util.BooleanList; | |
20 import org.jfree.util.ShapeUtilities; | |
21 | |
22 | |
23 public class EnhancedLineAndShapeRenderer extends XYLineAndShapeRenderer { | |
24 | |
25 private static final Logger logger = | |
26 Logger.getLogger(EnhancedLineAndShapeRenderer.class); | |
27 | |
28 protected BooleanList isMinimumShapeVisible; | |
29 protected BooleanList isMaximumShapeVisible; | |
30 | |
31 protected Map<Integer, Double> seriesMinimum; | |
32 protected Map<Integer, Double> seriesMaximum; | |
33 | |
34 | |
35 public EnhancedLineAndShapeRenderer(boolean lines, boolean shapes) { | |
36 super(lines, shapes); | |
37 this.isMinimumShapeVisible = new BooleanList(); | |
38 this.isMaximumShapeVisible = new BooleanList(); | |
39 this.seriesMinimum = new HashMap<Integer, Double>(); | |
40 this.seriesMaximum = new HashMap<Integer, Double>(); | |
41 } | |
42 | |
43 | |
44 public boolean getItemShapeVisible(XYDataset dataset, int series, int item){ | |
45 if (super.getItemShapeVisible(series, item)) { | |
46 return true; | |
47 } | |
48 | |
49 if (isMinimumShapeVisible(series) && isMinimum(dataset, series, item)) { | |
50 return true; | |
51 } | |
52 | |
53 if (isMaximumShapeVisible(series) && isMaximum(dataset, series, item)) { | |
54 return true; | |
55 } | |
56 | |
57 return false; | |
58 } | |
59 | |
60 | |
61 /** | |
62 * Overrides XYLineAndShapeRenderer.drawSecondaryPass() to call an adapted | |
63 * method getItemShapeVisible() which now takes an XYDataset. So, 99% of | |
64 * code equal the code in XYLineAndShapeRenderer. | |
65 */ | |
66 @Override | |
67 protected void drawSecondaryPass( | |
68 Graphics2D g2, | |
69 XYPlot plot, | |
70 XYDataset dataset, | |
71 int pass, | |
72 int series, | |
73 int item, | |
74 ValueAxis domainAxis, | |
75 Rectangle2D dataArea, | |
76 ValueAxis rangeAxis, | |
77 CrosshairState crosshairState, | |
78 EntityCollection entities | |
79 ) { | |
80 Shape entityArea = null; | |
81 | |
82 // get the data point... | |
83 double x1 = dataset.getXValue(series, item); | |
84 double y1 = dataset.getYValue(series, item); | |
85 if (Double.isNaN(y1) || Double.isNaN(x1)) { | |
86 return; | |
87 } | |
88 | |
89 PlotOrientation orientation = plot.getOrientation(); | |
90 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); | |
91 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); | |
92 double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); | |
93 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); | |
94 | |
95 if (getItemShapeVisible(dataset, series, item)) { | |
96 Shape shape = getItemShape(series, item); | |
97 if (orientation == PlotOrientation.HORIZONTAL) { | |
98 shape = ShapeUtilities.createTranslatedShape(shape, transY1, | |
99 transX1); | |
100 } | |
101 else if (orientation == PlotOrientation.VERTICAL) { | |
102 shape = ShapeUtilities.createTranslatedShape(shape, transX1, | |
103 transY1); | |
104 } | |
105 entityArea = shape; | |
106 if (shape.intersects(dataArea)) { | |
107 if (getItemShapeFilled(series, item)) { | |
108 if (getUseFillPaint()) { | |
109 g2.setPaint(getItemFillPaint(series, item)); | |
110 } | |
111 else { | |
112 g2.setPaint(getItemPaint(series, item)); | |
113 } | |
114 g2.fill(shape); | |
115 } | |
116 if (getDrawOutlines()) { | |
117 if (getUseOutlinePaint()) { | |
118 g2.setPaint(getItemOutlinePaint(series, item)); | |
119 } | |
120 else { | |
121 g2.setPaint(getItemPaint(series, item)); | |
122 } | |
123 g2.setStroke(getItemOutlineStroke(series, item)); | |
124 g2.draw(shape); | |
125 } | |
126 } | |
127 } | |
128 | |
129 double xx = transX1; | |
130 double yy = transY1; | |
131 if (orientation == PlotOrientation.HORIZONTAL) { | |
132 xx = transY1; | |
133 yy = transX1; | |
134 } | |
135 | |
136 // draw the item label if there is one... | |
137 if (isItemLabelVisible(series, item)) { | |
138 drawItemLabel(g2, orientation, dataset, series, item, xx, yy, | |
139 (y1 < 0.0)); | |
140 } | |
141 | |
142 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis); | |
143 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis); | |
144 updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, | |
145 rangeAxisIndex, transX1, transY1, orientation); | |
146 | |
147 // add an entity for the item, but only if it falls within the data | |
148 // area... | |
149 if (entities != null && isPointInRect(dataArea, xx, yy)) { | |
150 addEntity(entities, entityArea, dataset, series, item, xx, yy); | |
151 } | |
152 } | |
153 | |
154 | |
155 public void setIsMinimumShapeVisisble(int series, boolean isVisible) { | |
156 this.isMinimumShapeVisible.setBoolean(series, isVisible); | |
157 } | |
158 | |
159 | |
160 public boolean isMinimumShapeVisible(int series) { | |
161 if (this.isMinimumShapeVisible.size() <= series) { | |
162 return false; | |
163 } | |
164 | |
165 return isMinimumShapeVisible.getBoolean(series); | |
166 } | |
167 | |
168 | |
169 public void setIsMaximumShapeVisible(int series, boolean isVisible) { | |
170 this.isMaximumShapeVisible.setBoolean(series, isVisible); | |
171 } | |
172 | |
173 | |
174 public boolean isMaximumShapeVisible(int series) { | |
175 if (this.isMaximumShapeVisible.size() <= series) { | |
176 return false; | |
177 } | |
178 | |
179 return isMaximumShapeVisible.getBoolean(series); | |
180 } | |
181 | |
182 | |
183 public boolean isMinimum(XYDataset dataset, int series, int item) { | |
184 return dataset.getYValue(series, item) == getMinimum(dataset, series); | |
185 } | |
186 | |
187 | |
188 public double getMinimum(XYDataset dataset, int series) { | |
189 Integer key = Integer.valueOf(series); | |
190 Double old = seriesMinimum.get(key); | |
191 | |
192 if (old != null) { | |
193 return old.doubleValue(); | |
194 } | |
195 | |
196 logger.debug("Compute minimum of Series: " + series); | |
197 | |
198 double min = Double.MAX_VALUE; | |
199 | |
200 for (int i = 0, n = dataset.getItemCount(series); i < n; i++) { | |
201 double tmpValue = dataset.getYValue(series, i); | |
202 | |
203 if (tmpValue < min) { | |
204 min = tmpValue; | |
205 } | |
206 } | |
207 | |
208 seriesMinimum.put(key, Double.valueOf(min)); | |
209 | |
210 return min; | |
211 } | |
212 | |
213 | |
214 public boolean isMaximum(XYDataset dataset, int series, int item) { | |
215 return dataset.getYValue(series, item) == getMaximum(dataset, series); | |
216 } | |
217 | |
218 | |
219 public double getMaximum(XYDataset dataset, int series) { | |
220 Integer key = Integer.valueOf(series); | |
221 Double old = seriesMaximum.get(key); | |
222 | |
223 if (old != null) { | |
224 return old.doubleValue(); | |
225 } | |
226 | |
227 logger.debug("Compute maximum of Series: " + series); | |
228 | |
229 double max = -Double.MAX_VALUE; | |
230 | |
231 for (int i = 0, n = dataset.getItemCount(series); i < n; i++) { | |
232 double tmpValue = dataset.getYValue(series, i); | |
233 | |
234 if (tmpValue > max) { | |
235 max = tmpValue; | |
236 } | |
237 } | |
238 | |
239 seriesMaximum.put(key, Double.valueOf(max)); | |
240 | |
241 return max; | |
242 } | |
243 } | |
244 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |