androidandroid-graphviewdatapoint

Putting multiple values in DataPoint via for loop (GraphView)


I'm currently developing an application and inside the app, I need to draw some line graph. I researched and found a tool for this but the problem is below:

public class graphActivity extends AppCompatActivity {

GraphView graph;

SharedPrefManager sharedPrefManager = new SharedPrefManager();

int exercisePosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //getting the exercise position from caller activity and proceeding 
    //accordingly...
    if(savedInstanceState == null){
        Bundle extras = getIntent().getExtras();
        if(extras == null){
            //do nothing
        } else{
            exercisePosition = extras.getInt("position");

        }
    } else{
        exercisePosition = (int) savedInstanceState.getSerializable("position");
    }

    graph = (GraphView) findViewById(R.id.graph);

    ArrayList<Exercise> exercises= sharedPrefManager.readSharedPref(this);

    //getting the exercise from exercises array list so that the specific graph for the exercise can be shown.
    Exercise exercise = exercises.get(exercisePosition);


    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
            //need to put for loop here but I can't...
            //new DataPoint(x,y);
    });

    graph.addSeries(series);



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}

To be exact, I need to fill that DataPoint variables using for loop since the data will be entered by the user but when I try to implement for loop it get angry and I don't know how to get around it. If anyone dealth with this kind of thing before any help is appreciated.


Solution

  • Do something like this,

    DataPoint[] dp = new DataPoint[10];
    for(int i=1;i<=10;i++){
        dp[i] = new DataPoint(x, y);
    }
    
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);