I have a tab-delimited CSV file that has a header, and its first column are labels for each row. For example.
Label Sample1 Sample2 Sample3 Sample4 Sample5
U.S.A. 10.1 3.2 5.6 6.9 7.3
Canada 9.8 4.5 5.7 6.8 7.9
I use superCSV to parse this CSV file and create polygons for each point, for example: (1, 1, 10.1) [meaning first row, first column]. The points are added to a list of polygons. I then use the polygons to create a surface, but the surface is not continuous. I have attached a screenshot of my plotting
Part of my code is as follows:
public void init() {
/* build a list of polygons out of the CSV file */
List<Polygon> polygons = null;
try {
polygons = parseCSV("my_CSVFile.csv");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("size of polygons is: " + polygons.size());
// Creates the 3d object
Shape surface = new Shape(polygons);
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new org.jzy3d.colors.Color(1,1,1,1f)));
surface.setWireframeDisplayed(true);
surface.setWireframeColor(org.jzy3d.colors.Color.BLACK);
chart = AWTChartComponentFactory.chart(Quality.Advanced, getCanvasType());
// chart = new Chart();
chart.getScene().getGraph().add(surface);
}
public List<Polygon> parseCSV(String csvFile) throws IOException
{
if (csvFile.isEmpty())
System.exit(-1);
File inputFile = new File(csvFile);
String[] header = null; /* header row */
if (inputFile.exists())
{
FileReader fr = new FileReader(inputFile);
LineNumberReader lineReader = new LineNumberReader(fr);
String headerLine = null;
while (lineReader.getLineNumber() == 0)
{
headerLine = lineReader.readLine();
}
lineReader.close();
if (headerLine != null)
{
header = headerLine.split("\\t");
}
}
ICsvListReader listReader = null;
List<Polygon> polygons = new ArrayList<Polygon>();
try {
listReader = new CsvListReader(new FileReader(csvFile), CsvPreference.TAB_PREFERENCE);
listReader.getHeader(true);
List<String> contentList;
int rowIndex = 1; // excluding the header row
while((contentList = listReader.read()) != null)
{
if (contentList.size() != header.length) {
System.out.println("contentList size is: " + contentList.size() + ", header length is: " + header.length);
continue;
}
Polygon polygon = new Polygon();
for (int i = 1; i < contentList.size(); i++)
{
if (DoubleFactory.tryParseDouble(contentList.get(i)) != -1) /* unsuccessful double parse returns -1 */
{
polygon.add(new Point(new Coord3d(rowIndex, i, Double.parseDouble(contentList.get(i)))));
}
}
rowIndex++;
polygons.add(polygon);
}
} finally {
if(listReader != null) {
listReader.close();
}
}
return polygons;
}
/* inner class for parsing string to double */
private static class DoubleFactory
{
public static double tryParseDouble(final String number)
{
double result;
try {
result = Double.parseDouble(number);
} catch (NumberFormatException e) {
result = -1; /* default failed parsing*/
}
return result;
}
}
I need help with creating a continuous smooth 3D surface out of my CSV contents (the numbers)
My code for creating the polygon list is as follows
where
m_data
is a class variable and a matrix. As far as I know, this is the way you should create a polygon. A polygon only consisting of a single point seems rather unlikely to be correct.Related question: Build a 3d surface plot using xyz coordinates with jzy3d