0

I want to have a scatter plot with dates in x-axis.

Because if I use ChartFactory.getTimeChartView I receive a line graph and the problem is:

I make plot with current date as x axis and data (user input) as y axis.I store these data (date and user data) in Lists and also in a file.

The user may enter multiple times data during a day.And some of the data may be the same,so if I use a Linegraph the plot messes up.That's why I need scatter.

1)How can I do this?

2) Also, can someone point me to an example of adding custom labels on the X axis,in order to use a Linegraph?

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

I am trying to do this right now (i would like to know if it could be done as my questions above though)

I store dates as List String :

List<String> dates_asString=new ArrayList<String>();

I save them:

SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
        Date d=new Date();
String formattedDate=thedate.format(d);
        dates_asString.add(formattedDate);

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

In LineGraph:

private static List<String> dates_asString = new ArrayList<String>();
    private static List<Double> data = new ArrayList<Double>();
    private static List<Date> dates_asDates = new ArrayList<Date>();


loadfunc();    

//trying to copy the dates_asString to dates_asDates in order to use them in TimeSeries

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date convertedDate=new Date();
        try{
       for (int k=0;k<dates_asString.size();k++){
        convertedDate = formatter.parse(dates_asString.get(k));
        date_asDates.add(convertedDate);
       }
        }catch (ParseException e){

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        TimeSeries series = new TimeSeries("Showing data");
        for (int i=0;i<date_asDates.size();i++){    

            series.add(date_asDates.get(i),data.get(i));    
        }


mRenderer.setXLabels(0);
        for (int i=0;i<dates_asString.size();i++){
            mRenderer.addXTextLabel(i,dates_asString.get(i));

        }

public void loadfunc(){

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

    String formattedDate=thedate.format(d);

BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            ....
                     data.add(Double.parseDouble(splitLine[0]));
                    dates_asString.add(formattedDate);
4

3 回答 3

1

正如Dan 在他的回答中所说,您可以使用它renderer.addXTextLabel(x, "text");来设置 x 轴标签。

在你的情况下:: 你在注释行犯了错误,你试图添加 x 轴标签两次..

如下图所示进行更改

  TimeSeries series = new TimeSeries("Showing data");
    for (int i=0;i<date_asDates.size();i++){    

        series.add(i,data.get(i));    // only this change ll work as u needed
    }

更新:将您的代码更改为跟随它不会在添加新数据时崩溃

如果您只希望点与点不成直线可见,则将其更改ChartFactory.getLineChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");ChartFactory.getScatterChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");

MainActivity.java

package com.example.trying;
import java.io.BufferedWriter;
import java.io.File; // imports add all as before too

public class MainActivity extends Activity implements OnClickListener{


View savebtn;
View graphicsbtn;
EditText value,weight;
String filename = "mydata.csv";    
public static List<String> dates_Strings = new ArrayList<String>();
public static List<Double> data = new ArrayList<Double>();
public static List<Date> date = new ArrayList<Date>();

List<Double> mydata=new ArrayList<Double>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     date.clear();
     data.clear();
     dates_Strings.clear();
     //Set up click listeners
    savebtn=(View) findViewById(R.id.savebtn);
    savebtn.setOnClickListener(this);
    graphicsbtn=(View) findViewById(R.id.graphicsbtn);
    graphicsbtn.setOnClickListener(this);

    value=(EditText) findViewById(R.id.enter_data);


}


//called when a button is clicked
public void onClick(View v) {
    switch (v.getId()){
    case R.id.savebtn:
        savefunc();      
        break;
    case R.id.graphicsbtn:        
        Intent i = new Intent(this,LineGraph.class);        
        startActivity(i);
        break;

}
}

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




    try{
        double thedata=Double.parseDouble(value.getText().toString().trim());
        mydata.add(thedata);
        Log.d("tag","thedata :"+thedata);
    } catch (NumberFormatException e){
        String message="Sorry you did't type anything";

         Toast toast = Toast.makeText(getBaseContext(), message,Toast.LENGTH_SHORT);  
         toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
         toast.show();
  // Toast.makeText(getBaseContext(), "Sorry you did't type anything",    Toast.LENGTH_SHORT).show();

    }


    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");
          }
          value.setText("");
          bw.flush();
          bw.close();

        } catch (IOException e2) {
           e2.printStackTrace();
            }//catch
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

线图.java

package com.example.trying;

import java.io.BufferedReader;
import java.io.File; // add all other imports

public class LineGraph extends MainActivity {

String filename = "mydata.csv";    



private LinearLayout layout;
private GraphicalView mChartView;

protected void onCreate(Bundle savedInstanceState){

     super.onCreate(savedInstanceState);
     setContentView(R.layout.graph);
     layout = (LinearLayout) findViewById(R.id.chart);

    readfunc();       

    //trying to copy the dates_asString to date (Dates) in order to use them in TimeSeries
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date convertedDate;//=new Date();
    try{
   for (int k=0;k<dates_Strings.size();k++){
    convertedDate = formatter.parse(dates_Strings.get(k));
    date.add(convertedDate);
    Log.d("line","convertedDate :"+convertedDate);
   }
    }catch (ParseException e){

        // TODO Auto-generated catch block
        e.printStackTrace();
    }


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


    XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
    dataset.addSeries(series);


    //renderer for decay
    XYSeriesRenderer renderer =new XYSeriesRenderer();
    renderer.setColor(Color.YELLOW);
    renderer.setLineWidth(3);
    renderer.setPointStyle(PointStyle.SQUARE);


    XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.setXTitle("Date");
    mRenderer.setYTitle("Data");
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.BLACK);
    mRenderer.setMarginsColor(Color.BLACK);
    mRenderer.setXRoundedLabels(false);
    mRenderer.setShowCustomTextGrid(true);
    mRenderer.setXLabels(0);

    for (int i=0;i<date.size();i++){
        mRenderer.addXTextLabel(i,dates_Strings.get(i));

    }


     //mChartView = ChartFactory.getTimeChartView(this, dataset, mRenderer,"dd/MM/yyyy");

    //mChartView = ChartFactory.getScatterChartView(this, dataset, mRenderer);
    //layout.addView(mChartView);
    Intent intent= ChartFactory.getLineChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");  
    startActivity(intent);

 }



public void readfunc(){

SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
Date d=new Date();
String formattedDate=thedate.format(d);


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

    String s;

    FileInputStream fis;

   try {
      fis = new FileInputStream(file);

         BufferedReader br = new BufferedReader(new InputStreamReader(fis));

         do {
             s = br.readLine();     
             if (s != null ){
                 String[] splitLine = s.split(",");
                 data.add(Double.parseDouble(splitLine[0]));
                 //dates_Strings.add(thedate.parse(splitLine[1]));
                 dates_Strings.add(formattedDate);
       }                        
             } while (s != null );
          br.close();                      
       } catch (IOException e) {
          e.printStackTrace();
           }
}

}
于 2013-04-19T03:43:06.973 回答
0

关于散点图类型:

也许您可以为 XYSeriesRenderer 设置适当的参数:

XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.GREEN);//green point
r.setPointStyle(PointStyle.CIRCLE);//point in shape of circle
r.setFillPoints(true);//enable points
r.setLineWidth(0);//set line to 0 width - invisible
mRenderer.addSeriesRenderer(r);

当你打电话给工厂时:

mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,"H:mm:ss");

我知道这不是干净和正确的方法,但我没有 android 来测试它或找出更多细节......

我没有测试它,所以请给我反馈:)。

希望它有帮助,托尼

于 2013-04-19T12:34:21.387 回答
0

您可以通过以下方式添加自定义标签:

renderer.addXTextLabel(x, "label");

如果您想继续使用时间表方法,那么您可以这样做:

lineRenderer.setPointStyle(style);
lineRenderer.setLineWidth(0);

lineRenderer的. _XYSeriesRendererXYMultipleSeriesRenderer

于 2013-04-18T23:37:06.710 回答