MPAndroidChart3 - pieChart(饼状图)

重点:
https://blog.csdn.net/qq_20553007/article/details/82793487

细节学习-方法解释
https://blog.csdn.net/Honiler/article/details/80073883
https://blog.csdn.net/Morling0811/article/details/77450730

package com.eshore.cloud.activitys.ai.list;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.eshore.cloud.R;
import com.eshore.cloud.activitys.BaseActivity;
import com.eshore.cloud.utils.L;
import com.eshore.cloud.utils.Tools;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class AIListActivity extends BaseActivity {
    private PieChart picChartView;

    private Typeface tf;

    protected String[] mParties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.setBarColor(this);
        setContentView(R.layout.activity_ailist);

        picChartView = (PieChart) findViewById(R.id.pie_chart_with_line);
        /**
         * 是否使用百分比
         */
        picChartView.setUsePercentValues(true);
        /**
         * 描述信息
         */
        /*Description tempDescription=new Description();
        tempDescription.setText("描述信息");
        pie_chart_with_line.setDescription(tempDescription);*/
        picChartView.getDescription().setEnabled(false);

        //tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

        /**
         * 设置圆环中间的文字
         */
        picChartView.setCenterText(generateCenterSpannableText());
        /**
         * 圆环距离屏幕上下上下左右的距离
         */
        picChartView.setExtraOffsets(5f, 5.f, 5.f, 5.f);
        /**
         * 是否显示圆环中间的洞
         */
        picChartView.setDrawHoleEnabled(true);
        /**
         * 设置中间洞的颜色
         */
        picChartView.setHoleColor(Color.WHITE);
        /**
         * 设置圆环透明度及半径
         */
        picChartView.setTransparentCircleColor(Color.YELLOW);
        picChartView.setTransparentCircleAlpha(110);
        picChartView.setTransparentCircleRadius(61f);

        /**
         * 设置圆环中间洞的半径
         */
        picChartView.setHoleRadius(60f);

        /**
         * 是否显示洞中间文本
         */
        picChartView.setDrawCenterText(true);

        /**
         *触摸是否可以旋转以及松手后旋转的度数
         */
        picChartView.setRotationAngle(20);
        // enable rotation of the chart by touch
        picChartView.setRotationEnabled(false);

        picChartView.setDrawEntryLabels(false);
        //选中是否高亮,放大显示
        picChartView.setHighlightPerTapEnabled(false);
        /**
         *add a selection listener 值改变时候的监听
         */
       /* pie_chart_with_line.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                L.w("tan","Entry="+e);
                //e.getY() 等于 tempPieEntry.getValue()
                showToast(""+e.getData());
                PieEntry tempPieEntry=(PieEntry)e;
                //tempPieEntry.getValue()
                //tempPieEntry.getLabel()
                L.w("tan","tempPieEntry="+tempPieEntry);
            }

            @Override
            public void onNothingSelected() {

            }
        });*/



        //setData(3, 100);
        setData2(3);

        //圆环外面文字列表样式
        Legend l = picChartView.getLegend();
        //l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_INSIDE);
        l.setEnabled(false);
        l.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        l.setForm(Legend.LegendForm.CIRCLE);

        l.setFormSize(8f);
        l.setFormToTextSpace(4f);
        l.setXEntrySpace(6f);

        //pie_chart_with_line.animateY(1400, Easing.EasingOption.EaseInOutQuad);


    }

    private SpannableString generateCenterSpannableText() {
        SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
        s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
        s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
        return s;
    }

    private void setData2(int count){
        float mult = 100;
        ArrayList<PieEntry> yVals = new ArrayList<PieEntry>();
        for (int i = 0; i < count + 1; i++) {
            float value=(float) (Math.random() * mult) + mult / 5;
            String name=mParties[i % mParties.length];
            L.w("tan","v="+value+",n="+name);
            yVals.add(new PieEntry(value, name));
        }

        /*List<Integer> colors = new ArrayList<>();
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.BLACK);
        colors.add(Color.YELLOW);*/
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());

        PieDataSet pieDataSet = new PieDataSet(yVals, "Election Results");
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        //圆环内 是否显示百分比文字
        pieData.setDrawValues(false);
        picChartView.setData(pieData);
        picChartView.highlightValues(null);
        picChartView.invalidate();

    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
饼状图是一种常用的数据可视化图表,在Android开发中,可以使用MPAndroidChart库来实现饼状图的绘制。MPAndroidChart是一个功能强大且易于使用的开源图表库,提供了丰富的图表类型和自定义选项。 要在Android项目中使用MPAndroidChart库,首先需要在项目的build.gradle文件中添加依赖: ``` dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' } ``` 接下来,在布局文件中添加一个`PieChart`视图: ```xml <com.github.mikephil.charting.charts.PieChart android:id="@+id/pie_chart" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后,在代码中获取`PieChart`实例,并设置数据和样式: ```java PieChart pieChart = findViewById(R.id.pie_chart); // 创建数据集 ArrayList<PieEntry> entries = new ArrayList<>(); entries.add(new PieEntry(40f, "Label 1")); entries.add(new PieEntry(30f, "Label 2")); entries.add(new PieEntry(20f, "Label 3")); entries.add(new PieEntry(10f, "Label 4")); PieDataSet dataSet = new PieDataSet(entries, "Pie Chart"); // 设置每个数据块的颜色 dataSet.setColors(ColorTemplate.COLORFUL_COLORS); PieData data = new PieData(dataSet); pieChart.setData(data); // 设置饼状图的样式和交互选项 pieChart.setDrawHoleEnabled(true); pieChart.setHoleColor(Color.WHITE); pieChart.setTransparentCircleColor(Color.WHITE); pieChart.setTransparentCircleAlpha(110); pieChart.setHoleRadius(58f); pieChart.setTransparentCircleRadius(61f); pieChart.setDrawCenterText(true); pieChart.setRotationEnabled(true); pieChart.setHighlightPerTapEnabled(true); pieChart.animateY(1400, Easing.EaseInOutQuad); // 更新视图 pieChart.invalidate(); ``` 这样就可以在Android应用中绘制一个简单的饼状图了。你可以根据需要调整样式和交互选项,以满足具体需求。更多关于MPAndroidChart的使用和自定义选项,请参考官方文档和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值