Android 小工具--饼状图

1.导入依赖

第一步,先添加配置代码

maven {
    
     url "https://jitpack.io" }

在这里插入图片描述
第二步,添加依赖(记得点击同步)

  implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

在这里插入图片描述

2.Fragment代码

1.布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/pc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

2.java代码


public class Bt_Fragment extends Fragment {
    
    
    PieChart pc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_zt_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    
    
        super.onActivityCreated(savedInstanceState);
        pc = (PieChart) getActivity().findViewById(R.id.pc);
        List<PieEntry> yVals = new ArrayList<>();
        int min = 40;
        int max = 50;
        Random random = new Random();
        float num1 = random.nextInt(max) % (max - min + 1) + min;
        int min2 = 20;
        int max2 = 40;
        float num2 = random.nextInt(max2) % (max2 - min2 + 1) + min2;

        float num3 = 100 - num2 - num1;
        yVals.add(new PieEntry(num1, "白天"));
        yVals.add(new PieEntry(num2, "夜晚"));
        yVals.add(new PieEntry(num3, "凌晨"));
        pc.setEntryLabelTextSize(10f);
        pc.setCenterText("工作时间");

        pc.setCenterTextSize(pc.getHoleRadius() / 3);

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#FF16E1A7"));
        colors.add(Color.parseColor("#0676D8"));
        colors.add(Color.parseColor("#FF7110FB"));

        PieDataSet pieDataSet = new PieDataSet(yVals, "");
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        String descriptionStr = "今日使用手机时间分布";
        Description description = new Description();
        description.setText(descriptionStr);
        description.setTextColor(Color.WHITE);
        description.setTextSize(16);
        pc.setDescription(description);
        pc.setHoleColor(Color.parseColor("#5510C6DD"));


        float s = (float) (pc.getHoleRadius() / (8.5));
        pc.setExtraOffsets(0f, s, 0f, s);
        description.setTextSize(10f);

        pc.setData(pieData);

    }
}

3.Activity代码

布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="0dp"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2.java代码

public class MainActivity extends AppCompatActivity {
    
    


    Bt_Fragment mBt_fragment=new Bt_Fragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,mBt_fragment).commit();


    }
}

运行
在这里插入图片描述

参考博客

官方文档

猜你喜欢

转载自blog.csdn.net/qq_46526828/article/details/108930166