我在主活动类中动态地实现了graphview。
没有结果显示在里面。
活动在启动时保持空白。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GraphView
int num = 150;
GraphViewData[] data = new GraphViewData[num];
double v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(v));
}
GraphView graphView = new LineGraphView(this, "GraphViewDemo");
// add data
GraphViewSeries graphViewSeries=new GraphViewSeries(data);
graphView.addSeries(graphViewSeries);
// set view port, start=2, size=40
graphView.setViewPort(2, 40);
graphView.setScrollable(true);
// optional - activate scaling / zooming
graphView.setScalable(true);
Log.d("2", "closed");
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(graphView);
//GraphhView
}
我错过了什么,但什么都没有。
最佳答案
您需要在代码中创建实现GraphViewDataInterface
的graphviewdata类,如下例所示:
public class GraphViewData implements GraphViewDataInterface {
private double x,y;
public GraphViewData(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public double getX() {
return this.x;
}
@Override
public double getY() {
return this.y;
}
}