crushman
crushman

Reputation: 1112

MPAndroidChart inside Volley onResponse

Good day, I have been trying to resolve this problem for days now. I am trying to get some json data and place it as a pie entry for MPandroidChart library. The library works perfectly if i just drop it inside the onCreate method and provide the inputs manually without volley like so :

pie_entry.add(new PieEntry(5, "test"));

But the minute i place it inside volley's onResponse method or if i try to get the entries and send it to another method and then add it to pieChart, or if i try to add the entries to a global veriable and then add to chart it doesnt work. here is my code :

String tag_String_reg = "reg_GetVoteResults";

    db = new SQLiteHandler(getApplicationContext());
    final HashMap<String, Object> myData;
    myData = db.getUserIDAndOrg();
    int userID = (int) myData.get(db.KEY_USER_ID);
    final String mOrg = (String) myData.get(db.KEY_USER_UID);

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            AppConfig.URL_GET_VOTERESULTS, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray JArray = new JSONArray(response);
                for (int i = 0; i < JArray.length(); i++) {
                    JSONObject answerInfo = JArray.getJSONObject(i);
                    String answer = answerInfo.getString("answer");
                    float vote = answerInfo.getInt("answer_vote");
                    //vote_count = new ArrayList<>();
                    //anslist = new ArrayList<>();
                    //vote_count.add(vote);
                    //anslist.add(answer);
                    pie_entry.add(new PieEntry(vote, answer));
                }
                pie_entry.add(new PieEntry(5, "test"));
                pie_entry.add(new PieEntry(7, "test2"));
                Log.wtf("current name and int ", pie_entry.toString());
                //testing(pie_entry);
                //Log.wtf("final result: ", pe.toString());
                pieChart.setUsePercentValues(true);
                //Log.wtf("inside chart ", pe.toString());
                pieChart.getDescription().setEnabled(false);
                pieChart.setExtraOffsets(5, 10, 5, 5);
                pieChart.setDragDecelerationFrictionCoef(0.95f);
                pieChart.setDrawHoleEnabled(true);
                pieChart.setHoleColor(WHITE);
                pieChart.setTransparentCircleRadius(61f);

                PieDataSet dataSet = new PieDataSet(pie_entry, "Voting Results");
                dataSet.setSliceSpace(3f);
                dataSet.setSelectionShift(5f);
                dataSet.setColors(ColorTemplate.JOYFUL_COLORS);

                PieData data = new PieData((dataSet));
                data.setValueTextSize(10f);
                data.setValueTextColor(Color.YELLOW);

                pieChart.setData(data);
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Didnt work. Catch. "+ response.toString(), Toast.LENGTH_LONG).show();
                Log.d(TAG, "Didnt work. Error : "+e.toString());

            }

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(error.getMessage()==null){
                        Toast.makeText(getApplicationContext(), " ERROR ", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Error. Please check your internet connection. ErrorListener. " + error.getMessage(), Toast.LENGTH_LONG).show();


                    }

                }
            }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("org_id", mOrg);
            params.put("question_id", question_id);
            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(stringRequest, tag_String_reg);`

I am possitive the json response works as i checked it with Log.wtf Any help would be greatly appreciated.

Also, There is no error in the logcat, all it would show is the results of my Log.wtf:

current name and int: [Entry, x: 0.0 y: 1.0, Entry, x: 0.0 y: 1.0, Entry, x: 0.0 y: 5.0, Entry, x: 0.0 y: 7.0]

Upvotes: 0

Views: 813

Answers (2)

Umar Hussain
Umar Hussain

Reputation: 3527

Add these after pieChart.setData(data) :

pieChart.notifyDataSetChanged();
pieChart.invalidate();

You need to notify the view of chart about the changes of underlying data like we do with RecyclerView.Adapter when its data changes.

Upvotes: 0

Jerome
Jerome

Reputation: 1271

Didn't you forget a

pieChart.invalidate();

after

pieChart.setData(data);

to force the chart to refresh ?

Upvotes: 1

Related Questions