package com.xxmassdeveloper.mpchartexample;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
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 java.util.ArrayList;
/**
* @Info
* @Auth Bello
* @Time 18-3-5 下午2:16
* @Ver
*/
public class MyPieActivity extends FragmentActivity implements OnChartValueSelectedListener {
String[] mParties = new String[] {
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
"Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
"Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
"Party Y", "Party Z"
};
private PieChart mChart;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pie_chart);
mChart = findViewById(R.id.chart1);
mChart.setUsePercentValues(true);
mChart.setBackgroundColor(Color.WHITE);
mChart.getDescription().setEnabled(false);
mChart.setExtraOffsets(5, 10, 5, 5);
mChart.setDragDecelerationFrictionCoef(0.95f);
// mChart.setCenterText(generateCenterSpannableText());
mChart.setDrawHoleEnabled(false);
/* mChart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(158f);
mChart.setTransparentCircleRadius(61f);
*/
mChart.setDrawCenterText(false);
mChart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
// entry label styling
mChart.setEntryLabelColor(Color.RED);
mChart.setEntryLabelTextSize(12f);
// add a selection listener
// mChart.setOnChartValueSelectedListener(this);
setData(14, 100);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
Legend l = mChart.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);
l.setXOffset(2f);
}
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),
mParties[i % mParties.length], null));
}
PieDataSet dataSet = new PieDataSet(entries, "");
dataSet.setDrawIcons(false);
dataSet.setSliceSpace(0f);
dataSet.setSelectionShift(5f);
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<>();
for (int i =0; i<count; i++) {
colors.add(Color.rgb(240 - i*18, 108 - i*18, 0));
}
dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GREEN);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
mChart.invalidate();
}
/**
* Called when a value has been selected inside the chart.
*
* @param e The selected Entry
* @param h The corresponding highlight object that contains information
*/
@Override
public void onValueSelected(Entry e, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getY() + ", index: " + h.getX()
+ ", DataSet index: " + h.getDataSetIndex());
}
/**
* Called when nothing has been selected or an "un-select" has been made.
*/
@Override
public void onNothingSelected() {
Log.i("PieChart", "nothing selected");
}
}