Activity 中的 Android PIE 图表

标签 android charts pie-chart

在我的应用程序中,我正在开发带有饼图的 Activity 以比较五家公司的业绩。

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

我引用了一个 link ,但我在实现这一目标时遇到了一些困难。帮助我找到解决方案...

提前谢谢你..

最佳答案

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

看到这个,从你的 Activity 中调用。

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

  }
}

关于 Activity 中的 Android PIE 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447783/

相关文章:

javascript - 流程图是否可以添加额外的图例

javascript - 拉斐尔 JS 派 : rotate the slice

android - 在 Android Studio 中创建彩色饼图

android - Unity GoogleMobileAdsMediationTestSuite 抛出异常 setAdListener 必须在主 UI 线程上调用

android - Firestore 并发连接是否限制为 1M?

java - 具有多个类别的箱线图的 JFreeChart 缩放

javascript - 将标签放置在饼图切片内(Highchart)

java - 通过 bouncy caSTLe 在 android 中读取证书

android - 当键盘打开时,布局应该在 android 中移动到上方

MySQL生成年龄金字塔