aChartEngine不会在Android中显示图表

时间:2014-01-18 20:21:36

标签: android achartengine

这是我的LineGraph代码:

package com.example.testgraph;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

     private GraphicalView mChartView;
     private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
      /** The main renderer that includes all the renderers customizing a chart. */
      private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
      /** The most recently added series. */
      private XYSeries mCurrentSeries;
      /** The most recently created renderer, customizing the current series. */
      private XYSeriesRenderer mCurrentRenderer;
      /** Button for creating a new series of data. */
      private Button mNewSeries;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout mGraphParent = (LinearLayout)findViewById(R.id.graphParent);


        int x [] = {1,2,3};  
        int y [] = {30,40,50};  

        TimeSeries series = new TimeSeries("Line1");
        for (int i = 0; i < x.length;i++)
        {
            series.add(x[i],y[i]);

        } 
        Log.d("line","graph");
        XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();
        dataSet.addSeries(series);
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.BLACK);
        mRenderer.addSeriesRenderer(renderer);




          mChartView = ChartFactory.getLineChartView(this, dataSet, mRenderer);
          // enable the chart click events
          mRenderer.setClickEnabled(true);
          mRenderer.setSelectableBuffer(10);

          mGraphParent.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
              LayoutParams.FILL_PARENT));

          mChartView.repaint();


    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

        <LinearLayout
            android:id="@+id/graphParent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>

</RelativeLayout>

图表没有显示在屏幕上,我不知道在这个简单的实现中我哪里出错了。

2 个答案:

答案 0 :(得分:0)

我没有使用过AChartEngine,但我可以建议三种方法来调试它。

  1. 阅读文档,了解说明与代码的不同之处。

  2. 复制从the project's home page链接的其中一个示例。 This example很短。 project's demos是其功能的更长演示。一旦有效,你可以逐步进化并测试它。

  3. 想一想解释问题的假设,然后逐一检验这些假设。例如。也许mChartView在布局中不可见。您可以通过设置其背景颜色来测试它。也许视图是可见的,但整个图形是黑色的黑色。您可以通过选择不同的颜色来测试它。由于可以打破多项内容,因此最好先将程序简化为最低限度。

  4. P.S。 Java约定是编写int[] x = ...来声明int数组x。较旧的语法只是为了让C程序员更容易开始使用Java。

答案 1 :(得分:0)

尝试使用aChartEngine库的1.2版