0

I have an activity where I take the input from edit text and store it in an list.

I also store in list the current date.

Then , I press the save button which saves the above.

The next day the user enter some data more and save and so on.

I want to make a plot with x-axis date format and y axis the values the user entered.

In one activity I have:

...
String filename = "data.csv";    
List<Double> mydata=new ArrayList<Double>();
List<Date> mydate=new ArrayList<Date>();

....value=(EditText) findViewById(R.id.enter_data);
...
switch (v.getId()){
        case R.id.savebtn:
            savefunc();

            break;
        case R.id.graphicsbtn: 

            Intent i = new Intent();        
            i.setClassName(this,LineGraph.class.getName());                 
            this.startActivity(i);  
            break;

   public void savefunc(){

    SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
    Date d=new Date();
    try{
     d=thedate.parse(filename);
    mydate.add(d);
    }
    catch  (ParseException e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    double thedata=Double.parseDouble(value.getText().toString().trim());
    mydata.add(thedata);
..
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    for (int i=0;i<mydate.size();i++){
       bw.write(mydate.get(i)+","+mydata.get(i)+"\n");
   ...

In the LineGraph Activity:

public class LineGraph extends Activity {


    private static List<Date> date = new ArrayList<Date>();
private static List<Double> data = new ArrayList<Double>();

    public Intent getIntent(Context context){

           readfunc();

      TimeSeries series = new TimeSeries("Showing data");
    for (int i=0;i<date.size();i++){    
        series.add(date.get(i),data.get(i));    

    }

The read function:

public void readfunc(){

    SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
    Date d=new Date();
    try{
     d=thedate.parse(filename);
    }
    catch.. 
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

         do {
             s = br.readLine();     
             if (s != null ){
                 String[] splitLine = s.split(",");
                 date.add(d);//Double.parseDouble(splitLine[0]));
                 data.add(Double.parseDouble(splitLine[1]));

I have these problems:

1) The file I receive is empty (some problem with the Date because the method for saving and reading from a file works).

2) At the graph screen appears a white background (of course no data because the file is empty) ,but why white background?I use the same code for other purposes and I don't receive a whitebackground.

3) I am not sure how to use Dates in x axis.Should I use List ? List ? .

------------------------UPDATE---------------------------------------------------------

Ok ,finally!(After user 'Dan' suggestion)

I used ChartFactory.getTimeChartView(this, dataset, mRenderer,"dd/MM/yyyy");

instead of ChartFactory.getLineChartIntent(context, dataset, mRenderer,"dd/MM/yyyy");

and you don't need to use String List , just Date List

George
  • 5,620
  • 13
  • 72
  • 137

2 Answers2

2

The code that deal with your file must be something like this (not compiled):

public void savefunc(){
    List<String> myDate = new ArrayList<String>(); //To store the formatted dates
    SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
    Date d=new Date(); //the current date
    String sd = thedate.format(d); // sd contains "16/04/2013", the formatted date
    myDate.add(sd);

    double thedata=Double.parseDouble(value.getText().toString().trim());
    mydata.add(thedata);
    ...
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    for (int i=0;i<mydate.size();i++){
       bw.write(mydate.get(i)+","+mydata.get(i)+"\n");
    }
}


public void readfunc(){

    SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
    Date d;
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    do {
        s = br.readLine();     
        if (s != null ){
            String[] splitLine = s.split(","); //first substring is the formatted date
            date.add(thedate.parse(splitLine[0])); //do something with exception
            data.add(Double.parseDouble(splitLine[1]));
...

Hope it helps.

eltabo
  • 3,260
  • 1
  • 18
  • 31
  • :First of all ,thanks for helping.Now, the line "date.add(thedate.parse(splitLine[0]));"doesn't work because "date" is a List.Anyway , I did "Date d=new Date(); String formattedDate=thedate.format(d); and date.add(formattedDate); instead of date.add(thedate.parse(splitLine[0])); " and it works!I save the file which has 2 columns. – George Apr 17 '13 at 07:32
  • :Now,the only problem is the gpraph.As far as I understand in order to have dates in x-axis I must have List.So, I am trying to convert the strings we created to dates.So, I create a List Ddate = new ArrayList(); and I am trying to populate it with the dates List(from before).I do :SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date convertedDate=new Date(); try{ for (int k=0;k – George Apr 17 '13 at 07:36
  • @George In order to have dates on the X axis, you either create a time chart or add custom labels on the X axis. – Dan D. Apr 17 '13 at 22:27
  • @Dan:Hello, I use "TimeSeries series = new TimeSeries("Showing data"); for (int i=0;i – George Apr 18 '13 at 07:14
  • Your code doesn't show anywhere the type of chart you are building. ChartFactory.getTimeChartIntent See this: https://code.google.com/p/achartengine/source/browse/trunk/achartengine/demo/org/achartengine/chartdemo/demo/chart/ProjectStatusChart.java – Dan D. Apr 18 '13 at 13:08
  • @Dan:I use TimeSeries and ChartFactory.getLineChartIntent. I tried 2 things.1) Store Dates in String List and then (in Linegraph) copy these values to Date List . 2) Store Dates (Date List) and retrieve also Dates (in Linegraph).Both, result in showing big nnumbers in x axis.. – George Apr 18 '13 at 13:15
  • @Dan:Ok , I did it!Thanks! – George Apr 18 '13 at 13:42
  • @Dan:A last one please.If I want to plot a scatter graph and at the same time use the date x axis (hence,ChartFactory.getTimeChartView) ,what should I do?Because as a line graph when I enter same values the same date ,the graph overlaps.Or, can you point me to an example of adding custom labels on the X axis you said before?Thanks! – George Apr 18 '13 at 15:56
  • Please ask a separate question describing what you are trying to do. – Dan D. Apr 18 '13 at 16:58
0

for dynamic plots use GraphicalView rather then intent::

 public GraphicalView mChartView;

creat a xml::

<RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:id="@+id/graph"
    android:layout_width="fill_parent"
    android:layout_height="145dip" >

  </LinearLayout>
</RelativeLayout> 

then in ur code :

 LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
 mChartView = ChartFactory.mChartView = ChartFactory.getLineChartView(getBaseContext(), dataset, renderer)
 layout.addView(mChartView); 
  • After entering values you read from edit text fields right:

  • then you are passing the new values to add in respective arraylists

  • then you should call the code for linegraph again after adding new values and remember to clear series before reading arraylists (ie. dataset.clear();) ie. when line code function starts at beginning add dataset.clear(); because if u dont clear data will overlap and may through exception or line may look thick at old data ...

then call mChartView.repaint(); to refresh graph

these links too ll help u link1 link2

Community
  • 1
  • 1
Kiran
  • 2,990
  • 4
  • 21
  • 38
  • :Hello , If you can help me with my example please.I don't want to do sth else now.Also , the links aren't for this kind of problem.Thanks – George Apr 15 '13 at 13:32
  • if u have already done for static data then mail it to me ll edit it to work for dynamic data – Kiran Apr 15 '13 at 15:44