MPAndroidChart是一款基于Android的開源圖表庫,MPAndroidChart不僅可以在Android裝置上繪制各種統計圖表,而且可以對圖表進行拖動和縮放操作,應用起來非常靈活。MPAndroidChart同樣擁有常用的圖表類型:線型圖、餅圖、柱狀圖和散點圖。
GitHub位址:
https://github.com/PhilJay/MPAndroidChart
定義XML檔案
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="15dp"
/>
主要Java邏輯代碼如下:
public class MainActivity extends ActionBarActivity {
private PieChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (PieChart) findViewById(R.id.spread_pie_chart);
PieData mPieData = getPieData(, );
showChart(mChart, mPieData);
}
private void showChart(PieChart pieChart, PieData pieData) {
//顯示成内圈顔色是否透明
pieChart.setHoleColorTransparent(true);
//半徑
pieChart.setHoleRadius(f);
// 半透明圈
pieChart.setTransparentCircleRadius(f);
//實心圓
//pieChart.setHoleRadius(0);
//右下角名字
pieChart.setDescription("測試餅狀圖");
// mChart.setDrawYValues(true);
//餅狀圖中間可以添加文字
pieChart.setDrawCenterText(true);
//變成空心的餅圖(有白色邊)
pieChart.setDrawHoleEnabled(true);
// 初始旋轉角度
pieChart.setRotationAngle();
// draws the corresponding description value into the slice
// mChart.setDrawXValues(true);
// 可以手動旋轉
pieChart.setRotationEnabled(true);
//顯示成百分比
pieChart.setUsePercentValues(true);
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);
// add a selection listener
// mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);
// mChart.setOnAnimationListener(this);
pieChart.setCenterText("Quarterly Revenue"); //餅狀圖中間的文字
//設定資料
setData(pieData);
//setData(總共幾部分,傳進來的char,每個部分的值的集合)
setData(stoclist.size(), chart,bililist);
// undo all highlights
// pieChart.highlightValues(null);
// pieChart.invalidate();
//設定比例圖
Legend mLegend = pieChart.getLegend();
//最右邊顯示
mLegend.setPosition(LegendPosition.RIGHT_OF_CHART);
//設定比例圖的形狀,預設是方形
// mLegend.setForm(LegendForm.LINE);
mLegend.setXEntrySpace(f);
mLegend.setYEntrySpace(f);
//隐藏說明(比例圈)
mLegend.setEnabled(false);
pieChart.animateXY(, ); //設定動畫
// mChart.spin(2000, 0, 360);
}
/**
*
* @param count 分成幾部分
* @param range
*/
private void setData(int count, PieChart chart,List<Float> blist) {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Integer> colors = new ArrayList<Integer>();
// IMPORTANT: In a PieChart, no values (Entry) should have the same
// xIndex (even if from different DataSets), since no values can be
// drawn above each other.
if (new BigDecimal(pricecount.getText().toString()).compareTo(new BigDecimal())!= &&chart.equals(chart2)){
yVals1.add(new Entry(f, ));
xVals.add("stock");
colors.add(getResources().getColor(R.color.cC3));
}
else {
for (int i = ; i < count ; i++) {
//确定比例權重
yVals1.add(new Entry(blist.get(i), i));
}
//往餅圖上寫的分類名字(介紹上也有這個名字)
for (int i = ; i < count ; i++)
xVals.add(stoclist.get(i));
// add a lot of colors
for (int c : colorlist)
colors.add(c);
}
PieDataSet dataSet = new PieDataSet(yVals1, "");
//設定個餅狀圖之間的距離
dataSet.setSliceSpace(f);
dataSet.setSelectionShift(f);
// 餅圖顔色
dataSet.setColors(colors);
// 選中态多出的長度
//dataSet.setSelectionShift(0f);
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(f);
data.setValueTextColor(Color.TRANSPARENT);
chart.setData(data);
// undo all highlights
chart.highlightValues(null);
chart.invalidate();
}
}