亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消