Я использую MPAndroidChart
в моем приложении. Как получить список процентных графиков и отобразить их в txt_leads_percentage
?
Код:
public class ShowChartActivity extends AppCompatActivity {
private long mills1 = 0;
private long mills2 = 0;
private AppViewModel viewModel;
private ItemsByCategoryDao itemsByCategoryDao;
private PieChart chart;
private LinearLayout childlayout;
ArrayList<Integer> colorcodes = new ArrayList<>();
ArrayList<Float> munit_percentage_list = new ArrayList<>();
Legend l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_chart);
try {
Bundle bundle = getIntent().getExtras();
mills1 = bundle.getLong("mills1", 0);
mills2 = bundle.getLong("mills2", 0);
} catch (Exception ex) {
ex.printStackTrace();
}
viewModel = ViewModelProviders.of(this).get(AppViewModel.class);
itemsByCategoryDao = AppDatabase.getInstance(this).itemsByCategoryDao();
childlayout = findViewById(R.id.childlayout);
chart = findViewById(R.id.chart);
chart.setUsePercentValues(true);
chart.getDescription().setEnabled(false);
chart.setExtraOffsets(5, 10, 5, 5);
chart.setDragDecelerationFrictionCoef(0.95f);
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.animateY(1400, Easing.EaseInOutQuad);
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.setEntryLabelTextSize(12f);
chart.setDrawRoundedSlices(true);
chart.getLegend().setEnabled(false);
showPieChart();
}
private void showPieChart() {
ArrayList<PieEntry> entries = new ArrayList<>();
viewModel.showCategoriesForItemForPeriod(itemsByCategoryDao);
viewModel.getListCategoryForItemsForPeriod().observe(this, new Observer<PagedList<ItemsByCategory>>() {
@Override
public void onChanged(PagedList<ItemsByCategory> list) {
for (int i = 0; i < list.size() ; i++) {
float v = (float) list.get(i).getSumma();
entries.add(new PieEntry(v, list.get(i).getText()));
}
PieDataSet dataSet = new PieDataSet(entries, "");
dataSet.setDrawIcons(false);
dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);
//Add colors
ArrayList<Integer> colors = new ArrayList<>();
int color = 0;
for (int i = 0; i < list.size(); i++) {
Random rnd = new Random();
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
colors.add(color);
colorcodes.add(color);
}
//Set colors
dataSet.setColors(colors);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter(chart));
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
chart.setData(data);
// undo all highlights
chart.highlightValues(null);
chart.setDrawEntryLabels(false);
chart.invalidate();
//Show custom Legend
for (int i = 0; i < list.size(); i++) {
LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
parms_left_layout.weight = 1F;
LinearLayout left_layout = new LinearLayout(ShowChartActivity.this);
left_layout.setOrientation(LinearLayout.HORIZONTAL);
left_layout.setGravity(Gravity.CENTER);
left_layout.setLayoutParams(parms_left_layout);
LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
20, 20);
parms_legen_layout.setMargins(0, 0, 20, 0);
LinearLayout legend_layout = new LinearLayout(ShowChartActivity.this);
legend_layout.setLayoutParams(parms_legen_layout);
legend_layout.setOrientation(LinearLayout.HORIZONTAL);
legend_layout.setBackgroundColor(colorcodes.get(i));
left_layout.addView(legend_layout);
TextView txt_unit = new TextView(ShowChartActivity.this);
txt_unit.setText(entries.get(i).getLabel());
left_layout.addView(txt_unit);
LinearLayout.LayoutParams parms_middle_layout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
parms_middle_layout.weight = 1F;
LinearLayout middle_layout = new LinearLayout(ShowChartActivity.this);
middle_layout.setOrientation(LinearLayout.HORIZONTAL);
middle_layout.setGravity(Gravity.CENTER);
middle_layout.setLayoutParams(parms_middle_layout);
TextView txt_leads = new TextView(ShowChartActivity.this);
txt_leads.setText(entries.get(i).getValue() + "");
middle_layout.addView(txt_leads);
LinearLayout.LayoutParams parms_right_layout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
parms_right_layout.weight = 1F;
LinearLayout right_layout = new LinearLayout(ShowChartActivity.this);
right_layout.setOrientation(LinearLayout.HORIZONTAL);
right_layout.setGravity(Gravity.CENTER);
right_layout.setLayoutParams(parms_right_layout);
TextView txt_leads_percentage = new TextView(ShowChartActivity.this);
txt_leads_percentage.setText("");
right_layout.addView(txt_leads_percentage);
childlayout.addView(left_layout);
childlayout.addView(middle_layout);
childlayout.addView(right_layout);
}
}
});
FilterClassForItemsForCategoty t = new FilterClassForItemsForCategoty(mills1, mills2, 0);
viewModel.filterTextAllForItemsForCategory.setValue(t);
}
}