2012-11-19 52 views
3

在我的應用程序中,我正在開發活動與餅圖比較五家公司的表現。Android PIE圖表在一個活動

對於這一點,我怎麼可以用餅圖操作方法。

我已經refered一個link ,但我得到了一些困難,以實現這一目標。幫我找到解決方案...

在此先感謝..

回答

6

樣本類到PIE圖表。

public class DrawGraph extends View { 
Paint p; 
private ArrayList<Integer> aList = new ArrayList<Integer>(); 
int width; 
int height; 
int bar_width; 
int bar_height; 
int bar_height1; 
int c[] = { Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, 
     Color.YELLOW }; 

public DrawGraph(Context context, ArrayList<Integer> data) { 
    super(context); 
    p = new Paint(); 
    aList = data; 
} 


public void draw(Canvas canvas) { 
    int x = getWidth(); 
    int y = getHeight(); 
    float t = getTotal(); 
    p.setColor(Color.parseColor("#78777D")); 
    p.setStyle(Style.STROKE); 
    p.setStrokeWidth(2); 
    canvas.drawRect(0, 0, x - 1, y - 1, p); 
    int n = aList.size(); 
    float curPos = -90; 
    p.setStyle(Style.FILL); 
    RectF rect = new RectF(20, 20, x - 20, y - 20); 
    for (int i = 0; i < n; i++) { 
     p.setColor(c[i]); 
     float thita = (t == 0) ? 0 : 360 * aList.get(i)/t; 
     canvas.drawArc(rect, curPos, thita, true, p); 
     curPos = curPos + thita; 
    } 
} 

private float getTotal() { 
    int total = 0; 
    for (int i = 0; i < aList.size(); i++) { 
     total = total + aList.get(i); 
    } 
    return total; 
} 
} 

看到這個,從你的活動中調用。

public class MyGraphActivity extends Activity { 
LinearLayout pane; 
private DrawGraph dg; 
ArrayList<Integer> aLIst = new ArrayList<Integer>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    pane = (LinearLayout) findViewById(R.id.pane); 

    aLIst.add(200); 
    aLIst.add(300); 
    aLIst.add(150); 
    aLIst.add(400); 

    dg = new DrawGraph(this, aLIst); 
    pane.addView(dg); 

    } 
} 
+0

在這個我怎麼稱呼活動? – gowri

+1

我編輯了我的答案,請參閱。 –

相關問題