Search code examples
javaandroidachartengine

save user data during a day (the same day -> many user data)


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

Solution

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