Reputation: 31
Im using the Horizontal MPAndroid chart to display income/Expense and the chart works for the most. I can change the information displayed although I can only change it if I do it in OnViewCreated. Nothing at all happens if I try doing it from the activity in which the fragment is displayed and I have absolutely no idea why. Although I am not 100% sure if I am setting the data the right way.
public class BudgetFragment extends Fragment{
private HorizontalBarChart mainChart;
private BarData data;
private BarDataSet dataset1;
private BarDataSet dataset2;
private int expenseSum = 0;
private int incomeSum = 0;
public MainActivityBudgetFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.budget_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mainChart = (HorizontalBarChart) view.findViewById(R.id.mainBudgetChart);
ArrayList<BarEntry> entries1 = new ArrayList<>();
ArrayList<BarEntry> entries2 = new ArrayList<>();
entries1.add(new BarEntry(10000, 5));
entries2.add(new BarEntry(10000, 5));
dataset1 = new BarDataSet(entries1, "income");
dataset2 = new BarDataSet(entries2, "expense");
//X-axis labels
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("income"); xVals.add("expense");
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(dataset1);
dataSets.add(dataset2);
//Add to chart
data = new BarData(xVals, dataSets);
mainChart.setData(data);
//Description and animation
mainChart.setDescription(""); // set the description
mainChart.setScaleYEnabled(false);
mainChart.setTouchEnabled(false);
mainChart.animateY(2000);
setDataExpense(200);//(This works fine)
setDataIncome(200); //(This works fine)
}
public void updateDataExpense(){
Log.e("updateTag", "Updated expense");
dataset2.removeEntry(1);
data.addEntry(new BarEntry(expenseSum, 1), 1);
dataset2.setColor(getResources().getColor(R.color.orange));
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
public void updateDataIncome(){
Log.e("updateTag", "Updated Income");
dataset1.removeEntry(0);
data.addEntry(new BarEntry(newIncome, 0), 0);
dataset1.setColor(getResources().getColor(R.color.green));
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
//(These do not work when called outside OnViewCreated)
private void setDataExpense(int sum){
expenseSum = (expenseSum + sum);
Log.d("ResumeTag", "expense set at " + expenseSum);
updateDataExpense();
}
private void setDataIncome(int sum){
incomeSum = (incomeSum + sum);
Log.d("ResumeTag", "income set at " + incomeSum);
updateDataIncome();
}
}
Let me know if I forgot anything important. I do not have much experience in asking questions on Stackoverflow.
Thank you for your help! //Chris
Upvotes: 2
Views: 2441
Reputation: 487
entries1.add(new BarEntry(10000, 5));
This line x value is 1 and Y value is 10000
entries1.add(new BarEntry(5,10000));
Upvotes: 0
Reputation: 786
Your question does say what action is performed for which you expect the data to be updated. On whatever action you want the data to be refreshed, you should have one of the listener and then call your functions that populates the data that you want to be refreshed.
For example in this code on selection of a value in Spinner, onItemSelected(..) gets invoked & within it we are calling the populate function that refreshes the data. This is partial code but you can find a complete example of using Adapter & OnItemSelectedListener. Hope this helps you.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, monthList);
// Setting the array adapter containing country list to the spinner widget
spinnerMonth.setAdapter(adapter);
AdapterView.OnItemSelectedListener monthSelectedListener = new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position, long id) {
Log.d("Logger:MainAct"," onItemSelected:Entry::::");
tvMonth.setText("Chart for "+strMonth[position]);
populateChartByMonth(strMonth[position]);
//Toast.makeText(getApplicationContext(), "Success : " + strMonth[position], Toast.LENGTH_SHORT).show();
Log.d("Logger:MainAct"," onItemSelected:Exit::::");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Log.d("Logger:MainAct"," onNothingSelected:Entry/Exit");
}
};
Upvotes: 0
Reputation: 1266
Please try this :
public void updateDataIncome() {
Log.e("updateTag", "Updated Income");
dataset1.removeEntry(0);
data.addEntry(new BarEntry(newIncome, 0), 0);
dataset1.setColor(getResources().getColor(R.color.green));
data.notifyDataChanged(); // NOTIFIES THE DATA OBJECT
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
Upvotes: 1