【问题标题】:MPAndroid Chart - Chart not updatingMPAndroid Chart - 图表未更新
【发布时间】:2016-08-30 00:09:31
【问题描述】:

我使用水平 MPAndroid 图表来显示收入/支出,并且该图表最适用。我可以更改显示的信息,尽管我只能在 OnViewCreated 中更改它。如果我尝试从显示片段的活动中执行此操作,则什么都不会发生,我完全不知道为什么。虽然我不确定我是否以正确的方式设置数据。

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 上提问的经验。

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

【问题讨论】:

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


    【解决方案1】:
    entries1.add(new BarEntry(10000, 5));
    

    这一行x值为1,Y值为10000

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

    【讨论】:

      【解决方案2】:

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

      例如,在 Spinner 中选择值的这段代码中,onItemSelected(..) 被调用,并且在其中我们调用了刷新数据的填充函数。这是部分代码,但您可以找到使用 Adapter 和 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");
      
                  }
              };
      

      【讨论】:

        【解决方案3】:

        请试试这个:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-07
          • 2023-03-14
          • 1970-01-01
          • 2019-01-22
          • 2016-06-23
          • 2018-05-21
          • 1970-01-01
          相关资源
          最近更新 更多