afreechart Android eclipse esample

2019-07-14 08:06发布

问题:

I am trying to use AFreeChart to display a chart in my activity, I have checked so many documentation throw internet but I do not find any full example, I mean how to build a chart and to show it after in my Layout, I need to show it whining an GUI (same thing like an image in my GUI), y am using eclipse (android 4.2) for it.

Does anyone know how to use AfreeChart in android? Thanks

回答1:

Okay, since this is an older post I am not sure if you were able to find an answer for this question. This is what needs to be done to display a afree chart in your activity.

  1. Create a custom view extending ImageView, something like blelow

    public class ChartView extends ImageView
    {
        private Bitmap              bitmap;
        private RectShape           rectArea;
        private Canvas              canvas;
        private AFreeChart          chart;
    
        public ChartView( Context context, AttributeSet attributeSet )
        {
            super(context, attributeSet);
        }
    
        public ChartView( Context context )
        {
            super(context);
            intChart();
        }
    
        private void intChart()
        {
            //Setting different width and height based on the orientation.
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                bitmap = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
                rectArea = new RectShape(0.0, 0.0, 400, 200);
            }
            else
            {
                bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
                rectArea = new RectShape(0.0, 0.0, 200, 200);
            }
        }
    
        public void drawChart( AFreeChart chart )
        {
            canvas = new Canvas(bitmap);
            this.chart = chart;             
            this.chart.draw(canvas, rectArea);
            setImageBitmap(bitmap);
        }
    
        @Override
        protected void onDraw( Canvas canvas )
        {
            super.onDraw(canvas);               
        }
    }
    
  2. Create an activity, as shown below and you all set to go. I assume that you have already created your AFreeChart object to be passed to the view.

        public class ChartActivity extends Activity
        {
            @Override
            protected void onCreate( Bundle savedInstanceState )
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.chart);
    
                ViewGroup viewGroup = (ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content);
    
                ChartView chartView = new ChartView(this);
    
                chartView.drawChart(ChartFactory.createChart()/*Returns AFreechart object*/);       
    
                viewGroup.addView(chartView);
    
            }
        }
    
  3. chart.xml

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
            </LinearLayout>
    

Hope this helps