Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 654:bbc966c81809
#90 Removed margins between data area border and curves.
flys-artifacts/trunk@2050 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 03 Jun 2011 10:21:13 +0000 |
parents | 67c7020f4ed3 |
children | 434146596838 |
comparison
equal
deleted
inserted
replaced
653:67c7020f4ed3 | 654:bbc966c81809 |
---|---|
13 import org.jfree.chart.axis.NumberAxis; | 13 import org.jfree.chart.axis.NumberAxis; |
14 import org.jfree.chart.axis.ValueAxis; | 14 import org.jfree.chart.axis.ValueAxis; |
15 import org.jfree.chart.plot.PlotOrientation; | 15 import org.jfree.chart.plot.PlotOrientation; |
16 import org.jfree.chart.plot.XYPlot; | 16 import org.jfree.chart.plot.XYPlot; |
17 import org.jfree.data.Range; | 17 import org.jfree.data.Range; |
18 import org.jfree.data.xy.XYDataset; | |
19 | |
20 import org.jfree.ui.RectangleInsets; | |
18 | 21 |
19 import de.intevation.flys.exports.ChartExportHelper; | 22 import de.intevation.flys.exports.ChartExportHelper; |
20 | 23 |
21 | 24 |
22 /** | 25 /** |
25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 28 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
26 */ | 29 */ |
27 public abstract class XYChartGenerator extends ChartGenerator { | 30 public abstract class XYChartGenerator extends ChartGenerator { |
28 | 31 |
29 /** The logger that is used in this generator.*/ | 32 /** The logger that is used in this generator.*/ |
30 private static Logger logger = Logger.getLogger(ChartGenerator.class); | 33 private static Logger logger = Logger.getLogger(XYChartGenerator.class); |
31 | 34 |
32 | 35 |
33 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; | 36 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; |
34 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; | 37 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; |
35 | 38 |
108 return chart; | 111 return chart; |
109 } | 112 } |
110 | 113 |
111 | 114 |
112 /** | 115 /** |
113 * Zooms the chart to the ranges specified in the attribute document. | 116 * This method zooms the plot to the specified ranges in the attribute |
117 * document or to the ranges specified by the min/max values in the | |
118 * datasets. <b>Note:</b> We determine the range manually if no zoom ranges | |
119 * are given, because JFreeCharts auto-zoom adds a margin to the left and | |
120 * right of the data area. | |
114 * | 121 * |
115 * @param plot The XYPlot. | 122 * @param plot The XYPlot. |
116 */ | 123 */ |
117 protected void zoom(XYPlot plot) { | 124 protected void zoom(XYPlot plot) { |
118 logger.debug("Zoom to specified ranges."); | 125 logger.debug("Zoom to specified ranges."); |
119 zoomX(plot); | 126 |
120 zoomY(plot); | 127 Range[] ranges = null; |
128 | |
129 boolean x = zoomX(plot); | |
130 if (!x) { | |
131 XYDataset dataset = plot.getDataset(); | |
132 | |
133 ranges = getRangesForDataset(dataset); | |
134 | |
135 logger.debug("No x range specified. Set manually: " + ranges[0]); | |
136 | |
137 ValueAxis axis = plot.getDomainAxis(); | |
138 axis.setRange(ranges[0]); | |
139 } | |
140 | |
141 boolean y = zoomY(plot); | |
142 if (!y) { | |
143 XYDataset dataset = plot.getDataset(); | |
144 | |
145 if (ranges == null) { | |
146 ranges = getRangesForDataset(dataset); | |
147 } | |
148 | |
149 logger.debug("No y range specified. Set manually: " + ranges[1]); | |
150 | |
151 ValueAxis axis = plot.getRangeAxis(); | |
152 axis.setRange(ranges[1]); | |
153 } | |
121 } | 154 } |
122 | 155 |
123 | 156 |
124 /** | 157 /** |
125 * Zooms the x axis to the range specified in the attribute document. | 158 * Zooms the x axis to the range specified in the attribute document. |
126 * | 159 * |
127 * @param plot The XYPlot. | 160 * @param plot The XYPlot. |
128 */ | 161 * |
129 protected void zoomX(XYPlot plot) { | 162 * @return true, if a zoom range was specified, otherwise false. |
163 */ | |
164 protected boolean zoomX(XYPlot plot) { | |
130 Range xrange = getDomainAxisRange(); | 165 Range xrange = getDomainAxisRange(); |
131 if (xrange != null) { | 166 if (xrange != null) { |
132 ValueAxis xaxis = plot.getDomainAxis(); | 167 ValueAxis xaxis = plot.getDomainAxis(); |
133 xaxis.setRange(xrange); | 168 xaxis.setRange(xrange); |
134 | 169 |
135 logger.debug("Zoom chart to X: " + xrange); | 170 logger.debug("Zoom chart to X: " + xrange); |
136 } | 171 |
172 return true; | |
173 } | |
174 | |
175 return false; | |
137 } | 176 } |
138 | 177 |
139 | 178 |
140 /** | 179 /** |
141 * Zooms the y axis to the range specified in the attribute document. | 180 * Zooms the y axis to the range specified in the attribute document. |
142 * | 181 * |
143 * @param plot The XYPlot. | 182 * @param plot The XYPlot. |
144 */ | 183 * |
145 protected void zoomY(XYPlot plot) { | 184 * @return true, if a zoom range was specified, otherwise false. |
185 */ | |
186 protected boolean zoomY(XYPlot plot) { | |
146 Range yrange = getValueAxisRange(); | 187 Range yrange = getValueAxisRange(); |
147 if (yrange != null) { | 188 if (yrange != null) { |
148 ValueAxis yaxis = plot.getRangeAxis(); | 189 ValueAxis yaxis = plot.getRangeAxis(); |
149 yaxis.setRange(yrange); | 190 yaxis.setRange(yrange); |
150 | 191 |
151 logger.debug("Zoom chart to Y: " + yrange); | 192 logger.debug("Zoom chart to Y: " + yrange); |
152 } | 193 |
194 return true; | |
195 } | |
196 | |
197 return false; | |
198 } | |
199 | |
200 | |
201 /** | |
202 * This method extracts the minimum and maximum values for x and y axes. | |
203 * | |
204 * @param dataset The dataset that should be observed. | |
205 * | |
206 * @return a Range[] as follows: [x-Range, y-Range]. | |
207 */ | |
208 protected Range[] getRangesForDataset(XYDataset dataset) { | |
209 double[] xr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; | |
210 double[] yr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; | |
211 | |
212 int sCount = dataset.getSeriesCount(); | |
213 | |
214 for (int i = 0; i < sCount; i++) { | |
215 int iCount = dataset.getItemCount(i); | |
216 | |
217 for (int j = 0; j < iCount; j++) { | |
218 double x = dataset.getX(i, j).doubleValue(); | |
219 double y = dataset.getY(i, j).doubleValue(); | |
220 | |
221 xr[0] = xr[0] < x ? xr[0] : x; | |
222 xr[1] = xr[1] > x ? xr[1] : x; | |
223 yr[0] = yr[0] < y ? yr[0] : y; | |
224 yr[1] = yr[1] > y ? yr[1] : y; | |
225 } | |
226 } | |
227 | |
228 // this is only required, if there are no items in the dataset. | |
229 xr[0] = xr[0] < xr[1] ? xr[0] : xr[1]; | |
230 xr[1] = xr[1] > xr[0] ? xr[1] : xr[0]; | |
231 yr[0] = yr[0] < yr[1] ? yr[0] : yr[1]; | |
232 yr[1] = yr[1] > yr[0] ? yr[1] : yr[0]; | |
233 | |
234 return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])}; | |
153 } | 235 } |
154 | 236 |
155 | 237 |
156 /** | 238 /** |
157 * Adjusts the axes of a plot. | 239 * Adjusts the axes of a plot. |
179 plot.setDomainGridlinesVisible(true); | 261 plot.setDomainGridlinesVisible(true); |
180 | 262 |
181 plot.setRangeGridlineStroke(gridStroke); | 263 plot.setRangeGridlineStroke(gridStroke); |
182 plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR); | 264 plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR); |
183 plot.setRangeGridlinesVisible(true); | 265 plot.setRangeGridlinesVisible(true); |
266 | |
267 plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); | |
184 } | 268 } |
185 | 269 |
186 | 270 |
187 protected void addSubtitles(JFreeChart chart) { | 271 protected void addSubtitles(JFreeChart chart) { |
188 // override this method in subclasses that need subtitles | 272 // override this method in subclasses that need subtitles |