如何在MPAndroidChart中反转值?

时间:2017-04-28 06:58:10

标签: android mpandroidchart

现在我正在使用您的library来显示从Horizo​​ntal BarChart中的两个API调用获得的一些数据。我将值保存在BarEntries的两个ArrayLists中,然后绘制图表。 我的问题是我想要反转条形的顺序,所以它从最高到最低下降。 在将条目添加到BarEntries的ArrayList之后是否可以更改顺序?所以我做了相反的操作,但是有些问题是值不正确。 请查看我的代码,

     class Top10UsersChartDetails extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            String response = null;
            try {
                response = apiDetails.Top10UsersChartFunc(params[0], params[1]);
            } catch (Exception e) {

            }
            return response;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                JSONObject resObj = new JSONObject(s);
                String status = resObj.getString("status");
                String Month = resObj.getString("Month");
                Log.e("status", status);
                Log.e("Month", Month);
                leaderBoardTV.setText("BEER LEADER BOARD - "+Month);
                if (status.equalsIgnoreCase("Success")) {
                    JSONArray userdataDetailsArray = resObj.getJSONArray("userdata");
                    Log.e("userdataDetailsArray::", userdataDetailsArray + "");
                    for (int j = 0; j < userdataDetailsArray.length(); j++) {
                        JSONObject CampaignDetailsArrayObject = userdataDetailsArray.getJSONObject(j);
                        String userId = CampaignDetailsArrayObject.getString("userId");
                        String UserName = CampaignDetailsArrayObject.getString("UserName");
                        String CountRunning = CampaignDetailsArrayObject.getString("Count");
                        String position=CampaignDetailsArrayObject.getString("position");
                        Log.e("CountRunning",CountRunning+"");
                        Log.e("CountRunning1",Float.parseFloat(CountRunning)+"");
                        float floatCountUser = Float.parseFloat(CountRunning);
                        int positionsUser= Integer.parseInt(position);
                        if(userID.equals(userId)&&j>=9){
                            yourPOsitionTV.setVisibility(View.VISIBLE);
                            String yourDetail="YOU (at "+positionsUser+") with "+CountRunning+" beer";
                            Log.e("yourDetail",yourDetail+"");
                            yourPOsitionTV.setText(yourDetail);
                        }else {
//                            BARENTRY1.add(new BarEntry(fp1, j));
//                            BarEntryLabels1.add("User#" + position);
                            BARENTRY1.add(new BarEntry(floatCountUser, positionsUser));
                            BarEntryLabels1.add("User#" + positionsUser);
                        }
                        Collections.reverse(BARENTRY1);
                    }
                    horizontalBarChart.notifyDataSetChanged();
                    horizontalBarChart.invalidate();
                    Bardataset = new BarDataSet(BARENTRY1, "Projects");
                    BARDATA = new BarData(BarEntryLabels1, Bardataset);
                    Bardataset.setColors(new int[]{Color.parseColor("#701112")});
                    horizontalBarChart.setData(BARDATA);
                    horizontalBarChart.setDrawBarShadow(false);
                    horizontalBarChart.setDrawValueAboveBar(true);
                    horizontalBarChart.setPinchZoom(false);
                    horizontalBarChart.setDrawGridBackground(false);
                    horizontalBarChart.setDescription("");
                    Bardataset.setBarSpacePercent(10f);
                    Legend legend = horizontalBarChart.getLegend();
                    legend.setEnabled(false);
                    horizontalBarChart.setTouchEnabled(false);
                    XAxis xAxis1 = horizontalBarChart.getXAxis();
                    xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
                    xAxis1.setTextSize(8);
                    xAxis1.setSpaceBetweenLabels(8);
                    xAxis1.setTextColor(Color.parseColor("#701112"));
                    xAxis1.setTypeface(tf);
                    YAxis leftAxis = horizontalBarChart.getAxisLeft();
                    leftAxis.setEnabled(false);
                    Collections.reverse(BARENTRY1);
                    Collections.reverse(BarEntryLabels1);
                    Log.e("BARENTRY1",BARENTRY1+"");
                    Log.e("BarEntryLabels1",BarEntryLabels1+"");
                } else {
                    String message=resObj.getString("message");
                    apiDetails.logoutCheck(message, getActivity(), mEditPrefs, loginPref);
                }
            } catch (Exception e) {
            }

        }
    }

 Please look on to this,Where am doing mistake here? 

1 个答案:

答案 0 :(得分:0)

检查此循环:

 for (int j = 0; j < userdataDetailsArray.length(); j++) {
   ....
   ....
}

你在循环中使用Collections.reverse(BARENTRY1);。 完成在循环外添加条目后,请反转列表。

此外,应在创建BarData对象之前完成反转。 在这些行之前反转列表

//reverse here
Collections.reverse(BARENTRY1);
Bardataset = new BarDataSet(BARENTRY1, "Projects");
BARDATA = new BarData(BarEntryLabels1, Bardataset);

虽然更好的解决方案是从另一端解析您的回复...... 你可以把你的循环改为:

for (int j = (userdataDetailsArray.length()-1); j >=0; j++) {
   ....
   ....
}

这将消除任何扭转列表的需要。