Tengo un problema con mi MpAndroidChart. El punto es que cada vez que hago un click o un zom el cuadro de chart, éste se encoje. Lo malo también es que mientras mas barras haya más se encoje.
Pr favor podrían ayudarme a solucionar este problema? Es urgente.
Les adjunto mi código en android. Muchas gracias.
public class DistributionActivity extends AppCompatActivity {
String access_token;
private HorizontalBarChart chart;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
ArrayList<String> xVals = new ArrayList<String>();
BarData data = null;
float barWidth;
float barSpace;
float groupSpace;
double DbPercent;
float percent;
protected Typeface mTfLight;
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distribution);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
SharedPreferences prefs = getSharedPreferences("MyPreferencesG11", Context.MODE_PRIVATE);
ActionBar toolbar = getSupportActionBar();
toolbar.setDisplayHomeAsUpEnabled(true);
toolbar.setDisplayShowHomeEnabled(true);
access_token = prefs.getString("access_token", "");
//tv_prueba=(TextView) findViewById(R.id.tv_prueba);
/**********components************/
LoginService loginService = ServiceGenerator.createService(LoginService.class);
Call<UserAccountBean> call = loginService.getUserLogged(access_token);
call.enqueue(new Callback<UserAccountBean>() {
@Override
public void onResponse(Call<UserAccountBean> call, Response<UserAccountBean> response) {
UserAccountBean userAccountBean = response.body();
//tv_prueba.setText(userAccountBean.getId()+" "+userAccountBean.getFirstName()+" "+userAccountBean.getPatternName() );
}
@Override
public void onFailure(Call<UserAccountBean> call, Throwable t) {
}
});
DistributionSummaryDataService dashBoardService = ServiceGenerator.createService(DistributionSummaryDataService.class);
Call<ResponseBean<DistributionSummaryDataBean>> dbCall;
dbCall = dashBoardService.getAllPercent(access_token);
dbCall.enqueue(new Callback<ResponseBean<DistributionSummaryDataBean>>() {
@Override
public void onResponse(Call<ResponseBean<DistributionSummaryDataBean>> call, Response<ResponseBean<DistributionSummaryDataBean>> response) {
DistributionSummaryDataBean dsd = (DistributionSummaryDataBean) response.body().getData();
System.out.println("====================================================================");
chart= (HorizontalBarChart) findViewById(R.id.distribution_chart);
// chart.setOnChartValueSelectedListener((OnChartValueSelectedListener) contex);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(false);
chart.getDescription().setEnabled(false);
chart.setScaleYEnabled(true);// zomm y
chart.setScaleXEnabled(false);// zomm x
chart.setDrawBarShadow(false); //sombra en las barras
chart.setPinchZoom(false);
chart.setDrawGridBackground(false); //
chart.animateY(5000);
chart.setClickable(false);
xVals.add(dsd.getaLastMonthBean().getStrMonth());
xVals.add(dsd.getaLastWeekBean().getStrMonth());
for (int i = 0 ; i<dsd.getLstLastSevenDayBean().size();i++) {
xVals.add(dsd.getLstLastSevenDayBean().get(i).getStrDate());
}
xVals.add(dsd.getaDistributionSummaryDataBean().getStrDateData());
/*barras*/
DbPercent = dsd.getaLastMonthBean().getDbPercent();
percent = (float) DbPercent;
yVals1.add(new BarEntry(0, (float)Math.round(percent*100)/100));
DbPercent = dsd.getaLastWeekBean().getDbPercent();
percent = (float) DbPercent;
yVals2.add(new BarEntry(1, (float)Math.round(percent*100)/100));
int a=2;
for (int i = 0 ; i<dsd.getLstLastSevenDayBean().size();i++) {
DbPercent = dsd.getLstLastSevenDayBean().get(i).getDbPercent();
percent = (float) DbPercent;
yVals3.add(new BarEntry(a, (float) Math.round(percent * 100)));
a++;
}
DbPercent = dsd.getaDistributionSummaryDataBean().getDbPercent();
percent = (float) DbPercent;
yVals4.add(new BarEntry(9, (float) Math.round(percent * 100) / 100));
System.out.println("last month percent -> "+(float)Math.round(percent*100)/100+ " - "+DbPercent);
barWidth = 0.3f;
barSpace = 0f;
groupSpace = 0.2f;
BarDataSet set1, set2,set3,set4;
set1 = new BarDataSet(yVals1, "Mes pasado" );
set1.setColor(Color.BLUE);
set2 = new BarDataSet(yVals2,"Semana pasada");
set2.setColor(Color.CYAN);
set3 = new BarDataSet(yVals3,"7 días antes");
set3.setColor(Color.YELLOW);
set4 = new BarDataSet(yVals4,"Mes actual");
set4.setColor(Color.RED);
data = new BarData(set1, set2,set3,set4 );
data.setValueFormatter(new PercentFormatter(new DecimalFormat("###,###,##0.0")));
chart.setData(data);
chart.setFitBars(true);
//chart.getBarData().setBarWidth(barWidth);
// chart.getXAxis().setAxisMinimum(0);
// chart.getXAxis().setAxisMaximum(1);
System.out.println("xval: " + set1 + " yval: " + set2);
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setYOffset(20f);
l.setXOffset(0f);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.SQUARE);
l.setFormSize(9f);
l.setTextSize(11f);
l.setXEntrySpace(6f);
//X-axis
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.TOP);
xAxis.setCenterAxisLabels(false);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setLabelCount(20);
xAxis.setLabelRotationAngle(90f);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
//Y-axis
LimitLine limitline = new LimitLine(95);
limitline.setLabel("95 %");
limitline.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
limitline.setLineWidth(4f);
limitline.setTextSize(15f);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setAxisMinimum(0f);
leftAxis.setAxisMaximum(100f);
leftAxis.setDrawGridLines(false);
leftAxis.addLimitLine(limitline);
leftAxis.setValueFormatter(new PercentFormatter( new DecimalFormat("###,###,##0.0")));
YAxis yr = chart.getAxisRight();
yr.setTypeface(mTfLight);
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
yr.setAxisMaximum(100f);
yr.setValueFormatter(new PercentFormatter( new DecimalFormat("###,###,##0.0")));
/***********************CHART END*************************************/
/***********************CHART*************************************/
System.out.println("====================================================================");
chart.invalidate();
}
@Override
public void onFailure(Call<ResponseBean<DistributionSummaryDataBean>> call, Throwable t) {
System.err.println("ERRORORORORRO: " + t.toString());
System.err.println("ERRORORORORRO: " + t.getMessage());
}
});
}
}