diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java index 2ae38bfc1b2de70337f94093206da9c651a3714e..6ff29a5c1ceb6c9d1819a510123f50124e984517 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java @@ -21,6 +21,7 @@ */ package com.xeiam.xchart.standalone; +import com.xeiam.xchart.CSVExporter; import com.xeiam.xchart.CSVImporter; import com.xeiam.xchart.CSVImporter.DataOrientation; import com.xeiam.xchart.Chart; @@ -36,6 +37,8 @@ public class CSVChart { // import chart from a folder containing CSV files Chart chart = CSVImporter.getChartFromCSVDir("./CSV/CSVChart/", DataOrientation.Rows, 600, 400); + CSVExporter.writeCSVRows(chart.getSeriesMap().get(0), "./CSV/CSVChartExport/"); + // Show it new SwingWrapper(chart).displayChart(); diff --git a/xchart/src/main/java/com/xeiam/xchart/CSVExporter.java b/xchart/src/main/java/com/xeiam/xchart/CSVExporter.java new file mode 100644 index 0000000000000000000000000000000000000000..d20b030a679ae534c69dc2ae6b10c2b1e304dfe7 --- /dev/null +++ b/xchart/src/main/java/com/xeiam/xchart/CSVExporter.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2013 Knowmtech <http://knowmtech.com> + * + * All rights reserved. No warranty, explicit or implicit, provided. In no event shall the author be liable for any claim or damages. + * + * IMPORTANT: THIS CODE IS PROPRIETARY!!! ABSOLUTELY NO DUPLICATION OR DISTRIBUTION IS PERMITTED WITHOUT EXPRESS WRITTEN PERMISSION FROM: + * M. ALEXANDER NUGENT CONSULTING 22B STACY RD, SANTA FE NM 87585 (505)-988-7016 i@alexnugent.name + */ +package com.xeiam.xchart; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Collection; +import java.util.Iterator; + +/** + * @author timmolter + */ +public class CSVExporter { + + public static void writeCSVRows(Series series, String path2Dir) { + + File newFile = new File(path2Dir + series.getName() + ".csv"); + Writer out = null; + try { + + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8")); + String csv = join(series.getxData(), ",") + System.getProperty("line.separator"); + out.write(csv); + csv = join(series.getyData(), ",") + System.getProperty("line.separator"); + out.write(csv); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (out != null) { + try { + out.flush(); + out.close(); + } catch (IOException e) { + // NOP + } + } + } + + } + + public static void writeCSVColumns(Series series, String path2Dir) { + + File newFile = new File(path2Dir + series.getName() + ".csv"); + Writer out = null; + try { + + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8")); + Collection<?> xData = series.getxData(); + Collection<?> yData = series.getyData(); + Iterator<?> itrx = xData.iterator(); + Iterator<?> itry = yData.iterator(); + while (itrx.hasNext()) { + Number xDataPoint = (Number) itrx.next(); + Number yDataPoint = (Number) itry.next(); + String csv = xDataPoint + "," + yDataPoint + System.getProperty("line.separator"); + out.write(csv); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (out != null) { + try { + out.flush(); + out.close(); + } catch (IOException e) { + // NOP + } + } + } + + } + + private static String join(Collection<? extends Object> collection, String separator) { + + if (collection == null) { + return null; + } + Iterator iterator = collection.iterator(); + // handle null, zero and one elements before building a buffer + if (iterator == null) { + return null; + } + if (!iterator.hasNext()) { + return ""; + } + Object first = iterator.next(); + if (!iterator.hasNext()) { + return first == null ? "" : first.toString(); + } + + // two or more elements + StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small + if (first != null) { + buf.append(first); + } + + while (iterator.hasNext()) { + if (separator != null) { + buf.append(separator); + } + Object obj = iterator.next(); + if (obj != null) { + buf.append(obj); + } + } + return buf.toString(); + + } +}