Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

android - achartengine - can't figure how to use dates as x axis - the file I save is empty

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)+"
");
   ...

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...