用数据库生成android pie_chart

时间:2017-02-28 11:52:09

标签: android sqlite pie-chart

任何人都可以建议我使用数据库值生成饼图的最佳教程或示例代码。我已经访问过MPAndroidChart文档,但仍然令人困惑。谢谢!

1 个答案:

答案 0 :(得分:3)

我使用MPAndroid的饼图来显示出勤率

Add the following to your project level build.gradle:

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

    Add this to your app build.gradle:

dependencies {
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}
  

下面的Java代码

pieChartAttendance.setPressed(false);

        pieChartAttendance.setUsePercentValues(true);
        pieChartAttendance.setRotationEnabled(false);
        pieChartAttendance.setHoleRadius(0);
        pieChartAttendance.setDescription("");
        pieChartAttendance.animateXY(1000, 1000);
        pieChartAttendance.setDrawHoleEnabled(false);
        pieChartAttendance.invalidate();
        Legend pieLegend = pieChartAttendance.getLegend();
        pieLegend.setEnabled(false);
        ArrayList<Entry> arrayList = new ArrayList<Entry>();
        ArrayList<String> arrayListXAxis = new ArrayList<String>();

        //Percentage of Attendance
        float presentPercentage = (presentCount * 100) / totalAttendance;
        float absentPercentage = (absentCount * 100) / totalAttendance;


        arrayList.add(new Entry(presentPercentage, 0));
        arrayList.add(new Entry(absentPercentage, 1));

        arrayListXAxis.add("Present");
        arrayListXAxis.add("Absent");
        PieDataSet dataSet = new PieDataSet(arrayList, "");
        dataSet.setSliceSpace(2);
        dataSet.setSelectionShift(2);

        //Color Array
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.rgb(76, 175, 80));
        colors.add(Color.rgb(244, 67, 54));
        dataSet.setColors(colors);

        PieData piedata = new PieData(arrayListXAxis, dataSet);
        piedata.setValueFormatter(new PercentFormatter());
        piedata.setValueTextSize(11f);
        piedata.setValueTextColor(Color.WHITE);
        pieChartAttendance.setData(piedata);
  

Xml代码

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