为了账号安全,请及时绑定邮箱和手机立即绑定

MPChart如何画饼图

标签:
Android

效果图:

https://img1.sycdn.imooc.com//5c5ced440001162d06481148.jpg

private PieChart chart;
private static String[] colors1 = {"#ffbb86", "#F37997", "#ff927d", "#AA99ED", "#79D2FF", "#49C9C9","#BBBBBB"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_piechart);

    setTitle("PieChartActivity");

    chart = (PieChart)findViewById(R.id.chart1);
    chart.setUsePercentValues(true);
    chart.getDescription().setEnabled(false);
    chart.setExtraOffsets(5, 10, 5, 5);

    chart.setDragDecelerationFrictionCoef(0.95f);

    chart.setCenterTextTypeface(tfLight);
    chart.setCenterText(generateCenterSpannableText());

    chart.setDrawHoleEnabled(true);
    chart.setHoleColor(Color.WHITE);

    chart.setTransparentCircleColor(Color.WHITE);
    chart.setTransparentCircleAlpha(110);

    chart.setHoleRadius(58f);
    chart.setTransparentCircleRadius(61f);

    chart.setDrawCenterText(true);

    chart.setRotationAngle(0);
    // enable rotation of the chart by touch
    chart.setRotationEnabled(true);
    chart.setHighlightPerTapEnabled(true);

    // chart.setUnit(" €");
    // chart.setDrawUnitsInChart(true);

    // add a selection listener
    chart.setOnChartValueSelectedListener(this);


    chart.animateY(1400, Easing.EaseInOutQuad);
    // chart.spin(2000, 0, 360);

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    // entry label styling
    chart.setEntryLabelColor(Color.WHITE);
    chart.setEntryLabelTypeface(tfRegular);
    chart.setEntryLabelTextSize(12f);

    setData(4,10);
}

private void setData(int count, float range) {
    ArrayList<PieEntry> entries = new ArrayList<>();

    // NOTE: The order of the entries when being added to the entries array determines their position around the center of
    // the chart.
    for (int i = 0; i < count ; i++) {

        entries.add(new PieEntry((float) ((Math.random() * range) + range / 5),
                parties[i],
                null));
    }

    PieDataSet dataSet = new PieDataSet(entries, "Election Results");

    dataSet.setDrawIcons(false);

    dataSet.setSliceSpace(3f);
    dataSet.setIconsOffset(new MPPointF(0, 40));
    dataSet.setSelectionShift(5f);


    ArrayList<Integer> colors = new ArrayList<>();


    colors.add(Color.parseColor(colors1[0]));
    colors.add(Color.parseColor(colors1[1]));
    colors.add(Color.parseColor(colors1[2]));
    colors.add(Color.parseColor(colors1[3]));
    dataSet.setColors(colors);
    //dataSet.setSelectionShift(0f);

    PieData data = new PieData(dataSet);
    //data.setValueFormatter(new PercentFormatter(chart));
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);
    data.setValueTypeface(tfLight);
    chart.setData(data);

    // undo all highlights
    chart.highlightValues(null);

    chart.invalidate();
}

数据源的设置:

ArrayList<PieEntry> entries = new ArrayList<>();

// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < count ; i++) {

    entries.add(new PieEntry((float) ((Math.random() * range) + range / 5),
            parties[i],
            null));
}

PieDataSet dataSet = new PieDataSet(entries, "Election Results");

代码:https://github.com/nickgao1986/MPChartExample

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消