MPAndroidChart_饼图的那些事

版权声明:https://blog.csdn.net/petterp https://blog.csdn.net/petterp/article/details/85982395

MPAndroidChart攻略第一步——LineChart的点点滴滴。

带你入门折线图的基本使用,各种属性的设置,自定义轴上的标签,及去除边框线与轴线,和MarkView提示的使用。

MPAndroidChart_折线图的那些事

MPAndroidChart_饼图的那些事

MPAndroidChart_动态柱状图

MPAndroidChart_水平条形图的那些事

MPAndroidChart_并列柱状图,及如何实现点击隐藏掉不需要的条目。

目录

从简易Demo开始

1. 百分比的设置

2. 标签的设置(标签就是扇形图里的文字)

3. 饼心的设置

4. 透明圆的设置(即饼心旁边的的圆环)

5. 设置图表变化监听

6. 设置折线饼图

7. 設置突出时的间距

8. 设置图例

9. 设置动画

      -1. x轴动画

      -2. y轴动画 

      -3. xy轴动画

10. 其他属性的设置


从简易Demo开始

我们先做一个简易的饼图,然后开始对它进行丰富,完成对常用API的熟悉。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="500dp" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private PieChart chart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        chart=findViewById(R.id.chart);
        info();
    }
    private void info(){
        ArrayList<PieEntry> entries=new ArrayList<>();
        entries.add(new PieEntry(10,"晴天"));
        entries.add(new PieEntry(30,"雨天"));
        entries.add(new PieEntry(40,"阴天"));
        entries.add(new PieEntry(30,"多云转阴"));
        setData(entries);
    }
    private void setData(ArrayList<PieEntry> list){
        //label为""的话,图例下面也就是空白
        PieDataSet dataSet=new PieDataSet(list,"Petterp");
        ArrayList<Integer> colors=new ArrayList<>();
        for (int a:ColorTemplate.VORDIPLOM_COLORS){
            colors.add(a);
        }
        dataSet.setColors(colors);
        PieData data=new PieData(dataSet);
        chart.setData(data);
        chart.highlightValue(null);
        chart.invalidate();
    }
}

效果如下

看起来挺简单吧,接下来我们就开始对它的常用方法进行熟悉吧。


百分比的设置

 dataSet.setValueFormatter(new PercentFormatter());
 dataSet.setValueTextSize(30f);
 dataSet.setValueTextColor(Color.BLACK);


 标签的设置(标签就是扇形图里的文字)

//标签的颜色
chart.setEntryLabelColor(Color.BLUE);
//标签的大小
chart.setEntryLabelTextSize(20f);
//设置是否隐藏标签
 chart.setDrawEntryLabels(false);


 饼心的设置

        //显示饼心,默认显示
        chart.setDrawHoleEnabled(true);
        //设置饼心的颜色
        //设置饼心的半径,默认为50%
        chart.setHoleRadius(50f);
        chart.setHoleColor(Color.WHITE);
        //是否显示在饼心的文本
        chart.setDrawCenterText(true);
        //设置饼心显示的文字
        chart.setCenterText(new SpannableString("Petterp"));
        //设置饼心字体大小
        chart.setCenterTextSize(20);
        //设置中心文本的偏移量
        chart.setCenterTextOffset(30,0);


 透明圆的设置(即饼心旁边的的圆环)

         //启用透明圆
        chart.setDrawHoleEnabled(true);
        //设置透明圆的半径,默认为比饼心的半径大5%
        chart.setTransparentCircleRadius(60);
        //设置透明圆的透明度,默认为100,255=不透明,0=全透明
        chart.setTransparentCircleAlpha(120);
        //设置透明圆的颜色
        chart.setTransparentCircleColor(Color.WHITE);


 设置图表变化监听

        //设置图表变化监听
        chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                Toast.makeText(Main2Activity.this, "我被点击了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected() {

            }
        });


 设置折线饼图

        //设置折线的颜色
        dataSet.setValueLineColor(Color.BLACK);
        //设置数据线距离图像内部园心的距离,以百分比来计算
        dataSet.setValueLinePart1OffsetPercentage(100f);
        //当valuePosition在外部时,表示行前半部分的长度(即折线靠近圆的那端长度)
        dataSet.setValueLinePart1Length(0.5f);
        ///当valuePosition位于外部时,表示行后半部分的长度*(即折线靠近百分比那端的长度)
        dataSet.setValueLinePart2Length(0.1f);
        //设置Y值的位置在圆外
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);


 設置突出时的间距

         //选中时突出的长度
        dataSet.setSelectionShift(30);
        //饼块之间的间隔
        dataSet.setSliceSpace(10f);


设置图例

Legend legend=chart.getLegend();
        //设置图例的实际对齐方式
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        //设置图例水平对齐方式
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        //设置图例方向
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        //设置图例是否在图表内绘制
        legend.setDrawInside(false);
        //设置水平图例之间的空间
        legend.setXEntrySpace(5f);
        //设置垂直轴上图例条目间的空间
        legend.setYEntrySpace(0F);
        //设置x轴偏移量
        legend.setXOffset(50f);
        //设置此轴上的标签使用的y轴偏移量。对于图例,*高偏移量意味着整个图例将被放置在离顶部*更远的地方。
        legend.setYOffset(0f);
        //设置字体大小
        legend.setTextSize(20f);


设置动画

      x轴动画

//默认动画  
 chart.animateX(2000);

      y轴动画 

//默认动画
chart.animateY(2000);

 

xy轴动画

//默认动画
chart.animateXY(2000,2000);



其他属性的设置

        //设置图表偏移量
        chart.setExtraOffsets(5f, 5f, 5f, 5f);
        //设置可触摸
        chart.setRotationEnabled(true);
        //*减速摩擦系数为[o];1] interval,数值越高*表示速度下降越慢,例如设置为o,则*立即停止。1为无效值,将自动转换为*0.999f。
        chart.setDragDecelerationFrictionCoef(0.9f);

最后附上完整代码

public class MainActivity extends AppCompatActivity {
    private PieChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chart = findViewById(R.id.chart);
        info();
    }

    private void info() {
        //启动以百分比绘制
        chart.setUsePercentValues(true);
        chart.setEntryLabelColor(Color.BLUE);
        chart.setEntryLabelTextSize(20f);

        //显示饼心,默认显示
        chart.setDrawHoleEnabled(true);
        //设置饼心的颜色
        chart.setHoleColor(Color.WHITE);
        //设置饼心的半径,默认为50%
        chart.setHoleRadius(50f);
        //是否显示在饼心的文本
        chart.setDrawCenterText(true);
        //设置饼心显示的文字
        chart.setCenterText(new SpannableString("Petterp"));
        //设置饼心字体大小
        chart.setCenterTextSize(20);
        //设置中心文本的偏移量
        chart.setCenterTextOffset(30, 0);

        //设置透明圆的半径,默认为比饼心的半径大5%
        chart.setTransparentCircleRadius(60);
        //设置透明圆的透明度,默认为100,255=不透明,0=全透明
        chart.setTransparentCircleAlpha(120);
        //设置透明圆的颜色
        chart.setTransparentCircleColor(Color.WHITE);

        //设置图表变化监听
        chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                Toast.makeText(Main2Activity.this, "我被点击了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected() {

            }
        });

        Legend legend = chart.getLegend();
        //设置图例的实际对齐方式
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        //设置图例水平对齐方式
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        //设置图例方向
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        //设置图例是否在图表内绘制
        legend.setDrawInside(false);
        //设置水平图例之间的空间
        legend.setXEntrySpace(5f);
        //设置垂直轴上图例条目间的空间
        legend.setYEntrySpace(0F);
        //设置x轴偏移量
        legend.setXOffset(50f);
        //设置此轴上的标签使用的y轴偏移量。对于图例,*高偏移量意味着整个图例将被放置在离顶部*更远的地方。
        legend.setYOffset(0f);
        //设置字体大小
        legend.setTextSize(20f);

        //设置x轴动画
//        chart.animateX(2000);
//        //设置y轴动画
//        chart.animateY(2000);
        //设置xy轴动画
        chart.animateXY(2000, 2000);

        //设置图表偏移量
        chart.setExtraOffsets(5f, 5f, 5f, 5f);
        //设置可触摸
        chart.setRotationEnabled(true);
        //*减速摩擦系数为[o];1] interval,数值越高*表示速度下降越慢,例如设置为o,则*立即停止。1为无效值,将自动转换为*0.999f。
        chart.setDragDecelerationFrictionCoef(0.9f);

        ArrayList<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(10, "晴天"));
        entries.add(new PieEntry(30, "雨天"));
        entries.add(new PieEntry(40, "阴天"));
        entries.add(new PieEntry(30, "多云转阴"));
        setData(entries);
    }

    private void setData(ArrayList<PieEntry> list) {
        PieDataSet dataSet = new PieDataSet(list, "Petterp");
        ArrayList<Integer> colors = new ArrayList<>();
        for (int a : ColorTemplate.VORDIPLOM_COLORS) {
            colors.add(a);
        }

        //显示百分比号
        dataSet.setValueFormatter(new PercentFormatter());
        dataSet.setValueTextSize(20f);
        dataSet.setValueTextColor(Color.BLACK);
        dataSet.setColors(colors);

        //设置折线饼图
        //设置折线的颜色
        dataSet.setValueLineColor(Color.BLACK);
        //设置数据线距离图像内部园心的距离,以百分比来计算
        dataSet.setValueLinePart1OffsetPercentage(100f);
        //当valuePosition在外部时,表示行前半部分的长度(即折线靠近圆的那端长度)
        dataSet.setValueLinePart1Length(0.5f);
        ///当valuePosition位于外部时,表示行后半部分的长度*(即折线靠近百分比那端的长度)
        dataSet.setValueLinePart2Length(0.1f);
        //设置Y值的位置在圆外
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        //选中时突出的长度
        dataSet.setSelectionShift(30);
        //饼块之间的间隔
        dataSet.setSliceSpace(10f);
        
        PieData data = new PieData(dataSet);
        chart.setData(data);
        //取消高亮显示
        chart.highlightValue(null);
        chart.invalidate();
    }
}

好了,以上就是饼图常用的方法,以后开发中,如果有新的使用方法及修正之处,也会及时更新的。如果有帮到你的地方,不胜荣幸。 

猜你喜欢

转载自blog.csdn.net/petterp/article/details/85982395