【问题标题】:how to plot a graph with a CSV file?如何用 CSV 文件绘制图形?
【发布时间】:2017-11-30 22:24:01
【问题描述】:

我想在 android 中使用 GraphView 库 http://www.android-graphview.org/ 绘制图表。但是图表的点数超过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"

提前致谢

【问题讨论】:

  • 还有什么问题?我看不出有任何问题。
  • 每个信号由超过 12,000 个点组成,在这段代码中,我使用了几个点来显示。所以我需要一种通过编程添加所有要点的方法
  • 不错的信息。但我仍然没有看到任何问题描述或问题。
  • I need to read them from file and convert data。好吧,请做!这样做有什么问题?你没有提到一个问题!
  • 您正在为每一行疯狂地创建新的数据点。对于读取的每一行,您还可以创建一个图表。我想你应该知道得更好。我想你知道你应该在 while 循环之后创建一个图表。

标签: java android plot android-graphview


【解决方案1】:

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

}
}

【讨论】:

    【解决方案2】:

    你必须一步一步地解决这个问题。

    首先您要读取 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);
    

    现在您有了一些方向,尝试将一些代码拼凑在一起,如果遇到问题,请发布您的代码以寻求帮助。

    【讨论】:

    • "parse csv java" 不会告诉你如何从 Android 磁盘读取数据
    • @cricket_007 非常感谢您的提示。我有一个新问题要在问题中提出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2017-07-20
    相关资源
    最近更新 更多