0

tengo una consulta y es la siguiente:

Estoy consumiendo un servicio rest que consulta datos con retrofit y los muestro en un gráfico usando MPAndroidChart, el problema es que cada consulta que realizo se queda la información anterior almacenada en el gráfico.

Y lo que deseo es que no suceda eso y solo muestre la información que se consulta.

Mi código

private void ConsultarxMeses(String fecha1,String fecha2){

        Call<List<ListaGrafico>> call = dashboardRest.ConsultarxMeses(fecha1,fecha2);
        call.enqueue(new Callback<List<ListaGrafico>>() {
            @Override
            public void onResponse(Call<List<ListaGrafico>> call, Response<List<ListaGrafico>> response) {
                if(!response.isSuccessful()){
                    Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
                    return;
                }
                List<ListaGrafico> dataGrafico = response.body();
                CreateGrafico(dataGrafico);

            }

            @Override
            public void onFailure(Call<List<ListaGrafico>> call, Throwable t) {
                Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

Método de crear Gráfico

private void CreateGrafico(List<ListaGrafico> t){

        ArrayList<String> labelsname= new ArrayList<>();
        ArrayList<BarEntry> barEntries= new ArrayList<>();
        ArrayList<Integer> colores= new ArrayList<>();
        Description description = new Description();
        description.setText("Gráfico Estadístico");

        barChart.setDescription(description);

        BarEntry barDescarga = new BarEntry(0,t.get(0).getDescarga());
        barEntries.add(barDescarga);
        BarEntry barRetiro = new BarEntry(1,t.get(0).getRetiro());
        barEntries.add(barRetiro);
        BarEntry barExportacion = new BarEntry(2,t.get(0).getExportacion());
        barEntries.add(barExportacion);
        BarEntry barEmbarque = new BarEntry(3,t.get(0).getEmbarque());
        barEntries.add(barEmbarque);

        String[] nombresOperacion = {"Descarga","Retiro","Exportación","Embarque"};

        for(int i=0; nombresOperacion.length>i;i++ ){
            //barEntries.add(new BarEntry(i,contador));
            String tipo = nombresOperacion[i];
            labelsname.add(tipo);

        }

        BarDataSet barDataSet = new BarDataSet(barEntries,"Cantidad de Descuentos por Tipo");
        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        BarData barData= new BarData(barDataSet);
        barChart.setData(barData);

        XAxis xaxis= barChart.getXAxis();
        xaxis.setValueFormatter(new IndexAxisValueFormatter(labelsname));

        xaxis.setPosition(XAxis.XAxisPosition.TOP);
        xaxis.setDrawAxisLine(false);
        xaxis.setDrawGridLines(false);
        xaxis.setGranularity(1f);
        xaxis.setLabelCount(labelsname.size());
        xaxis.setLabelRotationAngle(270);
        barChart.animateY(2000);

    }

1 respuesta 1

0

Segun la documentación,

Después de llamar a invalidate(), el gráfico se actualiza y se dibujan los datos proporcionados.

Entonces al final de tu método deberias llamas al método invalidate()

private void CreateGrafico(List<ListaGrafico> t){
    ......
    ......
    // Todo el código que tienes para configurar tu chart
    xaxis.setDrawGridLines(false);
    xaxis.setGranularity(1f);
    xaxis.setLabelCount(labelsname.size());
    xaxis.setLabelRotationAngle(270);
    barChart.animateY(2000);

    // luego en la última linea, llamamos a invalidate
    barChart.invalidate()
}

Referencia -> MPAndroidChart

1
  • Funciono, gracias.
    – Dum
    el 2 jul. 2020 a las 18:50

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.