Add values to an Android Bar chart manually

0

I am working on a graph in Android, the graph is already shown if I add the values from the code, the problem is that I have an EditText and a button, in that EditText I want to place a value and when I press the button this Value is added to the Table and displayed, but when I add a value using this button the same value is added several times.

This is my MainActivity class the variable that I pass through my button is the variable named data :

public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {
    protected BarChart mChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        final EditText valor = (EditText) findViewById(R.id.editText);
        mChart = (BarChart) findViewById(R.id.chart1);
        mChart.setOnChartValueSelectedListener(this);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.setMaxVisibleValueCount(60);
        mChart.setPinchZoom(true);
        mChart.setDrawGridBackground(false);

        IAxisValueFormatter xAxisFormatter = new Formater(mChart);
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTypeface(Typeface.SERIF);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String dato = valor.getText().toString();
                if (dato.isEmpty()){
                    Toast.makeText(getApplicationContext(),"Debes añadir un valor", Toast.LENGTH_LONG).show();
                }else{
                    setData(  7, 50, Float.parseFloat(String.valueOf(dato)));
                }
            }
        });

        setData(7, 50, 0);
    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {

    }

    @Override
    public void onNothingSelected() {

    }

    private void setData(int count, float range, float dato) {

        Toast.makeText(this,"El valor en el setData es: "+dato, Toast.LENGTH_LONG).show();
        float start = 0f;
        mChart.getXAxis().setAxisMinValue(start);
        mChart.getXAxis().setAxisMaxValue(start + count+1 );

        ArrayList<BarEntry> valores = new ArrayList<BarEntry>();

        for(int i=0; i<count; i++){
            float val = (float)(dato);
            valores.add(new BarEntry(i,val));
        }
        BarDataSet set1;

        if(mChart.getData() !=null && mChart.getData().getDataSetCount()>0){
            set1= (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(valores);
            mChart.getData().notifyDataChanged();

        }else {

            set1 = new BarDataSet(valores,"Dias de la semana");
            set1.setColors(ColorTemplate.MATERIAL_COLORS);
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setValueTypeface(Typeface.SERIF);
            data.setBarWidth(0.9f);
            mChart.setData(data);

        }
    }
}

and this is my setData () method, in this method I add the value I receive from the EditText, the problem is that the value is added repeatedly;

private void setData(int count, float range, float dato) {

        Toast.makeText(this,"El valor en el setData es: "+dato, Toast.LENGTH_LONG).show();
        float start = 0f;
        mChart.getXAxis().setAxisMinValue(start);
        mChart.getXAxis().setAxisMaxValue(start + count+1 );

        ArrayList<BarEntry> valores = new ArrayList<BarEntry>();

        for(int i=0; i<count; i++){
            float val = (float)(dato);
            valores.add(new BarEntry(i,val));
        }
        BarDataSet set1;

        if(mChart.getData() !=null && mChart.getData().getDataSetCount()>0){
            set1= (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(valores);
            mChart.getData().notifyDataChanged();

        }else {

            set1 = new BarDataSet(valores,"Dias de la semana");
            set1.setColors(ColorTemplate.MATERIAL_COLORS);
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setValueTypeface(Typeface.SERIF);
            data.setBarWidth(0.9f);
            mChart.setData(data);

        }
    }

;

    
asked by Enrique Espinosa 27.08.2018 в 22:59
source

0 answers