Skip to content
Snippets Groups Projects
Commit e5ca9b37 authored by Tim Molter's avatar Tim Molter
Browse files

added CSV export

parent 1283d194
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
*/ */
package com.xeiam.xchart.standalone; package com.xeiam.xchart.standalone;
import com.xeiam.xchart.CSVExporter;
import com.xeiam.xchart.CSVImporter; import com.xeiam.xchart.CSVImporter;
import com.xeiam.xchart.CSVImporter.DataOrientation; import com.xeiam.xchart.CSVImporter.DataOrientation;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
...@@ -36,6 +37,8 @@ public class CSVChart { ...@@ -36,6 +37,8 @@ public class CSVChart {
// import chart from a folder containing CSV files // import chart from a folder containing CSV files
Chart chart = CSVImporter.getChartFromCSVDir("./CSV/CSVChart/", DataOrientation.Rows, 600, 400); Chart chart = CSVImporter.getChartFromCSVDir("./CSV/CSVChart/", DataOrientation.Rows, 600, 400);
CSVExporter.writeCSVRows(chart.getSeriesMap().get(0), "./CSV/CSVChartExport/");
// Show it // Show it
new SwingWrapper(chart).displayChart(); new SwingWrapper(chart).displayChart();
......
/**
* 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment