MPAndroidChart :PieChart(圆头饼状图)

 

 


package com.xxmassdeveloper.mpchartexample;

import android.graphics.Color;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

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.formatter.PercentFormatter;

import java.util.ArrayList;

public class PiePolylineChartActivity2 extends AppCompatActivity{

    private PieChart chart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_piechart2);

        chart = findViewById(R.id.chart1);
        chart.setUsePercentValues(true);
        chart.getDescription().setEnabled(false);
        chart.setExtraOffsets(5, 10, 5, 5);

        chart.setDragDecelerationFrictionCoef(0.95f);

        chart.setCenterText("18\n中运动");
        chart.setCenterTextSize(10);

        chart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);// 和四周相隔一段距离,显示数据

        chart.setDrawHoleEnabled(true);//设置是否启用空洞
        chart.setHoleColor(Color.WHITE);//设置空洞颜色

        chart.setTransparentCircleColor(Color.WHITE);//设置半透明圆颜色,默认为白色
        chart.setTransparentCircleAlpha(110);//设置半透明圆透明度

        chart.setHoleRadius(58f);//设置空洞半径
        chart.setTransparentCircleRadius(0f);//设置半透明圆环的半径, 0为透明

        chart.setDrawCenterText(true);//是否绘制PieChart内部中心文本


        chart.setDrawEntryLabels(false);//设置pieChart是否只显示饼图上百分比不显示文字
        // chart.spin(2000, 0, 360);

        chart.setDrawRoundedSlices(true);//设置pieChart图表是圆头
        chart.setRotationEnabled(false);//设置pieChart图表是否可以手动旋转
        // 不显示图例
        Legend legend = chart.getLegend();
        legend.setEnabled(false);
        setData();
    }


    private void setData() {

        ArrayList<PieEntry> entries = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();

        entries.add(new PieEntry(10, "101"));
        entries.add(new PieEntry(20, "201"));
        entries.add(new PieEntry(30, "301"));
        entries.add(new PieEntry(40, "401"));
        entries.add(new PieEntry(50, "401"));
        entries.add(new PieEntry(60, "401"));
        entries.add(new PieEntry(70, "401"));
        entries.add(new PieEntry(80, "401"));
        entries.add(new PieEntry(90, "401"));
        entries.add(new PieEntry(100, "401"));
        colors.add(Color.rgb(207, 248, 246));
        colors.add(Color.rgb(148, 212, 212));
        colors.add(Color.rgb(136, 180, 187));
        colors.add(Color.rgb(118, 174, 175));
        colors.add(Color.rgb(42, 109, 130));
        colors.add(Color.rgb(207, 248, 246));
        colors.add(Color.rgb(148, 212, 212));
        colors.add(Color.rgb(136, 180, 187));
        colors.add(Color.rgb(118, 174, 175));
        colors.add(Color.rgb(42, 109, 130));


        PieDataSet dataSet = new PieDataSet(entries, "Election Results");
        dataSet.setSliceSpace(0f);// 设置饼块之间的间隔
        dataSet.setSelectionShift(5f);
        dataSet.setColors(colors);
        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.2f);
        dataSet.setValueLinePart2Length(0.4f);

        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
        chart.setData(data);

        chart.highlightValues(null);

        chart.invalidate();
    }


}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart1"
        android:layout_centerInParent="true"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</RelativeLayout>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值