通过for循环在DataPoint中放置多个值(GraphView)

时间:2017-07-27 12:52:42

标签: android android-graphview datapoint

我正在开发一个应用程序,在应用程序内部,我需要画一些线图。我研究并找到了一个工具,但问题如下:

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

}

确切地说,我需要使用for循环填充DataPoint变量,因为数据将由用户输入,但是当我尝试实现for循环时,它会生气并且我不知道如何绕过它。如果有人在任何帮助之前对这种事情进行了任何帮助,我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

做这样的事,

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

LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);

答案 1 :(得分:0)

如果您不知道有多少DataPoints,可以使用ArrayList:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="utf-8">
  <title>Guess the Number</title>
  <link rel="stylesheet" href="css/Lab6.css" />
  <script type="text/javascript" src="script/Lab6.js"></script>
</head>

<body onload="pickInteger()">
  <div>
    <h2><strong>Guess the Number</strong></h2>
  </div>
  <br/>
  <div id="formDiv">
    <form name="AForm" method="get">
      <p>The computer has picked a number between 1 - 99, you must choose the correct number to win the game. When you guess the right number the game will restart.<br/>
      </p>
      <div id="bodyDiv">
        <p> Your guess is:
          <input id="guess" type="text" size="1" name="theData" value="" autofocus/>
          <input type="button" name="mybutton" value=" Guess " onclick="checkGuess()">
        </p>
        <p id="output">
        </p>
      </div>
    </form>
  </div>
</body>

</html>