Android GraphView,使用EditTextView将X和Y坐标添加到图形中

时间:2016-03-16 15:17:45

标签: java android android-layout android-graphview

所以,我正在使用android GraphView制作一个图形应用程序(Line Graph)。 我做了什么:

  1. 创建图表视图
  2. 为x创建2个文本视图(编辑仅接受数字的文本视图) 和y坐标
  3. 将点(x和y坐标)添加到图形中的按钮 更新新图表。
  4. 我通过以下功能调用按钮:writecor(View view) - (将坐标写入图表...)

    public void writecor(View view)
    {
        GraphView linegraph =  (GraphView)findViewById(R.id.graph);
        EditText xc,yc;
        int xv,yv;
        xc=(EditText)findViewById(R.id.xcor);
        yc=(EditText)findViewById(R.id.ycor);
        xv=Integer.parseInt(xc.getText().toString());
        yv=Integer.parseInt(yc.getText().toString());
        line_series  = new LineGraphSeries<DataPoint>(generatedata());
        line_series.appendData(new DataPoint(xv,yv),true,50);
        line_series.resetData(generatedata());
        linegraph.addSeries(line_series);
    }
    

    其中generatedata() - (包含预先存在的坐标)如下:

    private DataPoint[] generatedata()
    {
         DataPoint[] values =
                 {
                 new DataPoint(1,5),
                 new DataPoint(2,8),
                 new DataPoint(4,7),
                 new DataPoint(7,11)
    };
    

    代码显然是错误的,你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

DataPoint[] values; 
int size=0; 
private void generateData(int x,int y) {
    values = new DataPoint[size+1];
       DataPoint v = new DataPoint(x, y);
        values[size] = v;
    }

}
public void writecor(View view)
{
GraphView linegraph =  (GraphView)findViewById(R.id.graph);
EditText xc,yc;
int xv,yv;
xc=(EditText)findViewById(R.id.xcor);
yc=(EditText)findViewById(R.id.ycor);
xv=Integer.parseInt(xc.getText().toString());
yv=Integer.parseInt(yc.getText().toString());
generateData(int xv,int yv);
line_series  = new LineGraphSeries<DataPoint>(values);

}