用float创建一个累积数组

时间:2019-06-22 17:42:13

标签: java android arraylist mpandroidchart

我需要获取一个负浮点值和正浮点值序列的累积值,并将其放入MPAndroidChart条目中:

private ArrayList<Entry> chartValues;

public void getTransactions() {
        (...)
                for (int i = 0; i < info.length(); i++) {
                    JSONObject objInfo = info.getJSONObject(i);

                    (...)

                    int transaction_type = objInfo.getInt("transaction_type");
                    float transaction_amount;
                    if(transaction_type == 0){
                        transaction_amount = -(float) objInfo.getDouble("transaction_amount");
                    }else{
                        transaction_amount = (float) objInfo.getDouble("transaction_amount");

                    }

                    chartValues.add(new Entry(i, transaction_amount));

                }
        (...)
}

private void setData(int count, float range) {

    LineDataSet set1;

    if (chart.getData() != null &&
            chart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
        set1.setValues(chartValues);
        chart.getData().notifyDataChanged();
        chart.notifyDataSetChanged();
    } else {
    (...)
}

chartValues返回{1.00,2.00,-3.00,4.00,5.00 ...},我需要返回{1.00,3.00,0.00,4.00,9.00}。任何想法?谢谢!

1 个答案:

答案 0 :(得分:1)

似乎您需要其他变量来保留项目总数:

public void getTransactions() {
        (...)
                float sum = 0.0f;
                for (int i = 0; i < info.length(); i++) {
                    JSONObject objInfo = info.getJSONObject(i);
...

并使用它:

              sum += transaction_amount;
              chartValues.add(new Entry(i, sum));

                }
        (...)
}