comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/EnhancedLineAndShapeRenderer.java @ 2074:a026d005accd

Moved JFreeChart specific classes to de.intevation.flys.jfree and added required imports. flys-artifacts/trunk@3585 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 04 Jan 2012 14:43:48 +0000
parents
children f42b0e624e97
comparison
equal deleted inserted replaced
2073:27ada2e4243d 2074:a026d005accd
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 return isMinimumShapeVisible.getBoolean(series);
162 }
163
164
165 public void setIsMaximumShapeVisible(int series, boolean isVisible) {
166 this.isMaximumShapeVisible.setBoolean(series, isVisible);
167 }
168
169
170 public boolean isMaximumShapeVisible(int series) {
171 return isMaximumShapeVisible.getBoolean(series);
172 }
173
174
175 public boolean isMinimum(XYDataset dataset, int series, int item) {
176 return dataset.getYValue(series, item) == getMinimum(dataset, series);
177 }
178
179
180 public double getMinimum(XYDataset dataset, int series) {
181 Integer key = Integer.valueOf(series);
182 Double old = seriesMinimum.get(Integer.valueOf(series));
183
184 if (old != null) {
185 return old.doubleValue();
186 }
187
188 logger.debug("Compute minimum of Series: " + series);
189
190 double min = Double.MAX_VALUE;
191
192 for (int i = 0, n = dataset.getItemCount(series); i < n; i++) {
193 double tmpValue = dataset.getYValue(series, i);
194
195 if (tmpValue < min) {
196 min = tmpValue;
197 }
198 }
199
200 seriesMinimum.put(key, Double.valueOf(min));
201
202 return min;
203 }
204
205
206 public boolean isMaximum(XYDataset dataset, int series, int item) {
207 return dataset.getYValue(series, item) == getMaximum(dataset, series);
208 }
209
210
211 public double getMaximum(XYDataset dataset, int series) {
212 Integer key = Integer.valueOf(series);
213 Double old = seriesMaximum.get(Integer.valueOf(series));
214
215 if (old != null) {
216 return old.doubleValue();
217 }
218
219 logger.debug("Compute maximum of Series: " + series);
220
221 double max = -Double.MAX_VALUE;
222
223 for (int i = 0, n = dataset.getItemCount(series); i < n; i++) {
224 double tmpValue = dataset.getYValue(series, i);
225
226 if (tmpValue > max) {
227 max = tmpValue;
228 }
229 }
230
231 seriesMaximum.put(key, Double.valueOf(max));
232
233 return max;
234 }
235 }
236 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org