最初尝试使用 Google 的AChartEngine绘制图表。但是,我想用后退按钮将它绘制在不同的布局上。到目前为止,我找不到代码有任何问题,但图表在需要时没有显示。所以我有一个主文件,它启动了启动 ShowGraph 活动的意图。此代码有效,因为它将我带到适当的屏幕。所以问题是图表没有显示在线性布局中,只有按钮显示。您能找到任何建议或错误吗?
buttonConfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (radioButtonGraph.isChecked()){
Intent sg = new Intent (MainActivity.this, ShowGraph.class);
startActivity(sg);
}
在显示图形屏幕上,xml 如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonBackGraph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/coolbutton"
android:text="@string/doneAlarm" >
</Button>
<LinearLayout
android:id="@+id/graphLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</LinearLayout>
使用此布局的活动有一个按钮,以及对图形函数的调用:
public class ShowGraph extends Activity{
Button buttonDone1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.avgraph);
buttonDone1 = (Button)findViewById(R.id.buttonBackGraph);
buttonDone1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent dne = new Intent(ShowGraph.this, MainActivity.class);
startActivity(dne);
}
});
}
public void lineGraphHandler(View view){
GraphActivity line = new GraphActivity();
GraphicalView gView = line.getView(getApplicationContext());
LinearLayout l1 = (LinearLayout)findViewById(R.id.graphLayout);
l1.addView(gView);
}
}
最后,呈现图形并应该在上面的线性布局中绘制的图形函数:
public class GraphActivity extends Activity {
public GraphicalView getView(Context context){
//hardcoded plotting data for now
int[]x = {1,2,3,4,5,6,7,8,9,10};
int[]y ={130, 150, 358, 200, 90,60, 110, 120, 130, 150};
TimeSeries series = new TimeSeries ("BGL Data");
for (int i=0; i<x.length;i++){
series.add(x[i], y[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
mrenderer.addSeriesRenderer(renderer);
mrenderer.setChartTitle("Blood Glucose Levels Tracking");
mrenderer.setXTitle("Test Date and Time Reference Number"); //number corresponding to date and time of test
mrenderer.setXAxisMin(0);
mrenderer.setXAxisMax(10);
mrenderer.setYTitle("Blood Glucose Level/ mg/dl");
return ChartFactory.getLineChartView(context, dataset, mrenderer);
}
}