如何用 CSV 文件绘制图形? [英] how to plot a graph with a CSV file?

查看:172
本文介绍了如何用 CSV 文件绘制图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 GraphView 库在 android 中绘制图表 http://www.android-graphview.org/ .但是图表的点数超过12000,手动添加所有单点是不可能的.我的数据保存在存储中(使用自定义格式),如下所示:

I want to plot a diagram in android with GraphView library http://www.android-graphview.org/ . but the number of points of diagram is more than 12000 and it isn't possible to add all single points manually. my data saved in storage (with custom format) as shown below :

0.000,116.288
0.008,122.422
0.016,126.721
...

我从 https://physionet.org/cgi-bin/atm/获取数据自动取款机使用此设置:

信号:ABP

时间格式:秒

工具箱:将信号导出为 CSV

Toolbox:Export signals as CSV

我需要从文件中读取它们并转换数据,如下所示以绘制图表:

and I need to read them from file and convert data as shown below for plotting diagram :

new DataPoint(0.000,116.288),
new DataPoint(0.008,122.422),
new DataPoint(0.016,126.721)...

我在资产文件夹中复制了我的 CSV 文件并阅读了它.然后我将它们转换为 Double 并尝试用数据绘制图表.但图表不正确.我认为当我想添加新的Datapoint时会出现问题,因为它需要在每行后添加一个逗号,"

I copied my CSV file in asset folder and read it. then I convert them into Double and try to plot diagram with data. but the diagram is not correct . I think the problems appears when I want to add new Datapoint, because it need to add a comma "," after each line

请告诉我如何添加它?

此外,有时在运行应用程序后它会停止.

besides,sometimes after running the application it has stopped.

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"

提前致谢

推荐答案

java 代码:

使用 Graphview 库:

with using Graphview library:

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

}
}

这篇关于如何用 CSV 文件绘制图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆