Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java @ 171:7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
gnv-artifacts/trunk@208 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 09 Oct 2009 07:54:48 +0000 |
parents | f07637f96312 |
children | 34f233123a6f |
comparison
equal
deleted
inserted
replaced
170:7be22e76c270 | 171:7fb9441dd8af |
---|---|
15 */ | 15 */ |
16 package de.intevation.gnv.chart; | 16 package de.intevation.gnv.chart; |
17 | 17 |
18 import java.awt.Color; | 18 import java.awt.Color; |
19 import java.awt.Font; | 19 import java.awt.Font; |
20 import java.awt.Transparency; | |
20 import java.awt.image.BufferedImage; | 21 import java.awt.image.BufferedImage; |
21 import java.io.IOException; | 22 import java.io.IOException; |
22 import java.io.OutputStream; | 23 import java.io.OutputStream; |
23 import java.util.Collection; | 24 import java.util.Collection; |
24 import java.util.Date; | 25 import java.util.Date; |
57 * @see | 58 * @see |
58 * @since 06.12.2007 17:25:59 | 59 * @since 06.12.2007 17:25:59 |
59 */ | 60 */ |
60 public class ChartFactory { | 61 public class ChartFactory { |
61 | 62 |
62 /** | 63 /** |
63 * Default Logging instance | 64 * Default Logging instance |
64 */ | 65 */ |
65 private static Logger sLogger = Logger.getLogger(ChartFactory.class); | 66 private static Logger sLogger = Logger.getLogger(ChartFactory.class); |
66 private static boolean sDebug = sLogger.isDebugEnabled(); | 67 private static boolean sDebug = sLogger.isDebugEnabled(); |
67 | 68 |
68 | 69 public synchronized void createSimpleTimeSeriesChart( |
69 public synchronized void createSimpleTimeSeriesChart(ChartLabels pLabels, ChartStyle pStyle, Collection<KeyValueDescibeData> parameters, Collection<KeyValueDescibeData> measurements, OutputStream outputStream, Collection<Result> resultSet) throws IOException, TechnicalChartException { | 70 ChartLabels pLabels, |
70 if (sDebug) | 71 ChartStyle pStyle, |
71 sLogger.debug("createSimpleTimeSeriesChart()"); | 72 Collection<KeyValueDescibeData> parameters, |
72 int lLowerLevel = Integer.MIN_VALUE; | 73 Collection<KeyValueDescibeData> measurements, |
73 int lUpperLevel = Integer.MAX_VALUE; | 74 OutputStream outputStream, |
74 if (pStyle.isUseUpperDataLevel() | 75 Collection<Result> resultSet) |
75 && pStyle.getUpperLevel() < Integer.MAX_VALUE) { | 76 throws IOException, |
76 lUpperLevel = pStyle.getUpperLevel(); | 77 TechnicalChartException { |
77 } | 78 if (sDebug) |
78 if (pStyle.isUseLowerDataLevel() | 79 sLogger.debug("createSimpleTimeSeriesChart()"); |
79 && pStyle.getLowerLevel() > Integer.MIN_VALUE) { | 80 int lLowerLevel = Integer.MIN_VALUE; |
80 lLowerLevel = pStyle.getLowerLevel(); | 81 int lUpperLevel = Integer.MAX_VALUE; |
81 } | 82 if (pStyle.isUseUpperDataLevel() |
82 if (sDebug) | 83 && pStyle.getUpperLevel() < Integer.MAX_VALUE) { |
83 sLogger.debug(" vor createDataset()"); | 84 lUpperLevel = pStyle.getUpperLevel(); |
84 XYDataset lSet = this.createDataset(resultSet, lUpperLevel, | 85 } |
85 lLowerLevel,parameters,measurements); | 86 if (pStyle.isUseLowerDataLevel() |
86 if (sDebug) | 87 && pStyle.getLowerLevel() > Integer.MIN_VALUE) { |
87 sLogger.debug(" nach createDataset()"); | 88 lLowerLevel = pStyle.getLowerLevel(); |
88 final Color[] color = {Color.black, Color.red, Color.green, Color.blue}; | 89 } |
89 DateAxis domain = new DateAxis(pLabels.getDomainAxisLabel()); | 90 if (sDebug) |
91 sLogger.debug(" vor createDataset()"); | |
92 XYDataset lSet = this.createDataset(resultSet, lUpperLevel, | |
93 lLowerLevel, parameters, measurements); | |
94 if (sDebug) | |
95 sLogger.debug(" nach createDataset()"); | |
96 final Color[] color = { Color.black, Color.red, Color.green, Color.blue }; | |
97 DateAxis domain = new DateAxis(pLabels.getDomainAxisLabel()); | |
90 NumberAxis axis; | 98 NumberAxis axis; |
91 StandardXYItemRenderer renderer = new StandardXYItemRenderer(); | 99 StandardXYItemRenderer renderer = new StandardXYItemRenderer(); |
92 XYPlot plot = new XYPlot(); | 100 XYPlot plot = new XYPlot(); |
93 //Global settings | 101 // Global settings |
94 | 102 |
95 plot.setOrientation(PlotOrientation.VERTICAL); | 103 plot.setOrientation(PlotOrientation.VERTICAL); |
96 plot.setBackgroundPaint(Color.lightGray); | 104 plot.setBackgroundPaint(Color.lightGray); |
97 plot.setDomainGridlinePaint(Color.white); | 105 plot.setDomainGridlinePaint(Color.white); |
98 plot.setRangeGridlinePaint(Color.white); | 106 plot.setRangeGridlinePaint(Color.white); |
99 plot.setAxisOffset(new RectangleInsets(5.0,5.0,5.0,5.0)); | 107 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); |
100 //plot.getRangeAxis().setFixedDimension(10.0); | 108 // plot.getRangeAxis().setFixedDimension(10.0); |
101 plot.setDomainAxis(domain); | 109 plot.setDomainAxis(domain); |
102 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); | 110 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); |
103 if (parameters.size() == 1) { | 111 if (parameters.size() == 1) { |
104 KeyValueDescibeData parameter = parameters.iterator().next(); | 112 KeyValueDescibeData parameter = parameters.iterator().next(); |
105 axis = new NumberAxis(parameter.getValue()); | 113 axis = new NumberAxis(parameter.getValue()); |
106 if(parameter.getValue().contains("richtung")){ | 114 if (parameter.getValue().contains("richtung")) { |
107 NumberAxis axis1 = new NumberAxis( | 115 NumberAxis axis1 = new NumberAxis((parameter |
108 ((String) parameter.getValue()));//,new Range(0.0,360.0)); | 116 .getValue()));// ,new Range(0.0,360.0)); |
109 axis1.setTickUnit(new NumberTickUnit(30.0)); | 117 axis1.setTickUnit(new NumberTickUnit(30.0)); |
110 axis1.setUpperBound(360.0); | 118 axis1.setUpperBound(360.0); |
111 axis1.setLowerBound(0.0); | 119 axis1.setLowerBound(0.0); |
112 //axis1.setDisplayRange(0.0,360.0); | 120 // axis1.setDisplayRange(0.0,360.0); |
113 plot.setRangeAxis( axis1); | 121 plot.setRangeAxis(axis1); |
114 }else{ | 122 } else { |
115 axis.setFixedDimension(10.0); | 123 axis.setFixedDimension(10.0); |
116 axis.setAutoRangeIncludesZero(false); | 124 axis.setAutoRangeIncludesZero(false); |
117 plot.setRangeAxis(axis); | 125 plot.setRangeAxis(axis); |
118 } | |
119 axis.configure(); | |
120 plot.setRangeAxisLocation( AxisLocation.BOTTOM_OR_LEFT); | |
121 plot.setRenderer(renderer); | |
122 plot.setDataset(lSet); | |
123 } else { | |
124 // Individual settings for different parameters | |
125 for (int i = 0; i < lSet.getSeriesCount(); i++) { | |
126 | |
127 plot.setDataset(i, getDataset((TimeSeriesCollection) lSet, i)); | |
128 Color mColor=color[i % color.length]; // zyklische Farbvergabe | |
129 mColor = color[0]; | |
130 // if ( pParameterId.length==1){ | |
131 | |
132 if(((String) lSet.getSeriesKey(i)).contains("richtung")){ | |
133 NumberAxis axis1 = new NumberAxis(((String) lSet.getSeriesKey(i)));//,new Range(0.0,360.0)); | |
134 axis1.setTickUnit(new NumberTickUnit(30.0)); | |
135 //axis1.setDisplayRange(0.0,360.0); | |
136 axis1.setLabelPaint(mColor); | |
137 axis1.setTickLabelPaint(mColor); | |
138 axis1.setUpperBound(360.0); | |
139 axis1.setLowerBound(0.0); | |
140 plot.setRangeAxis(i, axis1); | |
141 | |
142 | |
143 } | |
144 else { | |
145 axis = new NumberAxis((String) lSet.getSeriesKey(i)); | |
146 axis.setFixedDimension(10.0); | |
147 axis.setAutoRangeIncludesZero(false); | |
148 axis.setLabelPaint(mColor); | |
149 axis.setTickLabelPaint(mColor); | |
150 plot.setRangeAxis(i, axis); | |
151 axis.configure(); | |
152 } | |
153 if (i % 2 != 0) | |
154 plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_RIGHT); | |
155 else | |
156 plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT); | |
157 plot.mapDatasetToRangeAxis(i, i); | |
158 // } | |
159 renderer = new StandardXYItemRenderer(); | |
160 renderer.setSeriesPaint(i, mColor); | |
161 // renderer.setSeriesStroke(i,stroke[j]); | |
162 plot.setRenderer(i, renderer); | |
163 } | |
164 } | |
165 JFreeChart chart = new JFreeChart( | |
166 pLabels.getTitle(), | |
167 new Font ("SansSerif",Font.BOLD,24), | |
168 plot,true); | |
169 | |
170 | |
171 setStyle(chart, pStyle); | |
172 configureRenderingOptions(chart); | |
173 if (sDebug) | |
174 sLogger.debug(" vor encodeChart()"); | |
175 | |
176 encodeChart(chart, pStyle, outputStream); | |
177 } | |
178 private static XYDataset getDataset(TimeSeriesCollection T, int pIndex){ //throws TechnicalChartException{ | |
179 //if (T.getSeriesCount() < pIndex) throw TechnicalChartException(); | |
180 TimeSeriesCollection TSC = new TimeSeriesCollection(); | |
181 TSC.addSeries(T.getSeries(pIndex)); | |
182 return (XYDataset) TSC; | |
183 } | |
184 | |
185 private void configureRenderingOptions(JFreeChart pJfreechart) { | |
186 org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = ((XYPlot) pJfreechart | |
187 .getPlot()).getRenderer(); | |
188 if (xyitemrenderer instanceof XYLineAndShapeRenderer) { | |
189 XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer; | |
190 xylineandshaperenderer.setBaseShapesVisible(true); | |
191 xylineandshaperenderer.setBaseShapesFilled(true); | |
192 } | |
193 } | |
194 | |
195 private void setStyle(JFreeChart pJfreechart, ChartStyle pStyle) { | |
196 if (sDebug) | |
197 sLogger.debug("setStyle()"); | |
198 pJfreechart.setBackgroundPaint(pStyle.getCanvasColor()); | |
199 XYPlot xyplot = (XYPlot) pJfreechart.getPlot(); | |
200 xyplot.setBackgroundPaint(pStyle.getPlotBackgroundColor()); | |
201 xyplot.setDomainGridlinePaint(pStyle.getDomainGridlineColor()); | |
202 xyplot.setRangeGridlinePaint(pStyle.getRangeGridlineColor()); | |
203 | |
204 Insets lOffsets = pStyle.getAxisOffset(); | |
205 RectangleInsets lRectangleInsets = new RectangleInsets(lOffsets.mUpper, | |
206 lOffsets.mLeft, lOffsets.mLower, lOffsets.mRight); | |
207 xyplot.setAxisOffset(lRectangleInsets); | |
208 xyplot.setDomainCrosshairVisible(pStyle.isDomainCrosshairVisible()); | |
209 xyplot.setRangeCrosshairVisible(pStyle.isRangeCrosshairVisible()); | |
210 | |
211 } | |
212 | |
213 | |
214 private TimeSeries createTimeSeries(String pTimeSeriesName, Collection<Result> resultSet, | |
215 int lUpperCut, int lLowerCut,int pStart,int pEnd, Date dStart, Date dEnd) throws TechnicalChartException{ | |
216 if (sDebug) | |
217 sLogger.debug("createTimeSeries()"); | |
218 Result lRow0, lRow1; | |
219 | |
220 Date lDate=null, lDate0=null; | |
221 | |
222 TimeSeries lTimeseries = new TimeSeries(pTimeSeriesName, | |
223 org.jfree.data.time.Minute.class); | |
224 try { | |
225 long maxGap=0, lDateDiff=0; | |
226 double lValue=0; | |
227 | |
228 | |
229 int i = 0; | |
230 Iterator<Result> resultIterator = resultSet.iterator(); | |
231 while (resultIterator.hasNext()){ | |
232 Result lRow = resultIterator.next(); | |
233 if (i >= pStart+1 && i <= pEnd){ | |
234 if (i == pStart+1){ | |
235 lRow0 = lRow;// | |
236 | |
237 // Handle Gaps > 0.5% timeserieslength, i.e do not draw here | |
238 // +TODO mache maxGap variabel über diagram options | |
239 maxGap = (dEnd.getTime() - dStart.getTime()) / 200; // 0,5 prozent der Länge | |
240 if (maxGap < 3600000) maxGap=3600010; | |
241 if (maxGap <(dEnd.getTime() - dStart.getTime())/(pEnd-pStart)) | |
242 maxGap = (dEnd.getTime() - dStart.getTime())/(pEnd-pStart) + 1000; | |
243 | |
244 if (sDebug) | |
245 sLogger.debug("MaxGap : "+maxGap/1000+" Länge : "+(dEnd.getTime() - dStart.getTime())/1000+ | |
246 "Intervall "+(dEnd.getTime() - dStart.getTime())/(pEnd-pStart)/1000); | |
247 lDate = lRow0.getDate("XORDINATE"); | |
248 lDate0 = lDate; | |
249 lValue = lRow0.getDouble("YORDINATE"); | |
250 if (lValue > lLowerCut && lValue < lUpperCut){ | |
251 //lTimeseries.addOrUpdate(new Minute(lDate), lValue); | |
252 lTimeseries.add(new Minute(lDate), lValue); | |
253 } | |
254 } | |
255 //for (int i = pStart+1; i <= pEnd; i++) { | |
256 lRow1 =lRow; | |
257 lDate = lRow1.getDate("XORDINATE"); | |
258 lValue = lRow1.getDouble("YORDINATE"); | |
259 lDateDiff = lDate.getTime() - lDate0.getTime(); | |
260 if (lDateDiff > maxGap) { | |
261 // add 1 minute in millisecs to left hand side Date | |
262 // and insert Dummy to break line | |
263 lDate0.setTime((lDate0.getTime() + 60000)); | |
264 lTimeseries.addOrUpdate(new Minute(lDate0), null); | |
265 lTimeseries.addOrUpdate(new Minute(lDate), lValue); | |
266 //lTimeseries.add(new Minute(lDate0), null); | |
267 } else if (lDateDiff == 0) { | |
268 if (sDebug) | |
269 sLogger.debug("Datediff: "+lDateDiff+" bei index : "+i+" Datum : "+lDate+" "+lDate0); | |
270 } | |
271 else if (lValue > lLowerCut && lValue < lUpperCut) { | |
272 lTimeseries.addOrUpdate(new Minute(lDate), lValue); | |
273 //lTimeseries.add(new Minute(lDate), lValue); | |
274 } | |
275 lRow0 = lRow1; | |
276 lDate0 = lDate; | |
277 }else if (i > pEnd){ | |
278 return lTimeseries; | |
279 } | |
280 i++; | |
281 } | 126 } |
282 | 127 axis.configure(); |
283 | 128 plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); |
284 } catch (OutOfMemoryError e) { | 129 plot.setRenderer(renderer); |
285 sLogger.error(e.getMessage(), e); | 130 plot.setDataset(lSet); |
286 return lTimeseries; | 131 } else { |
287 | 132 // Individual settings for different parameters |
288 } catch (Exception e) { //TechnicalChartException | 133 for (int i = 0; i < lSet.getSeriesCount(); i++) { |
289 sLogger.error(e.getMessage(), e); | 134 |
290 } | 135 plot.setDataset(i, getDataset((TimeSeriesCollection) lSet, i)); |
291 finally { | 136 Color mColor = color[i % color.length]; // zyklische Farbvergabe |
292 } | 137 mColor = color[0]; |
293 | 138 // if ( pParameterId.length==1){ |
294 return lTimeseries; | 139 |
295 } | 140 if (((String) lSet.getSeriesKey(i)).contains("richtung")) { |
296 | 141 NumberAxis axis1 = new NumberAxis(((String) lSet |
297 private XYDataset createDataset(Collection<Result> resultSet, | 142 .getSeriesKey(i)));// ,new Range(0.0,360.0)); |
298 int lUpperCut, int lLowerCut,Collection<KeyValueDescibeData> parameters, Collection<KeyValueDescibeData> measurements) throws TechnicalChartException { | 143 axis1.setTickUnit(new NumberTickUnit(30.0)); |
299 | 144 // axis1.setDisplayRange(0.0,360.0); |
300 TimeSeriesCollection lTimeSeriesCollection = new TimeSeriesCollection(); | 145 axis1.setLabelPaint(mColor); |
301 try{ | 146 axis1.setTickLabelPaint(mColor); |
302 Date dStart = null, dEnd= null; | 147 axis1.setUpperBound(360.0); |
303 String break1, break2, break3; | 148 axis1.setLowerBound(0.0); |
304 int mStart = 0; | 149 plot.setRangeAxis(i, axis1); |
305 int mEnd = 0; | 150 |
306 | 151 } else { |
307 | 152 axis = new NumberAxis((String) lSet.getSeriesKey(i)); |
308 Iterator<Result> resultIterator = resultSet.iterator(); | 153 axis.setFixedDimension(10.0); |
309 if (resultIterator.hasNext()){ | 154 axis.setAutoRangeIncludesZero(false); |
310 Result row = resultIterator.next(); | 155 axis.setLabelPaint(mColor); |
311 | 156 axis.setTickLabelPaint(mColor); |
312 break1 = row.getString("GROUP1"); // 2 | 157 plot.setRangeAxis(i, axis); |
313 break2 = row.getString("GROUP2"); //3 | 158 axis.configure(); |
314 break3 = row.getString("GROUP3"); // 4 | 159 } |
315 dStart = row.getDate("XORDINATE"); | 160 if (i % 2 != 0) |
316 int i = 1; | 161 plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_RIGHT); |
317 while (resultIterator.hasNext()) { | 162 else |
318 row = resultIterator.next(); | 163 plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT); |
319 if (!break1.equals(row.getString("GROUP1")) | 164 plot.mapDatasetToRangeAxis(i, i); |
320 || !break2 .equals(row.getString("GROUP2")) | 165 // } |
321 || !break3.equals(row.getString("GROUP3"))){ | 166 renderer = new StandardXYItemRenderer(); |
322 String mTimeSeriesName = findValueTitle(parameters,break1)+" "+ | 167 renderer.setSeriesPaint(i, mColor); |
323 findValueTitle(measurements,break2)+"m"; | 168 // renderer.setSeriesStroke(i,stroke[j]); |
324 | 169 plot.setRenderer(i, renderer); |
325 lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName, | 170 } |
326 resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd)); | 171 } |
327 mStart = i; | 172 JFreeChart chart = new JFreeChart(pLabels.getTitle(), new Font( |
328 dStart = row.getDate("XORDINATE"); | 173 "SansSerif", Font.BOLD, 24), plot, true); |
329 break1 = row.getString("GROUP1"); | 174 |
330 break2 = row.getString("GROUP2"); //3 | 175 setStyle(chart, pStyle); |
331 break3 = row.getString("GROUP3"); // 4 | 176 configureRenderingOptions(chart); |
332 | 177 if (sDebug) |
333 } | 178 sLogger.debug(" vor encodeChart()"); |
334 mEnd = i; | 179 |
335 dEnd = row.getDate("XORDINATE"); | 180 encodeChart(chart, pStyle, outputStream); |
336 i = i + 1; | 181 } |
337 } | 182 |
338 | 183 private static XYDataset getDataset(TimeSeriesCollection T, int pIndex) { // throws |
339 String mTimeSeriesName = findValueTitle(parameters,break1)+" "+ | 184 // TechnicalChartException{ |
340 findValueTitle(measurements,break2)+"m"; | 185 // if (T.getSeriesCount() < pIndex) throw TechnicalChartException(); |
341 lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName, | 186 TimeSeriesCollection TSC = new TimeSeriesCollection(); |
342 resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd)); | 187 TSC.addSeries(T.getSeries(pIndex)); |
343 } | 188 return TSC; |
344 } | 189 } |
345 catch (Exception e){ | 190 |
346 sLogger.error(e.getMessage(), e); | 191 private void configureRenderingOptions(JFreeChart pJfreechart) { |
347 } | 192 org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = ((XYPlot) pJfreechart |
348 finally{ | 193 .getPlot()).getRenderer(); |
349 } | 194 if (xyitemrenderer instanceof XYLineAndShapeRenderer) { |
350 return lTimeSeriesCollection; | 195 XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer; |
351 } | 196 xylineandshaperenderer.setBaseShapesVisible(true); |
352 | 197 xylineandshaperenderer.setBaseShapesFilled(true); |
353 | 198 } |
354 | 199 } |
355 private void encodeChart(JFreeChart pChart, ChartStyle pStyle, OutputStream outputStream) | 200 |
356 throws IOException { | 201 private void setStyle(JFreeChart pJfreechart, ChartStyle pStyle) { |
357 if (sDebug) | 202 if (sDebug) |
358 sLogger.debug("encodeChart()"); | 203 sLogger.debug("setStyle()"); |
359 KeypointPNGEncoderAdapter lEncoder = new KeypointPNGEncoderAdapter(); | 204 pJfreechart.setBackgroundPaint(pStyle.getCanvasColor()); |
360 lEncoder.setEncodingAlpha(true); | 205 XYPlot xyplot = (XYPlot) pJfreechart.getPlot(); |
361 | 206 xyplot.setBackgroundPaint(pStyle.getPlotBackgroundColor()); |
362 int lWidth = (int) pStyle.getChartSize().getWidth(); | 207 xyplot.setDomainGridlinePaint(pStyle.getDomainGridlineColor()); |
363 int lHeight = (int) pStyle.getChartSize().getHeight(); | 208 xyplot.setRangeGridlinePaint(pStyle.getRangeGridlineColor()); |
364 | 209 |
365 BufferedImage lImage = pChart.createBufferedImage(lWidth, lHeight, | 210 Insets lOffsets = pStyle.getAxisOffset(); |
366 BufferedImage.BITMASK, null); | 211 RectangleInsets lRectangleInsets = new RectangleInsets(lOffsets.mUpper, |
367 | 212 lOffsets.mLeft, lOffsets.mLower, lOffsets.mRight); |
368 lEncoder.encode(lImage, outputStream); | 213 xyplot.setAxisOffset(lRectangleInsets); |
369 | 214 xyplot.setDomainCrosshairVisible(pStyle.isDomainCrosshairVisible()); |
370 } | 215 xyplot.setRangeCrosshairVisible(pStyle.isRangeCrosshairVisible()); |
371 | 216 |
372 private String findValueTitle(Collection<KeyValueDescibeData> values, String pMmtId){ | 217 } |
373 int id = 0; | 218 |
374 try { | 219 private TimeSeries createTimeSeries(String pTimeSeriesName, |
220 Collection<Result> resultSet, | |
221 int lUpperCut, int lLowerCut, | |
222 int pStart, int pEnd, Date dStart, | |
223 Date dEnd) | |
224 throws TechnicalChartException { | |
225 if (sDebug) | |
226 sLogger.debug("createTimeSeries()"); | |
227 Result lRow0, lRow1; | |
228 | |
229 Date lDate = null, lDate0 = null; | |
230 | |
231 TimeSeries lTimeseries = new TimeSeries(pTimeSeriesName, | |
232 org.jfree.data.time.Minute.class); | |
233 try { | |
234 long maxGap = 0, lDateDiff = 0; | |
235 double lValue = 0; | |
236 | |
237 int i = 0; | |
238 Iterator<Result> resultIterator = resultSet.iterator(); | |
239 while (resultIterator.hasNext()) { | |
240 Result lRow = resultIterator.next(); | |
241 if (i >= pStart + 1 && i <= pEnd) { | |
242 if (i == pStart + 1) { | |
243 lRow0 = lRow;// | |
244 | |
245 // Handle Gaps > 0.5% timeserieslength, i.e do not draw | |
246 // here | |
247 // +TODO mache maxGap variabel über diagram options | |
248 maxGap = (dEnd.getTime() - dStart.getTime()) / 200; // 0,5 | |
249 // prozent | |
250 // der | |
251 // Länge | |
252 if (maxGap < 3600000) | |
253 maxGap = 3600010; | |
254 if (maxGap < (dEnd.getTime() - dStart.getTime()) | |
255 / (pEnd - pStart)) | |
256 maxGap = (dEnd.getTime() - dStart.getTime()) | |
257 / (pEnd - pStart) + 1000; | |
258 | |
259 if (sDebug) | |
260 sLogger.debug("MaxGap : " + maxGap / 1000 | |
261 + " Länge : " | |
262 + (dEnd.getTime() - dStart.getTime()) | |
263 / 1000 + "Intervall " | |
264 + (dEnd.getTime() - dStart.getTime()) | |
265 / (pEnd - pStart) / 1000); | |
266 lDate = lRow0.getDate("XORDINATE"); | |
267 lDate0 = lDate; | |
268 lValue = lRow0.getDouble("YORDINATE"); | |
269 if (lValue > lLowerCut && lValue < lUpperCut) { | |
270 // lTimeseries.addOrUpdate(new Minute(lDate), | |
271 // lValue); | |
272 lTimeseries.add(new Minute(lDate), lValue); | |
273 } | |
274 } | |
275 // for (int i = pStart+1; i <= pEnd; i++) { | |
276 lRow1 = lRow; | |
277 lDate = lRow1.getDate("XORDINATE"); | |
278 lValue = lRow1.getDouble("YORDINATE"); | |
279 lDateDiff = lDate.getTime() - lDate0.getTime(); | |
280 if (lDateDiff > maxGap) { | |
281 // add 1 minute in millisecs to left hand side Date | |
282 // and insert Dummy to break line | |
283 lDate0.setTime((lDate0.getTime() + 60000)); | |
284 lTimeseries.addOrUpdate(new Minute(lDate0), null); | |
285 lTimeseries.addOrUpdate(new Minute(lDate), lValue); | |
286 // lTimeseries.add(new Minute(lDate0), null); | |
287 } else if (lDateDiff == 0) { | |
288 if (sDebug) | |
289 sLogger.debug("Datediff: " + lDateDiff | |
290 + " bei index : " + i + " Datum : " | |
291 + lDate + " " + lDate0); | |
292 } else if (lValue > lLowerCut && lValue < lUpperCut) { | |
293 lTimeseries.addOrUpdate(new Minute(lDate), lValue); | |
294 // lTimeseries.add(new Minute(lDate), lValue); | |
295 } | |
296 lRow0 = lRow1; | |
297 lDate0 = lDate; | |
298 } else if (i > pEnd) { | |
299 return lTimeseries; | |
300 } | |
301 i++; | |
302 } | |
303 | |
304 } catch (OutOfMemoryError e) { | |
305 sLogger.error(e.getMessage(), e); | |
306 return lTimeseries; | |
307 | |
308 } catch (Exception e) { // TechnicalChartException | |
309 sLogger.error(e.getMessage(), e); | |
310 } finally { | |
311 } | |
312 | |
313 return lTimeseries; | |
314 } | |
315 | |
316 private XYDataset createDataset(Collection<Result> resultSet, | |
317 int lUpperCut, int lLowerCut, | |
318 Collection<KeyValueDescibeData> parameters, | |
319 Collection<KeyValueDescibeData> measurements) | |
320 throws TechnicalChartException { | |
321 | |
322 TimeSeriesCollection lTimeSeriesCollection = new TimeSeriesCollection(); | |
323 try { | |
324 Date dStart = null, dEnd = null; | |
325 String break1, break2, break3; | |
326 int mStart = 0; | |
327 int mEnd = 0; | |
328 | |
329 Iterator<Result> resultIterator = resultSet.iterator(); | |
330 if (resultIterator.hasNext()) { | |
331 Result row = resultIterator.next(); | |
332 | |
333 break1 = row.getString("GROUP1"); // 2 | |
334 break2 = row.getString("GROUP2"); // 3 | |
335 break3 = row.getString("GROUP3"); // 4 | |
336 dStart = row.getDate("XORDINATE"); | |
337 int i = 1; | |
338 while (resultIterator.hasNext()) { | |
339 row = resultIterator.next(); | |
340 if (!break1.equals(row.getString("GROUP1")) | |
341 || !break2.equals(row.getString("GROUP2")) | |
342 || !break3.equals(row.getString("GROUP3"))) { | |
343 String mTimeSeriesName = findValueTitle(parameters, | |
344 break1) | |
345 + " " | |
346 + findValueTitle(measurements, | |
347 break2) + "m"; | |
348 | |
349 lTimeSeriesCollection.addSeries(createTimeSeries( | |
350 mTimeSeriesName, resultSet, lUpperCut, | |
351 lLowerCut, mStart, mEnd, dStart, dEnd)); | |
352 mStart = i; | |
353 dStart = row.getDate("XORDINATE"); | |
354 break1 = row.getString("GROUP1"); | |
355 break2 = row.getString("GROUP2"); // 3 | |
356 break3 = row.getString("GROUP3"); // 4 | |
357 | |
358 } | |
359 mEnd = i; | |
360 dEnd = row.getDate("XORDINATE"); | |
361 i = i + 1; | |
362 } | |
363 | |
364 String mTimeSeriesName = findValueTitle(parameters, break1) | |
365 + " " | |
366 + findValueTitle(measurements, break2) | |
367 + "m"; | |
368 lTimeSeriesCollection.addSeries(createTimeSeries( | |
369 mTimeSeriesName, resultSet, lUpperCut, lLowerCut, | |
370 mStart, mEnd, dStart, dEnd)); | |
371 } | |
372 } catch (Exception e) { | |
373 sLogger.error(e.getMessage(), e); | |
374 } finally { | |
375 } | |
376 return lTimeSeriesCollection; | |
377 } | |
378 | |
379 private void encodeChart(JFreeChart pChart, ChartStyle pStyle, | |
380 OutputStream outputStream) throws IOException { | |
381 if (sDebug) | |
382 sLogger.debug("encodeChart()"); | |
383 KeypointPNGEncoderAdapter lEncoder = new KeypointPNGEncoderAdapter(); | |
384 lEncoder.setEncodingAlpha(true); | |
385 | |
386 int lWidth = (int) pStyle.getChartSize().getWidth(); | |
387 int lHeight = (int) pStyle.getChartSize().getHeight(); | |
388 | |
389 BufferedImage lImage = pChart.createBufferedImage(lWidth, lHeight, | |
390 Transparency.BITMASK, null); | |
391 | |
392 lEncoder.encode(lImage, outputStream); | |
393 | |
394 } | |
395 | |
396 private String findValueTitle(Collection<KeyValueDescibeData> values, | |
397 String pMmtId) { | |
398 int id = 0; | |
399 try { | |
375 id = Integer.parseInt(pMmtId); | 400 id = Integer.parseInt(pMmtId); |
376 } catch (NumberFormatException e) { | 401 } catch (NumberFormatException e) { |
377 sLogger.warn(e,e); | 402 sLogger.warn(e, e); |
378 return pMmtId; | 403 return pMmtId; |
379 } | 404 } |
380 | 405 |
381 Iterator<KeyValueDescibeData> it = values.iterator(); | 406 Iterator<KeyValueDescibeData> it = values.iterator(); |
382 while(it.hasNext()){ | 407 while (it.hasNext()) { |
383 KeyValueDescibeData data = it.next(); | 408 KeyValueDescibeData data = it.next(); |
384 if (id ==Integer.parseInt(data.getKey())){ | 409 if (id == Integer.parseInt(data.getKey())) { |
385 return data.getValue(); | 410 return data.getValue(); |
386 } | 411 } |
387 } | 412 } |
388 return ""; | 413 return ""; |
389 } | 414 } |
390 } | 415 } |