diff --git a/pom.xml b/pom.xml index 445f822fc631eac7850d091e996526d5a89cb58f..c3a947170ffcc780f97b51ffa8400aefb46b9863 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ <groupId>com.xeiam.xchart</groupId> <artifactId>xchart-parent</artifactId> - <version>2.1.1-SNAPSHOT</version> + <version>2.2.0-SNAPSHOT</version> <packaging>pom</packaging> <name>XChart Parent</name> <description>Basic Charts for Java Applications</description> diff --git a/xchart-demo/CSV/CSVChart/series1.csv b/xchart-demo/CSV/CSVChart/series1.csv new file mode 100644 index 0000000000000000000000000000000000000000..18bcd34e66f5845392b7cbb951769c762967c66c --- /dev/null +++ b/xchart-demo/CSV/CSVChart/series1.csv @@ -0,0 +1,2 @@ +1,2,3 +12,34,56 \ No newline at end of file diff --git a/xchart-demo/CSV/CSVChart/series2.csv b/xchart-demo/CSV/CSVChart/series2.csv new file mode 100644 index 0000000000000000000000000000000000000000..0b3fe0d3a422dd4a410dbb5aece32369c30d8fc6 --- /dev/null +++ b/xchart-demo/CSV/CSVChart/series2.csv @@ -0,0 +1,2 @@ +1,2,3 +56,34,12 \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..72387636563aa524366d879f32861b989e3ba928 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart.standalone; + +import com.xeiam.xchart.CSVImporter; +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.SwingWrapper; + +/** + * @author timmolter + */ +public class CSVChart { + + public static void main(String[] args) throws Exception { + + // import chart from a folder containing CSV files + Chart chart = CSVImporter.getChartFromCSVDir("./CSV/CSVChart/", 600, 400); + + // Show it + new SwingWrapper(chart).displayChart(); + + } + +} diff --git a/xchart/src/main/java/com/xeiam/xchart/CSVImporter.java b/xchart/src/main/java/com/xeiam/xchart/CSVImporter.java new file mode 100644 index 0000000000000000000000000000000000000000..594e556088eef3d46a46dca211b16eaf1312757e --- /dev/null +++ b/xchart/src/main/java/com/xeiam/xchart/CSVImporter.java @@ -0,0 +1,146 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +/** + * @author timmolter + */ +public class CSVImporter { + + public static Chart getChartFromCSVDir(String path2Directory, int width, int height) { + + // 1. get the directory, name chart the dir name + + Chart chart = new Chart(width, height); + + // 2. get all the csv files in the dir + File[] csvFiles = getAllFiles(path2Directory, ".*.csv"); + + // 3. create a series for each file, naming the series the file name + for (int i = 0; i < csvFiles.length; i++) { + File csvFile = csvFiles[i]; + String[] xAndYData = getSeriesData(csvFile); + chart.addSeries(csvFile.getName().substring(0, csvFile.getName().indexOf(".csv")), getAxisData(xAndYData[0]), getAxisData(xAndYData[1])); + } + + return chart; + } + + private static String[] getSeriesData(File csvFile) { + + String[] xAndYData = new String[2]; + + BufferedReader bufferedReader = null; + try { + int counter = 0; + String line = null; + bufferedReader = new BufferedReader(new FileReader(csvFile)); + while ((line = bufferedReader.readLine()) != null) { + xAndYData[counter++] = line; + } + + } catch (Exception e) { + System.out.println("Exception while reading csv file: " + e); + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return xAndYData; + } + + private static List<Number> getAxisData(String stringData) { + + List<Number> axisData = new ArrayList<Number>(); + String[] stringDataArray = stringData.split(","); + for (int i = 0; i < stringDataArray.length; i++) { + String dataPoint = stringDataArray[i]; + BigDecimal value = new BigDecimal(dataPoint); + axisData.add(value); + } + return axisData; + } + + /** + * This method returns the files found in the given directory matching the given regular expression. + * + * @param dirName - ex. "./images/colors/original/" *make sure you have the '/' on the end + * @param regex - ex. ".*.png" + * @return File[] - an array of files + */ + public static File[] getAllFiles(String dirName, String regex) { + + File[] allFiles = getAllFiles(dirName); + + List<File> matchingFiles = new ArrayList<File>(); + + for (int i = 0; i < allFiles.length; i++) { + + if (allFiles[i].getName().matches(regex)) { + matchingFiles.add(allFiles[i]); + } + } + + return matchingFiles.toArray(new File[matchingFiles.size()]); + + } + + /** + * This method returns the Files found in the given directory + * + * @param dirName - ex. "./images/colors/original/" *make sure you have the '/' on the end + * @return File[] - an array of files + */ + public static File[] getAllFiles(String dirName) { + + File dir = new File(dirName); + + File[] files = dir.listFiles(); // returns files and folders + + if (files != null) { + List<File> filteredFiles = new ArrayList<File>(); + for (int i = 0; i < files.length; i++) { + + if (files[i].isFile()) { + filteredFiles.add(files[i]); + } + } + return filteredFiles.toArray(new File[filteredFiles.size()]); + } else { + System.out.println(dirName + " does not denote a valid directory!"); + return new File[0]; + } + } + +}