当前位置:   article > 正文

MPAndroidChart初次使用(1)——饼状图_mpandroidchart piechart饼图设置大小

mpandroidchart piechart饼图设置大小

应开发的需要,需要画各种各种图表,发现MPAndroidChart这个宝藏。
MPAndroidChart的Github链接
这里说明一下饼状图的画法,以及自己遇到的坑。

效果图

在这里插入图片描述

1.添加依赖

在build.gradle中添加依赖。
注意:依赖很重要,网上有很多MPAndroidChart的教程,你们会发现他们会有一定的差异。因为MPAndroidChart是在不断更新,所以他的使用方法也会发生变化。所以在看别人使用的教程之前,记得看看自己的依赖是否博主相同,我就遇到了这个坑(;´д`)ゞ

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. XML

如果是使用constraintlayout布局,记得添加约束。

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/contextPieChart"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintTop_toBottomOf="@+id/context_analysis" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3. 代码实现

        //饼状图
        PieChart contextPieChart = (PieChart) view.findViewById(R.id.contextPieChart);

//        添加数据
        ArrayList<PieEntry> contextPieData = new ArrayList<>();
        contextPieData.add(new PieEntry(25f,"Part1"));
        contextPieData.add(new PieEntry(25f,"Part2"));
        contextPieData.add(new PieEntry(5f,"Part3"));
        contextPieData.add(new PieEntry(5f,"Part4"));
        contextPieData.add(new PieEntry(30f,"Part5"));
        contextPieData.add(new PieEntry(10f,"Part6"));
//        label出现在饼状图的右下角,不想显示的话直接设置为空
        PieDataSet context_pieDataSet = new PieDataSet(contextPieData, "");

//        设置饼状图的颜色,有几个组成成分,就需要添加几个颜色
        List<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.bewitched_tree));
        colors.add(getResources().getColor(R.color.light_heart_blue));
        colors.add(getResources().getColor(R.color.magic_powder));
        colors.add(getResources().getColor(R.color.mustard_addicted));
        colors.add(getResources().getColor(R.color.true_blush));
        colors.add(getResources().getColor(R.color.merry));

//        设置饼状图颜色
        context_pieDataSet.setColors(colors);
//        各个饼直接的空白距离
        context_pieDataSet.setSliceSpace(2);
//        使用百分比
        contextPieChart.setUsePercentValues(true);
//        取消饼状图的中心圆
        contextPieChart.setDrawHoleEnabled(false);
//        旋转动画
        contextPieChart.animateXY(1400, 1400);
//        让饼状图往左移动30
        contextPieChart.setExtraLeftOffset(-30);

        PieData context_pieData = new PieData(context_pieDataSet);
//        显示数值
        context_pieData.setDrawValues(true);
//        设置饼状图上的字的大小
        context_pieData.setValueTextSize(12);
//        设置饼状图上的字的颜色
        context_pieData.setValueTextColor(WHITE);
//        使用setValueFormatter可以自定义如何显示数值,这里是显示成 数值%
        context_pieData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                NumberFormat nf = NumberFormat.getNumberInstance();
                nf.setMaximumFractionDigits(2);
                return nf.format(value) + "%";
            }
        });
        contextPieChart.setData(context_pieData);

//        去掉description(即图表描述,自己看需不需要)
        Description description = new Description();
        description.setText("");
        contextPieChart.setDescription(description);

//        Legend 是 提示(显示各个颜色分别代表的是什么
        Legend context_legend = contextPieChart.getLegend();
//        将 Legend 的位置设置到右上,垂直显示
        context_legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        context_legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        context_legend.setOrientation(Legend.LegendOrientation.VERTICAL);

//        legend内的各个部分的竖直间距
        context_legend.setYEntrySpace(5);
//        对lgend的位置进行调整
        context_legend.setXOffset(4);
//        设置 legend 的大小
        context_legend.setTextSize(12);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
Note:

这个是MPAndroidChart讲得比较好的博主写的教程MPAndroidChart 教程,但是因为是15年写的,有一些功能现在已经发生了变化。但是大体和原理没有什么变化,可以参考这个教程。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/254724?site
推荐阅读
相关标签
  

闽ICP备14008679号