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

前端 未结 3 912
梦如初夏
梦如初夏 2021-01-17 00:05

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:

3条回答
  •  臣服心动
    2021-01-17 00:22

    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

    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 dates_Strings = new ArrayList();
    public static List data = new ArrayList();
    public static List date = new ArrayList();
    
    List mydata=new ArrayList();
    
    
    @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

    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

提交回复
热议问题