Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/ChartExportHelper.java @ 9106:ffd4e2b0381c
Added metadata subtitle to all chart export.
author | gernotbelger |
---|---|
date | Tue, 29 May 2018 11:38:13 +0200 |
parents | 5e38e2924c07 |
children | 431f1c269be5 |
comparison
equal
deleted
inserted
replaced
9105:ae3565385e6a | 9106:ffd4e2b0381c |
---|---|
24 | 24 |
25 import java.io.IOException; | 25 import java.io.IOException; |
26 import java.io.OutputStream; | 26 import java.io.OutputStream; |
27 import java.io.OutputStreamWriter; | 27 import java.io.OutputStreamWriter; |
28 import java.io.UnsupportedEncodingException; | 28 import java.io.UnsupportedEncodingException; |
29 | 29 import java.text.DateFormat; |
30 import java.text.NumberFormat; | 30 import java.text.NumberFormat; |
31 import java.util.Date; | |
32 import java.util.Locale; | |
31 import java.util.Map; | 33 import java.util.Map; |
32 | 34 |
33 import org.jfree.chart.ChartRenderingInfo; | 35 import org.jfree.chart.ChartRenderingInfo; |
34 | 36 |
35 import javax.imageio.ImageIO; | 37 import javax.imageio.ImageIO; |
42 import org.apache.log4j.Logger; | 44 import org.apache.log4j.Logger; |
43 | 45 |
44 import org.jfree.chart.JFreeChart; | 46 import org.jfree.chart.JFreeChart; |
45 import org.jfree.chart.plot.XYPlot; | 47 import org.jfree.chart.plot.XYPlot; |
46 import org.jfree.data.xy.XYDataset; | 48 import org.jfree.data.xy.XYDataset; |
47 | 49 import org.dive4elements.artifacts.Artifact; |
48 import org.dive4elements.artifacts.CallContext; | 50 import org.dive4elements.artifacts.CallContext; |
49 | 51 |
50 import org.dive4elements.artifacts.common.utils.XMLUtils; | 52 import org.dive4elements.artifacts.common.utils.XMLUtils; |
51 | 53 import org.dive4elements.river.FLYS; |
54 import org.dive4elements.river.artifacts.resources.Resources; | |
55 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; | |
52 import org.dive4elements.river.jfree.XYMetaSeriesCollection; | 56 import org.dive4elements.river.jfree.XYMetaSeriesCollection; |
53 import org.dive4elements.river.utils.Formatter; | 57 import org.dive4elements.river.utils.Formatter; |
54 | 58 |
55 | 59 |
56 /** | 60 /** |
299 OutputStream out, | 303 OutputStream out, |
300 JFreeChart chart, | 304 JFreeChart chart, |
301 CallContext context) | 305 CallContext context) |
302 { | 306 { |
303 log.debug("export chart as CSV"); | 307 log.debug("export chart as CSV"); |
304 CSVWriter writer = null; | 308 |
305 try { | 309 try { |
306 writer = new CSVWriter( | 310 // FIXME: decide: to BOM or not to BOM |
311 // final byte[] BOM = new byte[] {(byte)0xEF, (byte)0xBB, (byte)0xBF}; | |
312 // out.write(BOM); | |
313 | |
314 final CSVWriter writer = new CSVWriter( | |
307 new OutputStreamWriter( | 315 new OutputStreamWriter( |
308 out, | 316 out, |
309 DEFAULT_ENCODING), | 317 DEFAULT_ENCODING), |
310 DEFAULT_CSV_SEPARATOR, '"', '\\', "\r\n"); | 318 DEFAULT_CSV_SEPARATOR, '"', '\\', "\r\n"); |
319 | |
320 NumberFormat format = Formatter.getCSVFormatter(context); | |
321 | |
322 XYPlot plot = chart.getXYPlot(); | |
323 int count = plot.getDatasetCount(); | |
324 for (int i = 0; i < count; i++) { | |
325 XYDataset data = plot.getDataset(i); | |
326 int scount = data.getSeriesCount(); | |
327 for (int j = 0; j < scount; j++) { | |
328 Comparable seriesKey = data.getSeriesKey(j); | |
329 log.debug("series key: " + seriesKey.toString()); | |
330 Map<String, String> metaData = null; | |
331 if (data instanceof XYMetaSeriesCollection) { | |
332 metaData = ((XYMetaSeriesCollection) data).getMetaData(); | |
333 } | |
334 writeCSVHeader(writer, seriesKey.toString(), metaData); | |
335 writeCSVData(writer, data, format); | |
336 } | |
337 } | |
338 writer.close(); | |
311 } | 339 } |
312 catch(UnsupportedEncodingException uee) { | 340 catch(UnsupportedEncodingException uee) { |
313 log.warn("Wrong encoding for CSV export."); | 341 log.warn("Wrong encoding for CSV export."); |
314 return; | 342 return; |
315 } | |
316 | |
317 NumberFormat format = Formatter.getCSVFormatter(context); | |
318 | |
319 XYPlot plot = chart.getXYPlot(); | |
320 int count = plot.getDatasetCount(); | |
321 for (int i = 0; i < count; i++) { | |
322 XYDataset data = plot.getDataset(i); | |
323 int scount = data.getSeriesCount(); | |
324 for (int j = 0; j < scount; j++) { | |
325 Comparable seriesKey = data.getSeriesKey(j); | |
326 log.debug("series key: " + seriesKey.toString()); | |
327 Map<String, String> metaData = null; | |
328 if (data instanceof XYMetaSeriesCollection) { | |
329 metaData = ((XYMetaSeriesCollection) data).getMetaData(); | |
330 } | |
331 writeCSVHeader(writer, seriesKey.toString(), metaData); | |
332 writeCSVData(writer, data, format); | |
333 } | |
334 } | |
335 try { | |
336 writer.close(); | |
337 } | 343 } |
338 catch(IOException ioe) { | 344 catch(IOException ioe) { |
339 log.error("Writing CSV export failed!"); | 345 log.error("Writing CSV export failed!"); |
340 } | 346 } |
341 } | 347 } |
443 * | 449 * |
444 * @return an array that contains the anchor for a chart with the given | 450 * @return an array that contains the anchor for a chart with the given |
445 * parameters. The first value is the x point, the second value is the y | 451 * parameters. The first value is the x point, the second value is the y |
446 * point. | 452 * point. |
447 */ | 453 */ |
448 public static double[] getCenteredAnchor( | 454 private static double[] getCenteredAnchor( |
449 double mLeft, double mRight, double mBottom, double mTop, | 455 double mLeft, double mRight, double mBottom, double mTop, |
450 double width, double height, | 456 double width, double height, |
451 double chartWidth, double chartHeight | 457 double chartWidth, double chartHeight |
452 ) { | 458 ) { |
453 if (log.isDebugEnabled()) { | 459 if (log.isDebugEnabled()) { |
478 log.debug("==> centered top origin: " + origin[1]); | 484 log.debug("==> centered top origin: " + origin[1]); |
479 } | 485 } |
480 | 486 |
481 return origin; | 487 return origin; |
482 } | 488 } |
489 | |
490 | |
491 public static String createMetadataSubtitle(Artifact artifact, final CallContext context, final String riverName) { | |
492 | |
493 final String version = FLYS.VERSION; | |
494 final String user = CalculationUtils.findArtifactUser(context, artifact); | |
495 final Locale locale = Resources.getLocale(context.getMeta()); | |
496 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); | |
497 final String dateText = df.format(new Date()); | |
498 | |
499 return Resources.getMsg(context.getMeta(), "chart.subtitle.metadata", "default", version, user, dateText, riverName); | |
500 } | |
483 } | 501 } |
484 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 502 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |