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

CSV import and export with errors bars

parent e99d9c6d
No related branches found
No related tags found
No related merge requests found
1,12
2,34
3,56
\ No newline at end of file
1,12,4
2,34,12
3,56,21
\ No newline at end of file
1,12
2,34
3,56
1,12,4,
2,34,12,
3,56,21,
1,2,3
12,34,56
4,12,21
\ No newline at end of file
1,2,3
12,34,56
4,12,21
......@@ -49,6 +49,10 @@ public class CSVExporter {
out.write(csv);
csv = join(series.getyData(), ",") + System.getProperty("line.separator");
out.write(csv);
if (series.getErrorBars() != null) {
csv = join(series.getErrorBars(), ",") + System.getProperty("line.separator");
out.write(csv);
}
} catch (Exception e) {
e.printStackTrace();
......@@ -78,14 +82,32 @@ public class CSVExporter {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8"));
Collection<?> xData = series.getxData();
Collection<?> yData = series.getyData();
Collection<Number> yData = series.getyData();
Collection<Number> errorBarData = series.getErrorBars();
Iterator<?> itrx = xData.iterator();
Iterator<?> itry = yData.iterator();
Iterator<Number> itry = yData.iterator();
Iterator<Number> itrErrorBar = null;
if (errorBarData != null) {
itrErrorBar = errorBarData.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);
Number yDataPoint = itry.next();
Number errorBarValue = null;
if (itrErrorBar != null) {
errorBarValue = itrErrorBar.next();
}
StringBuilder sb = new StringBuilder();
sb.append(xDataPoint + ",");
sb.append(yDataPoint + ",");
if (errorBarValue != null) {
sb.append(errorBarValue + ",");
}
sb.append(System.getProperty("line.separator"));
// String csv = xDataPoint + "," + yDataPoint + errorBarValue == null ? "" : ("," + errorBarValue) + System.getProperty("line.separator");
// String csv = + yDataPoint + System.getProperty("line.separator");
out.write(sb.toString());
}
} catch (Exception e) {
......
......@@ -70,7 +70,13 @@ public class CSVImporter {
else {
xAndYData = getSeriesDataFromCSVColumns(csvFile);
}
chart.addSeries(csvFile.getName().substring(0, csvFile.getName().indexOf(".csv")), getAxisData(xAndYData[0]), getAxisData(xAndYData[1]));
if (xAndYData[2] == null || xAndYData[2].trim().equalsIgnoreCase("")) {
chart.addSeries(csvFile.getName().substring(0, csvFile.getName().indexOf(".csv")), getAxisData(xAndYData[0]), getAxisData(xAndYData[1]));
}
else {
chart.addSeries(csvFile.getName().substring(0, csvFile.getName().indexOf(".csv")), getAxisData(xAndYData[0]), getAxisData(xAndYData[1]), getAxisData(xAndYData[2]));
}
}
return chart;
......@@ -96,7 +102,7 @@ public class CSVImporter {
*/
private static String[] getSeriesDataFromCSVRows(File csvFile) {
String[] xAndYData = new String[2];
String[] xAndYData = new String[3];
BufferedReader bufferedReader = null;
try {
......@@ -127,9 +133,10 @@ public class CSVImporter {
*/
private static String[] getSeriesDataFromCSVColumns(File csvFile) {
String[] xAndYData = new String[2];
String[] xAndYData = new String[3];
xAndYData[0] = "";
xAndYData[1] = "";
xAndYData[2] = "";
BufferedReader bufferedReader = null;
try {
......@@ -137,8 +144,12 @@ public class CSVImporter {
String line = null;
bufferedReader = new BufferedReader(new FileReader(csvFile));
while ((line = bufferedReader.readLine()) != null) {
xAndYData[0] += line.split(",")[0] + ",";
xAndYData[1] += line.split(",")[1] + ",";
String[] dataArray = line.split(",");
xAndYData[0] += dataArray[0] + ",";
xAndYData[1] += dataArray[1] + ",";
if (dataArray.length > 2) {
xAndYData[2] += dataArray[2] + ",";
}
}
} catch (Exception e) {
......@@ -165,8 +176,13 @@ public class CSVImporter {
String[] stringDataArray = stringData.split(",");
for (int i = 0; i < stringDataArray.length; i++) {
String dataPoint = stringDataArray[i];
BigDecimal value = new BigDecimal(dataPoint);
axisData.add(value);
try {
BigDecimal value = new BigDecimal(dataPoint);
axisData.add(value);
} catch (NumberFormatException e) {
System.out.println("Error parsing >" + dataPoint + "< !");
throw (e);
}
}
return axisData;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment