MPAndroidChart 라이브러리를 활용한 PieChart 함수 예제다.
<com.github.mikephil.charting.charts.PieChart android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/chart" /> |
private void PieChartGraph(ArrayList<String> labelList, ArrayList<Integer> valList) { // PieChart 메소드 PieChart pieChart = (PieChart) findViewById(R.id.chart);
ArrayList<Entry> entries = new ArrayList<>(); for(int i=0; i < valList.size();i++){ entries.add(new Entry((Integer) valList.get(i), i)); }
PieDataSet depenses = new PieDataSet (entries, "월별 가입자수"); depenses.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<String> labels = new ArrayList<String>(); for(int i=0; i < labelList.size(); i++){ labels.add((String) labelList.get(i)); }
PieData data = new PieData(labels,depenses); // 라이브러리 v3.x 사용하면 에러 발생함 depenses.setColors(ColorTemplate.COLORFUL_COLORS); //
pieChart.setData(data); pieChart.animateXY(1000,1000); pieChart.invalidate(); } |