1

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?

the incorrect diagram plotted

the correct diagram

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

m.rajabi
  • 37
  • 1
  • 7
  • And what is the problem? And i see no question. – greenapps Nov 30 '17 at 22:33
  • Each signal consists of more than 12,000 points And in this code I used a few points for displaying. so I need a way to add all point with programing – m.rajabi Nov 30 '17 at 22:41
  • Nice info. But i still see no problem description or a question. – greenapps Nov 30 '17 at 22:44
  • Sorry, I edited the previous comment – m.rajabi Nov 30 '17 at 22:47
  • Dont tell only what you want. Tell the problems you have doing so. Then end with a question. – greenapps Nov 30 '17 at 22:59
  • Pls advise a solution to input the raw data i.e. 0.000,50 or 0.008,60 in the system to get processed data in this way: " New datapoint(0.000,50), ". As already mentioned there are 12000 points which I cannot input them one by one! So I need a quick solution. Hope I could clear my request and appreciate if you can provide me a solution. – m.rajabi Dec 01 '17 at 10:39
  • Please put what you want in your post. Not in a comment. You should write a decent post to begin with. And it looks that you still only tell what you want. You have not elaborated further. – greenapps Dec 01 '17 at 10:40
  • 1
    `I need to read them from file and convert data`. Well please do! What is the problem doing so? You did not mention one problem! – greenapps Dec 01 '17 at 10:44
  • The problem is I don't know how to convert the raw data! There are 12000 points and not easy to type all of them There must be an easy solution to convert them into new format! That's all – m.rajabi Dec 01 '17 at 15:03
  • You already told the solution: read them from file. So do it! Start with code that reads a file line by line. Post it here. – greenapps Dec 01 '17 at 17:19
  • So are you saying that if you have a string like `String line ="0.016,126.721"; ` you are unable to write code that ends with `new DataPoint(lat, lon),` with variables lat and lon containing the respective values? – greenapps Dec 01 '17 at 17:23
  • Yes, exactly that's what I need – m.rajabi Dec 01 '17 at 18:09
  • Yes i know that you need that. But i asked you something. You did not answer my question. You are again not mentioning any problem you have. – greenapps Dec 01 '17 at 18:15
  • when I am saying I need this, it means I can't do it and the problem is doing this work. I need somebody help to do it – m.rajabi Dec 01 '17 at 19:37
  • @greenapps I edited my post .pls read it – m.rajabi Dec 04 '17 at 16:33
  • 1
    You are creating new DataPoints like crazy for every line. And for every line read you also create a graph. I think you should know better. I think you know you should create one graph after the while loop. – greenapps Dec 04 '17 at 16:59
  • *because it need to add a comma "," after each line*... I don't think that is the issue. It looks like you plotted the points okay, but the top left coordinate of the screen is the starting point for all the lines – OneCricketeer Dec 04 '17 at 17:14
  • I plotted a diagram with only one point and as a result there was a line between the top left coordinate of the screen and the input point. then I plotted another diagram with two input points and then 2 points were joined together with 1 line. according to these results I think this diagram (as you see it) contains some diagrams with one point. so I should enter all the points at once enabling it to connect all the points together. I get out the drawing line _(graph.addSeries(series);)_ from the while loop , but still did not get any good results. – m.rajabi Dec 04 '17 at 19:41
  • @greenapps tell a better way to create new Datapoint ,if u can. I do that but still did not get any good result – m.rajabi Dec 04 '17 at 20:10
  • You create it ok i think. But you create way too much i said. You should only create one for every line. You create as much as there are characters in that line + 1. `for (int i = 0; i < valXY.length; i++) ` Well that is a ridiculous loop. Remove it. And construct the graph AFTER the while loop. Adjust your code. Also here. When you have done that you are not ready yet. But this first please. – greenapps Dec 04 '17 at 20:40

2 Answers2

1

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

}
}
m.rajabi
  • 37
  • 1
  • 7
0

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.