如何用 CSV 文件绘制图表?
how to plot a graph with a CSV file?
我想用 GraphView 库 http://www.android-graphview.org/ 在 android 中绘制图表。但是图表的点数超过12000个,不可能手动添加所有的单点。我的数据保存在存储器中(使用自定义格式),如下所示:
0.000,116.288
0.008,122.422
0.016,126.721
...
我从 https://physionet.org/cgi-bin/atm/ATM 获取数据
使用此设置:
信号:ABP
时间格式:秒
工具箱:将信号导出为 CSV
我需要从文件中读取它们并转换数据,如下图所示:
new DataPoint(0.000,116.288),
new DataPoint(0.008,122.422),
new DataPoint(0.016,126.721)...
我将我的 CSV 文件复制到资产文件夹中并阅读了它。然后我将它们转换成 Double 并尝试用数据绘制图表。
但图表不正确。我认为当我想添加新的数据点时会出现问题,因为它需要在每行后添加一个逗号“,”
请问如何添加?
此外,有时在 运行 应用程序停止后。
java代码:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
try {
reader=new BufferedReader(new InputStreamReader(getAssets().open("plot/b00_2010_abp_5s.txt")));
String mline;
while((mline=reader.readLine())!=null)
{
valXY = mline.split(",");
Xval =Double.parseDouble(valXY[0]);
Yval =Double.parseDouble(valXY[1]);
DataPoint[] dp = new DataPoint[valXY.length];
for (int i = 0; i < valXY.length; i++)
{
dp[i] = new DataPoint(Xval, Yval);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
}
} catch (IOException e)
{
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}
XML代码:
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_marginRight="11dp"
android:layout_width="wrap_content"
android:layout_height="280dp"
在此先感谢
你必须一步一步来解决这个问题。
首先您要读取 CSV 文件。搜索 "parse csv file java",您会找到许多关于如何执行此操作的教程。
当您解析 csv 文件时,您需要根据收集的值构建一个(或两个)数组。
在 for 循环中使用这些值来生成新的数据点。因此,无需手动输入每个值,您将得到更像这样的内容:
DataPoint[] dp = new DataPoint[yourCSVArray.size()];
for (int i = 0; i < yourCSVArray.size(); i++) {
dp[i] = new DataPoint(variableX, variableY);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
现在您有了一些方向,请尝试将一些代码拼凑在一起,如果您 运行 遇到麻烦 post 您的代码寻求帮助。
java代码:
使用 Graphview 库:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open("plot/excel_data_abp.csv")));
reader.readLine(); //skip first line of file
reader.readLine(); //skip second line of file
String mline;
ArrayList<DataPoint> arrDataPoint=new ArrayList<>();
while ((mline = reader.readLine()) != null) {
valXY = mline.split(",");
Xval = Double.parseDouble(valXY[0]);
Yval = Double.parseDouble(valXY[1]);
DataPoint dp = new DataPoint(Xval, Yval);
arrDataPoint.add(dp);
}
DataPoint[] listDp = new DataPoint[arrDataPoint.size()];
for(int i=0;i<arrDataPoint.size();i++){
listDp[i]=arrDataPoint.get(i);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(listDp);
graph.addSeries(series);
} catch (IOException e) {
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}
我想用 GraphView 库 http://www.android-graphview.org/ 在 android 中绘制图表。但是图表的点数超过12000个,不可能手动添加所有的单点。我的数据保存在存储器中(使用自定义格式),如下所示:
0.000,116.288
0.008,122.422
0.016,126.721
...
我从 https://physionet.org/cgi-bin/atm/ATM 获取数据 使用此设置:
信号:ABP
时间格式:秒
工具箱:将信号导出为 CSV
我需要从文件中读取它们并转换数据,如下图所示:
new DataPoint(0.000,116.288),
new DataPoint(0.008,122.422),
new DataPoint(0.016,126.721)...
我将我的 CSV 文件复制到资产文件夹中并阅读了它。然后我将它们转换成 Double 并尝试用数据绘制图表。 但图表不正确。我认为当我想添加新的数据点时会出现问题,因为它需要在每行后添加一个逗号“,”
请问如何添加?
此外,有时在 运行 应用程序停止后。
java代码:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
try {
reader=new BufferedReader(new InputStreamReader(getAssets().open("plot/b00_2010_abp_5s.txt")));
String mline;
while((mline=reader.readLine())!=null)
{
valXY = mline.split(",");
Xval =Double.parseDouble(valXY[0]);
Yval =Double.parseDouble(valXY[1]);
DataPoint[] dp = new DataPoint[valXY.length];
for (int i = 0; i < valXY.length; i++)
{
dp[i] = new DataPoint(Xval, Yval);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
}
} catch (IOException e)
{
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}
XML代码:
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_marginRight="11dp"
android:layout_width="wrap_content"
android:layout_height="280dp"
在此先感谢
你必须一步一步来解决这个问题。
首先您要读取 CSV 文件。搜索 "parse csv file java",您会找到许多关于如何执行此操作的教程。
当您解析 csv 文件时,您需要根据收集的值构建一个(或两个)数组。
在 for 循环中使用这些值来生成新的数据点。因此,无需手动输入每个值,您将得到更像这样的内容:
DataPoint[] dp = new DataPoint[yourCSVArray.size()];
for (int i = 0; i < yourCSVArray.size(); i++) {
dp[i] = new DataPoint(variableX, variableY);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
现在您有了一些方向,请尝试将一些代码拼凑在一起,如果您 运行 遇到麻烦 post 您的代码寻求帮助。
java代码:
使用 Graphview 库:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open("plot/excel_data_abp.csv")));
reader.readLine(); //skip first line of file
reader.readLine(); //skip second line of file
String mline;
ArrayList<DataPoint> arrDataPoint=new ArrayList<>();
while ((mline = reader.readLine()) != null) {
valXY = mline.split(",");
Xval = Double.parseDouble(valXY[0]);
Yval = Double.parseDouble(valXY[1]);
DataPoint dp = new DataPoint(Xval, Yval);
arrDataPoint.add(dp);
}
DataPoint[] listDp = new DataPoint[arrDataPoint.size()];
for(int i=0;i<arrDataPoint.size();i++){
listDp[i]=arrDataPoint.get(i);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(listDp);
graph.addSeries(series);
} catch (IOException e) {
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}