MPAndroid图表 - 图表未更新

时间:2016-05-04 13:45:00

标签: java android android-fragments android-studio mpandroidchart

我使用水平MPAndroid图表来显示收入/费用,图表最常用。我可以更改显示的信息,但我只能在 OnViewCreated 中更改它。如果我尝试从显示片段的活动中进行操作并且我完全不知道为什么,那么什么都没发生。虽然我不是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上提问时没什么经验。

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

3 个答案:

答案 0 :(得分:0)

请试试这个:

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
}

答案 1 :(得分:0)

您的问题确实说明了您希望更新数据的操作。在您想要刷新数据的任何操作上,您应该拥有一个侦听器,然后调用填充您要刷新的数据的函数。

例如,在这个代码中选择Spinner中的值时,onItemSelected(..)被调用&amp;在其中我们调用刷新数据的populate函数。这是部分代码,但您可以找到使用Adapter&amp; amp;的完整示例。 OnItemSelectedListener。希望这会对你有所帮助。

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");

            }
        };

答案 2 :(得分:0)

entries1.add(new BarEntry(10000, 5));

此行x值为1,Y值为10000

entries1.add(new BarEntry(5,10000));