Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
XChart
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
XChart
Commits
f9f77882
Commit
f9f77882
authored
11 years ago
by
Tim Molter
Browse files
Options
Downloads
Patches
Plain Diff
added CSV importer for data in columns
parent
adf4ef7b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java
+2
-2
2 additions, 2 deletions
...o/src/main/java/com/xeiam/xchart/standalone/CSVChart.java
xchart/src/main/java/com/xeiam/xchart/CSVImporter.java
+44
-3
44 additions, 3 deletions
xchart/src/main/java/com/xeiam/xchart/CSVImporter.java
with
46 additions
and
5 deletions
xchart-demo/src/main/java/com/xeiam/xchart/standalone/CSVChart.java
+
2
−
2
View file @
f9f77882
...
...
@@ -22,6 +22,7 @@
package
com.xeiam.xchart.standalone
;
import
com.xeiam.xchart.CSVImporter
;
import
com.xeiam.xchart.CSVImporter.DataOrientation
;
import
com.xeiam.xchart.Chart
;
import
com.xeiam.xchart.SwingWrapper
;
...
...
@@ -33,11 +34,10 @@ 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
);
Chart
chart
=
CSVImporter
.
getChartFromCSVDir
(
"./CSV/CSVChart/"
,
DataOrientation
.
Rows
,
600
,
400
);
// Show it
new
SwingWrapper
(
chart
).
displayChart
();
}
}
This diff is collapsed.
Click to expand it.
xchart/src/main/java/com/xeiam/xchart/CSVImporter.java
+
44
−
3
View file @
f9f77882
...
...
@@ -37,13 +37,18 @@ import java.util.List;
*/
public
class
CSVImporter
{
public
enum
DataOrientation
{
Rows
,
Columns
}
/**
* @param path2Directory
* @param width
* @param height
* @return
*/
public
static
Chart
getChartFromCSVDir
(
String
path2Directory
,
int
width
,
int
height
)
{
public
static
Chart
getChartFromCSVDir
(
String
path2Directory
,
DataOrientation
dataOrientation
,
int
width
,
int
height
)
{
// 1. get the directory, name chart the dir name
...
...
@@ -55,14 +60,19 @@ public class CSVImporter {
// 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
);
String
[]
xAndYData
=
null
;
if
(
dataOrientation
==
DataOrientation
.
Rows
)
{
xAndYData
=
getSeriesDataFromCSVRows
(
csvFile
);
}
else
{
xAndYData
=
getSeriesDataFromCSVColumns
(
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
)
{
private
static
String
[]
getSeriesData
FromCSVRows
(
File
csvFile
)
{
String
[]
xAndYData
=
new
String
[
2
];
...
...
@@ -89,12 +99,43 @@ public class CSVImporter {
return
xAndYData
;
}
private
static
String
[]
getSeriesDataFromCSVColumns
(
File
csvFile
)
{
String
[]
xAndYData
=
new
String
[
2
];
xAndYData
[
0
]
=
""
;
xAndYData
[
1
]
=
""
;
BufferedReader
bufferedReader
=
null
;
try
{
int
counter
=
0
;
String
line
=
null
;
bufferedReader
=
new
BufferedReader
(
new
FileReader
(
csvFile
));
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
xAndYData
[
0
]
+=
line
.
split
(
","
)[
0
]
+
","
;
xAndYData
[
1
]
+=
line
.
split
(
","
)[
1
]
+
","
;
}
}
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
];
System
.
out
.
println
(
dataPoint
);
BigDecimal
value
=
new
BigDecimal
(
dataPoint
);
axisData
.
add
(
value
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment