最初嘗試使用Google的AChartEngine繪製圖表。不過,我想用後退按鈕在不同的佈局上繪製它。到目前爲止,我沒有發現代碼有什麼問題,但是當它需要的時候圖表並沒有顯示出來。所以我有一個主要文件開始意圖啓動ShowGraph活動。此代碼正在工作,因爲它將我帶到適當的屏幕。所以問題是圖形沒有顯示在線性佈局中,只有按鈕纔會顯示。任何建議或錯誤,你可以找到?在單獨的活動中顯示繪製的AChartEngine圖形
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);
}
}
這不是「Google的」AChartEngine。 –