我有一个制表符分隔的 CSV 文件,它有一个标题,它的第一列是每一行的标签。例如。
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
我使用 superCSV 来解析这个 CSV 文件并为每个点创建多边形,例如:(1, 1, 10.1) [表示第一行,第一列]。这些点被添加到多边形列表中。然后我使用多边形创建一个曲面,但该曲面不是连续的。我附上了我的绘图截图
我的部分代码如下:
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;
}
}
我需要帮助才能从我的 CSV 内容(数字)中创建一个连续平滑的 3D 表面