我想用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并尝试用数据绘制图表。 但图表不正确。我想当我想添加新的Datapoint时会出现问题,因为它需要添加一个逗号","每行之后
请告知我如何添加它?
此外,有时在运行应用程序后它已停止。
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"
提前致谢
答案 0 :(得分:0)
你必须一步一步解决这个问题。
首先,您要阅读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);
现在您已经有了一些方向,尝试将一些代码拼凑在一起,如果遇到麻烦,请发布代码以获取帮助。
答案 1 :(得分:0)
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
}
}