2012-10-23 46 views
-1

我想显示一个图表,我用另一个View元素的AChartEngineLibrary中的GraphicalView创建。 我应该这样做,因为我使用其他视图元素来接收用于更新图表的消息。 我不知道如何在视图元素中显示视图元素。 有人可以帮我吗? 预先感谢您。查看元素android

回答

0
without third parties like AChartEngineLibrary also we will implement charts. below is the sample code for without third party this is the piechart example 

package com.rmn.piechart; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.LinearLayout; 

public class AndroidPieChartActivity extends Activity { 
    /** Called when the activity is first created. */ 
    float values[]={200,400,200,500,300}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LinearLayout linear=(LinearLayout) findViewById(R.id.linear); 
     values=calculateData(values); 
     MyGraphview graphView = new MyGraphview(this, values); 
     graphView.setPadding(50, 30, 35, 0); 
     linear.addView(graphView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 

    } 
    private float[] calculateData(float[] data) { 

     float total=0; 
     for(int i=0;i<data.length;i++) 
     { 
      total+=data[i]; 
     } 
     for(int i=0;i<data.length;i++) 
     { 
      data[i]=360*(data[i]/total); 
     } 
     return data; 

    } 
    public class MyGraphview extends View 
    { 
     private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG); 
     private float[] value_degree; 
     private int[] COLORS={Color.BLUE,Color.GREEN,Color.GRAY,Color.CYAN,Color.RED}; 
     RectF rectf = new RectF (10, 10, 200, 200); 
     int temp=0; 
     public MyGraphview(Context context, float[] values) { 
      super(context); 
      value_degree=new float[values.length]; 
      for(int i=0;i<values.length;i++) 
      { 
       value_degree[i]=values[i]; 
      } 
     } 
     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 

      for (int i = 0; i < value_degree.length; i++) {//values2.length; i++) { 
       if (i == 0) { 
        paint.setColor(COLORS[i]); 
        canvas.drawArc(rectf, 0, value_degree[i], true, paint); 
       } 
       else 
       { 
         temp += (int) value_degree[i - 1]; 
         paint.setColor(COLORS[i]); 
         canvas.drawArc(rectf, temp, value_degree[i], true, paint); 
       } 
      } 
     } 

    } 
}