gpt4 book ai didi

java - MPAndroid 图表 - 图表未更新

转载 作者:行者123 更新时间:2023-11-29 01:20:33 28 4
gpt4 key购买 nike

我使用 Horizo​​ntal MPAndroid 图表显示收入/支出,该图表最适用。我可以更改显示的信息,尽管我只能在 OnViewCreated 中进行更改。如果我尝试从显示 fragment 的 Activity 中执行此操作,则什么也不会发生,我完全不知道为什么。虽然我不是 100% 确定我是否以正确的方式设置数据。

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();
}

如果我忘记了什么重要的事情,请告诉我。我没有太多在 Stackoverflow 上提问的经验。

感谢您的帮助!//克里斯

最佳答案

请试试这个:

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
}

关于java - MPAndroid 图表 - 图表未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37029679/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com