Failed to add data into PieEntry. All the dependencies and repositories are added correctly. It can run, but the output is not as same as what I code in. I have no idea about how to solve it.
Main Activity:
private void ShowPieChart() {
ArrayList<PieEntry> pieEntries = new ArrayList<>();
String label = "type";
//initializing data
Map<String, Integer> typeAmountMap = new HashMap<>();
typeAmountMap.put("Toys",200);
typeAmountMap.put("Snacks",230);
typeAmountMap.put("Clothes",100);
typeAmountMap.put("Stationary",500);
typeAmountMap.put("Phone",50);
//initializing colors for the entries
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.parseColor("#304567"));
colors.add(Color.parseColor("#309967"));
colors.add(Color.parseColor("#476567"));
colors.add(Color.parseColor("#890567"));
colors.add(Color.parseColor("#a35567"));
colors.add(Color.parseColor("#ff5f67"));
colors.add(Color.parseColor("#3ca567"));
//input data and fit data into pie chart entry
for(String type: typeAmountMap.keySet()){
pieEntries.add(new PieEntry(typeAmountMap.get(type).floatValue(), type));
}
PieDataSet pieDataSet = new PieDataSet(pieEntries,label);
pieDataSet.setValueTextSize(12f);
pieDataSet.setColors(colors);
PieData pieData = new PieData(pieDataSet);
pieData.setDrawValues(true);
pieChart.setData(pieData);
pieChart.invalidate();
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setRotationEnabled(true);
pieChart.setDragDecelerationFrictionCoef(0.9f);
pieChart.setRotationAngle(0);
pieChart.setHighlightPerTapEnabled(true);
pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
pieChart.setHoleColor(Color.parseColor("#000000"));
}
XML:
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_above="@+id/bottom_navi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="160dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="287dp"
android:layout_marginEnd="144dp"
android:layout_marginRight="144dp"
android:layout_marginBottom="391dp" />
