在单独的活动中显示绘制的AChartEngine图

时间:2012-10-22 16:56:57

标签: android button android-linearlayout achartengine

最初尝试使用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); 

}
}

1 个答案:

答案 0 :(得分:0)

lineGraphHandler方法不会在任何地方调用。