scatter plot with dates at x axis — add custom labels on X axis

≯℡__Kan透↙ 提交于 2019-12-04 02:23:40

问题


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

回答1:


As told by Dan in his answer you can use renderer.addXTextLabel(x, "text"); to set x-axis labels.

In your case:: u made mistake at commented line , You where trying to add x-axis labels twice..

change it as shown below

  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
    }

UPDATE: Change Your code to following it wont crash on adding new data's

and if u want only points to be visible not linealong with point then change this ChartFactory.getLineChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy"); to 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;
}

}

LineGraph.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();
           }
}

}



回答2:


About scatter type of chart:

Maybe you could just set proper parameters to 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);

and when you are calling factory:

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

I'm aware that it is not clean and proper way, but I don't have android here to test it or to figure out more into details...

I didn't test it, so give feed back to me please :).

Hope it helps, Toni




回答3:


You can add custom labels this way:

renderer.addXTextLabel(x, "label");

If you want to keep using the time chart approach then you can do this:

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

where lineRenderer is a XYSeriesRenderer inside your XYMultipleSeriesRenderer.



来源:https://stackoverflow.com/questions/16089610/scatter-plot-with-dates-at-x-axis-add-custom-labels-on-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!