为了在手机端显示项目管理系统中的质量报表,以及成本分布的报表,我们引入了 MPAndroidChart
开源地址:https://github.com/PhilJay/MPAndroidChart
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/page_bg"
android:orientation="vertical">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</LinearLayout>
2.java代码
package com.butterflypm.app.report;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.butterflypm.app.R;
import com.butterflypm.app.base.ISSFragment;
import com.butterflypm.helper.ViewHelper;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
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 com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
import java.util.List;
public class BugPieFragment extends ISSFragment {
private PieChart pieChart;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = LayoutInflater.from(getFromActivity()).inflate(R.layout.bugpie, container, false);
pieChart=view.findViewById(R.id.chart);
setupPieChart();
loadPieChartData();
pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.e("@@@@----->",e.toString());
}
@Override
public void onNothingSelected() {
}
});
return view;
}
private void setupPieChart() {
pieChart.setDrawHoleEnabled(true);
pieChart.setUsePercentValues(true);
pieChart.setEntryLabelTextSize(12);
pieChart.setEntryLabelColor(Color.BLACK);
pieChart.setCenterText("Spending by Category");
pieChart.setCenterTextSize(24);
pieChart.getDescription().setEnabled(false);
Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setEnabled(true);
}
private void loadPieChartData() {
ArrayList<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(0.2f, "Food & Dining"));
entries.add(new PieEntry(0.15f, "Medical"));
entries.add(new PieEntry(0.10f, "Entertainment"));
entries.add(new PieEntry(0.25f, "Electricity and Gas"));
entries.add(new PieEntry(0.3f, "Housing"));
ArrayList<Integer> colors = new ArrayList<>();
for (int color: ColorTemplate.MATERIAL_COLORS) {
colors.add(color);
}
for (int color: ColorTemplate.VORDIPLOM_COLORS) {
colors.add(color);
}
PieDataSet dataSet = new PieDataSet(entries, "Expense Category");
dataSet.setColors(colors);
PieData data = new PieData(dataSet);
data.setDrawValues(true);
data.setValueFormatter(new PercentFormatter(pieChart));
data.setValueTextSize(12f);
data.setValueTextColor(Color.BLACK);
pieChart.setData(data);
pieChart.invalidate();
pieChart.animateY(1400, Easing.EaseInOutQuad);
}
}