我在Fragment内有一个LineChart,并且包括一个微调框,用于按周/月/年过滤数据。 Fragment的OnCreateView会将其初始化为显示每周数据,但是当我选择微调器以按月或年份进行设置时,该数据将添加到先前设置的“每周”数据之上,而不是被清除。
这是我的OnCreateView方法:
@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;
}
这是我的setData()方法
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();
}