6

I have an application where the user enters data in edittext and presses the save button.

By pressing 'save' I save in a file the user data (in one column) and the current date (in the other column).

Then , I press another button and make the plot (using achartengine) date (x axis) data (y axis).

So, entering data during a day ,results in saving for example: "1" (user data) -> 20/4/2013 , "2" -> 20/4/2013 , "3" -> 20/4/2013.

And in plot I have 3 points in y axis (ok) and 3 points in x axis (not ok).

I want to have one point in x axis because the data where entered in the same day.

I save data :

public void savefunc(){

        SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
        Date d=new Date();

        String formattedDate=thedate.format(d);
        Log.d("tag","format"+formattedDate);
        dates_Strings.add(formattedDate);


        double thedata=Double.parseDouble(value.getText().toString().trim());
            mydata.add(thedata);


        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard, "MyFiles");
        directory.mkdirs();            
        File file = new File(directory, filename);

        FileOutputStream fos;

        //saving them
        try {
           fos = new FileOutputStream(file);

              BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
              for (int i=0;i<mydata.size();i++){
                 bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
              }
              ...

How can I save the user data during a day ?

Maybe some check here : Date d=new Date(); ? To check if it is the same day.

Or here : bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");

But I can't figure.

For example I enter data " 1" , "2" ,"3" in date "20/4/2013".

This is what I get now using my code: This is what I get now

But i require graph like below: data entered on same day should be put together:: This is what I want

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

  mRenderer.setXLabels(0);
    for (int i=0;i<mydata.size();i++){

        mRenderer.addXTextLabel(i,dates_Strings.get(i));

        Date lastDate=null;
        String lastdate="";

        try{

    // the initial date
Date initialDate=formatter.parse(dates_Strings.get(mydata.size()-1));

Calendar c = Calendar.getInstance();
c.setTime(initialDate);
c.add(Calendar.DATE, 1);  // increase date by one 
lastDate =c.getTime();                  

}catch ...
      }
    mRenderer.setXAxisMax(lastDate.getTime());
    mRenderer.addXTextLabel(i,dates_Strings.get(i));
    }
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
George
  • 5,808
  • 15
  • 83
  • 160

4 Answers4

1

ok.

When you call new Date(), you also determine time of creation (default format is: January 1, 1970, 00:00:00 GMT). Because your points are created in different time but same date, your points are not aligned.

So you should do it like this:

Calendar thisDay = Calendar.getInstance();
thisDay.set(Calendar.HOUR, 0);
thisDay.set(Calendar.MINUTE, 0);
thisDay.set(Calendar.SECOND, 0);
Date d=thisDay.getTime();//this returns Date :) - it is funny but true

then you can use d as current date :).

Hope it is true and it helps, Toni

toni
  • 361
  • 7
  • 19
  • @:Using the above and then "SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); " and "String formattedDate=thedate.format(d); dates_Strings.add(formattedDate);" , (because I want to store dates as Strings ) didn't work.Still the same issue.. – George Apr 23 '13 at 20:54
  • :So, do you have any other ideas for that?Maybe a check in order to see if the date is the same?Thank you – George Apr 24 '13 at 09:33
  • How do you than create a date object when you are reading from db? – toni Apr 24 '13 at 13:59
  • :Sorry , I didn't understand your question? – George Apr 24 '13 at 14:01
  • When you are loading your data from your file (storing data into file is actually not good - why you dont use sqlite?), how do you convert it to date? you read from txt file date in dd/MM/yyyy. How do you put this string into date object? – toni Apr 24 '13 at 17:58
  • :Ok , I updated.I don't want to use database for this.And I want to store data into excel file (csv).Thanks! – George Apr 24 '13 at 19:17
  • @George i'm quite sure excel can handle a file that has multiple points which have the same x-value. – android developer Apr 27 '13 at 19:15
1

There are a few possible solutions for this problem:

  1. instead of a date, put the unix time of the date (long value) . in order to show it, you can convert the unix time to formatted date .

  2. since excel can handle dates, edit the output file and use "=Date(year,month,day)" or "=DATEVALUE("2013/4/20")"

this is all because the problem isn't even related to android. it's about showing the data. the data is ok. it's just how you show it.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • :I can't figure how to do 1.Can you give me an example based in my code?Also, I didn't understand 2.How should I do it?Thanks for your help. – George Apr 27 '13 at 21:13
  • both examples do not involve android code (except for #1 which you just use Date.getTime() ) . the rest is done in excel. for #2 , you create a new column which reference to the column of the date . if the column of the date is "A" and the new column is "C" you create the values of "C" cells according to "A" : "=DATEVALUE($A1)" for first cell, "=DATEVALUE($A2)" for second and so on. of course, instead of writing it each time you can drag the bottom-right icon of the cell to automatically create new cells. – android developer Apr 27 '13 at 21:41
  • :I want to make the plot right.I don't care so much for the excel file. – George Apr 28 '13 at 10:26
  • so if you don't care about the excel file, simply create your own app (using any programming language and platform you wish) that shows it well. the data is still fine and can be handled. – android developer Apr 28 '13 at 11:20
  • :But the problem is that I can't handle the data in the x axis plot. – George Apr 28 '13 at 11:26
  • why can't you handle the data in the x axis plot? excel can handle it just fine. – android developer Apr 28 '13 at 12:46
  • ok, since i never used it, i only have ideas: have you tried setting the x values to be strings instead ? or long values ? – android developer Apr 28 '13 at 13:51
  • :I tried to use either date or string values.But the problem isn't that.The problem is that the user enters some data in an edittext field and presses the save button.Then, the data and the date are being saved.So,during a day (the same day) the user may enter various data.And doing the plot I have equal points of data and date.But I want the data of one day to be bind in one day only. – George Apr 28 '13 at 13:55
  • sadly i don't know how to handle this library. if you can't find a solution, a workaround might be to use multiple legends/layers . – android developer Apr 28 '13 at 14:30
1

If I am not very much mistaken this is not a problem of saving or loading the data but simply of displaying the data. Your graph algorithm should recognize equal dates and do not make a new entry for it.

As it is, it seem like the date is treated as label, not as x-axis value, which would be reasonable because the date string is not numeric.

I suggest to check achartengine if there is a way to additional provide x-values and then let them only increase if the date string of the next entry is different of the previous entry.

You probably have to give a different model to achartengine.

I don't think it is a save problem because well the date stored is the right one, so any behavior there is mostly as expected.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
1

This is an AChartEngine issue indeed. The internal model used to be kept in ArrayLists and these issues didn't exist. At some point there was a community effort to make AChartEngine faster with a lot of data points. At that point, the model started to use a Map instead of an ArrayList. This implementation prevented having the same X value added several times. However, in order to fix this, I add a very small value to X, if it already exists. In your example, the first value is 20/04/2013 00:00:00.0, the second one is at 20/04/2013 00:00:00.001 and the third is 20/04/2013 00:00:00.002.

Now, the solution to your problem is to have a wider range on the X axis.

renderer.setXAxisMax(someDate.getTime());

where someDate can be something like 21/04/2013.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • :I did what you said and it seemed to work..I entered some data on 04/05/2013 and the plot was ok , as the one I have above.BUT , when I entered some other data in 05/05/2013 the 04/05/2013 in x axis was replaced by 05/05/2013 and in the y axis where all the data.. – George May 04 '13 at 22:53
  • :Maybe because I have "Date someDate=null;"?Also , I use "SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy",Locale.US);" for Dates.(Thank you for helping) – George May 04 '13 at 22:58
  • :I updated.The "dates_Strings" is a String List which contains the dates.I put the "somedate" as the first date which I save (use). – George May 05 '13 at 06:58
  • Just call the setXAxisMax() with some date that is later than the last value. – Dan D. May 05 '13 at 12:58
  • :I updated my post showing the code I use for that.I am still getting the same.Can you check if I am defining right the initial and last date?Also, I use lastDate=c.getTime() and then "mRenderer.setXAxisMax(lastDate.getTime());" (again lastDate.getTime()).I am not sure about the whole thing.I am trying to define initial date as the last value (mydata.size()-1) and then increase this date by one in order to put it in setXAxisMax.Thanks – George May 05 '13 at 21:14
  • I don't understand the issues you have now. – Dan D. May 06 '13 at 07:34
  • :It doesn't work.I enter data in 05/05/13 and then in 06/05/13 and the plot showed only 06/05/13 in x axis.The code I use for initial and lastdate ?Is it ok?You said to put a date that is later than the last value.So,i define an initial date as the "last date" and I add one day to it in order to have the lastdate.Because I don't know what the last day will be.The user may enter data in a whole month,in a week ,whenever he wants. – George May 06 '13 at 09:53
  • Always update the X axis max value to be the maximum entered date + 1 day for instance. – Dan D. May 06 '13 at 15:10
  • :I think the problem is in saving and loading!Because I saw that if I enter data another date it keeps only the last date in the file.So,I am accepting your answer and upvoting but can you please help me with that?Can you check my code for saving and loading the dates?Please,it is small. – George May 06 '13 at 15:13
  • :I think in loading I have a problem. – George May 06 '13 at 15:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29495/discussion-between-george-and-dan) – George May 06 '13 at 15:13
  • I probably don't understand the exact issue you have. I think I have answered the AChartEngine one you had. – Dan D. May 06 '13 at 15:16