效果图:
private PieChart chart; private static String[] colors1 = {"#ffbb86", "#F37997", "#ff927d", "#AA99ED", "#79D2FF", "#49C9C9","#BBBBBB"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_piechart); setTitle("PieChartActivity"); chart = (PieChart)findViewById(R.id.chart1); chart.setUsePercentValues(true); chart.getDescription().setEnabled(false); chart.setExtraOffsets(5, 10, 5, 5); chart.setDragDecelerationFrictionCoef(0.95f); chart.setCenterTextTypeface(tfLight); chart.setCenterText(generateCenterSpannableText()); chart.setDrawHoleEnabled(true); chart.setHoleColor(Color.WHITE); chart.setTransparentCircleColor(Color.WHITE); chart.setTransparentCircleAlpha(110); chart.setHoleRadius(58f); chart.setTransparentCircleRadius(61f); chart.setDrawCenterText(true); chart.setRotationAngle(0); // enable rotation of the chart by touch chart.setRotationEnabled(true); chart.setHighlightPerTapEnabled(true); // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener chart.setOnChartValueSelectedListener(this); chart.animateY(1400, Easing.EaseInOutQuad); // chart.spin(2000, 0, 360); Legend l = chart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setDrawInside(false); l.setXEntrySpace(7f); l.setYEntrySpace(0f); l.setYOffset(0f); // entry label styling chart.setEntryLabelColor(Color.WHITE); chart.setEntryLabelTypeface(tfRegular); chart.setEntryLabelTextSize(12f); setData(4,10); } private void setData(int count, float range) { ArrayList<PieEntry> entries = new ArrayList<>(); // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. for (int i = 0; i < count ; i++) { entries.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i], null)); } PieDataSet dataSet = new PieDataSet(entries, "Election Results"); dataSet.setDrawIcons(false); dataSet.setSliceSpace(3f); dataSet.setIconsOffset(new MPPointF(0, 40)); dataSet.setSelectionShift(5f); ArrayList<Integer> colors = new ArrayList<>(); colors.add(Color.parseColor(colors1[0])); colors.add(Color.parseColor(colors1[1])); colors.add(Color.parseColor(colors1[2])); colors.add(Color.parseColor(colors1[3])); dataSet.setColors(colors); //dataSet.setSelectionShift(0f); PieData data = new PieData(dataSet); //data.setValueFormatter(new PercentFormatter(chart)); data.setValueTextSize(11f); data.setValueTextColor(Color.WHITE); data.setValueTypeface(tfLight); chart.setData(data); // undo all highlights chart.highlightValues(null); chart.invalidate(); }
数据源的设置:
ArrayList<PieEntry> entries = new ArrayList<>(); // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. for (int i = 0; i < count ; i++) { entries.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i], null)); } PieDataSet dataSet = new PieDataSet(entries, "Election Results");