3

在我的应用程序中,我正在使用饼图开发活动,用于比较五家公司的业绩。

为此,我该如何使用饼图操作..

我已经引用了一个链接 ,但我遇到了一些困难来实现这一点。帮助我找到解决方案...

先感谢您..

4

1 回答 1

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);

  }
}
于 2012-11-19T04:50:39.847 回答