how to plot a graph with a CSV file?

微笑、不失礼 提交于 2019-12-08 14:09:37

问题


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
...

I get the data from https://physionet.org/cgi-bin/atm/ATM with this setting:

Signals:ABP

Time format:seconds

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)...

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

pls advise how I can add it?

besides,sometimes after running the application it has stopped.

java code:

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 code:

<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_marginRight="11dp"
android:layout_width="wrap_content"
android:layout_height="280dp"

thank in advance


回答1:


You have to tackle this one step by step.

First you want to read a CSV file. Search for "parse csv file java" and you'll find many tutorials on how to do this.

As you parse through the csv file you'll want to build an array (or two) from the values collected.

Use these values in a for loop to generate new data points. So instead of manually entering each value, you'll have something that looks more like this:

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);

Now that you have some direction, try piecing some code together and if you run into trouble post your code for help.




回答2:


java code:

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

}
}


来源:https://stackoverflow.com/questions/47583634/how-to-plot-a-graph-with-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!