0

I have a LineChart inside of a Fragment, and I included a spinner to filter the data by week/month/year. The OnCreateView of the Fragment initializes it to display the Weekly data, but when I select the spinner to set it by Month or Year, the data is added on top of the previously set Weekly data instead of being cleared.

This is my OnCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_corporate_chart, container, false);
    corporateChart = view.findViewById(R.id.corporateChart);
    cChart.getAxisLeft().setAxisMaximum(150f);
    cChart.getAxisLeft().setAxisMinimum(0f);
    spinnerTime = view.findViewById(R.id.spinnerTime);
    spinnerMonth = view.findViewById(R.id.spinnerMonth);
    spinnerYear= view.findViewById(R.id.spinnerYear);

    months.add("JANUARY");
    months.add("FEBRUARY");
    months.add("MARCH");
    months.add("APRIL");
    months.add("MAY");
    months.add("JUNE");
    months.add("JULY");
    months.add("AUGUST");
    months.add("SEPTEMBER");
    months.add("OCTOBER");
    months.add("NOVEMBER");
    months.add("DECEMBER");

    time.add("WEEK");
    time.add("MONTH");
    time.add("YEAR");

    Calendar start = Calendar.getInstance();
    start.set(Calendar.YEAR, 2000);
    start.setTime(start.getTime());

    Calendar end = Calendar.getInstance();
    int yearnow = Calendar.getInstance().get(Calendar.YEAR);
    end.set(Calendar.YEAR, yearnow);
    end.setTime(end.getTime());

    while(start.before(end) || start.equals(end)){
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        Log.i("INFO", "START YEAR: " + start);
        Log.i("INFO", "START YEAR: " + end);
        years.add(year.format(start.getTime()));
        start.add(Calendar.YEAR, 1);
    }
    Collections.reverse(years);

    monthAdapter = new ArrayAdapter<>(getActivity(), R.layout.custom_spinner_left, months);
    yearAdapter = new ArrayAdapter<>(getActivity(), R.layout.custom_spinner_left, years);
    timeAdapter = new ArrayAdapter<>(getActivity(), R.layout.custom_spinner_left, time);

    spinnerMonth.setAdapter(monthAdapter);
    spinnerMonth.setPrompt("Select Month");

    spinnerYear.setAdapter(yearAdapter);
    spinnerYear.setPrompt("Select Year");

    spinnerTime.setAdapter(timeAdapter);
    spinnerTime.setPrompt("Select Time Range");

    setData("WEEK");


    spinnerTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            resetChart();
            selectedTime = parent.getItemAtPosition(position).toString();
            setData(selectedTime);
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    return view;

}

and here is my setData() method

private void setData(String selectedTime)
{
    if(selectedTime.equals("WEEK"))
    {
        chartModel = createCChartFunction.setupWeekData(realm, corporateChart);
    }
    else if(selectedTime.equals("MONTH"))
    {
        chartModel = createCChartFunction.setupMonthData(realm, corporateChart);
    }
    else if(selectedTime.equals("YEAR"))
    {
        chartModel = createCChartFunction.setupYearData(realm, corporateChart);
    }

    ArrayList<Entry> e1 = new ArrayList<Entry>();
    final ArrayList<String> e2 = new ArrayList<>();

    for (int i = 0; i < chartModel.size(); i++) // Y Axis
    {
        e1.add(new Entry(i, chartModel.get(i).getyValueCount()));
    }

    for (int j = 0; j < chartModel.size() ; j++) // X Axis
    {
        e2.add(chartModel.get(j).getxValueDate());
    }

    LineDataSet AS = new LineDataSet(e1, "Corporate Accounts");
    AS.setLineWidth(2.5f);
    AS.setCircleRadius(4.5f);
    AS.setHighLightColor(Color.rgb(244, 117, 117));
    AS.setDrawValues(false);

    approvedSet = new ArrayList<ILineDataSet>();
    approvedSet.add(AS);
    approvedCorporate = new LineData(approvedSet);

    XAxis xAxis = corporateChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularityEnabled(true);

    YAxis yAxisRight = corporateChart.getAxisRight();
    yAxisRight.setEnabled(false);

    YAxis yAxisLeft = corporateChart.getAxisLeft();
    yAxisLeft.setDrawGridLines(true);

    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return e2.get((int) value);
        }
    });

    IMarker marker = new chartMarker(getActivity(), R.layout.custom_chart_marker);
    corporateChart.setMarker(marker);

    cChart.setVisibleYRangeMaximum(10, YAxis.AxisDependency.LEFT);
    AS.notifyDataSetChanged();
    cChart.notifyDataSetChanged();
    cChart.setData(approvedCorporate);
    cChart.invalidate();
}

private void resetChart() {
    approvedSet.clear();
    approvedCorporate.notifyDataChanged();
    cChart.clearValues();
    cChart.notifyDataSetChanged();
    cChart.clear();
    cChart.invalidate();
}

0 Answers0