Reputation: 109
I am using MPAndroidChart library for pie chart in my android application, using mysql server data. I used volley library to retrieve the data from server.
I can able to retrieve data json successfully:
com.example.bharat.plantnow D/ChartActivity﹕ Location Response: [{"treecondition":"Balanced","Count":"2"},{"treecondition":"Dieseased","Count":"1"},{"treecondition":"Healthy","Count":"4"},{"treecondition":"Imbalance","Count":"2"},{"treecondition":"Transplanted","Count":"1"}]
But In pie chart , I am getting only last value i.e "Transplated" with Count "1", remaining not able to display. Pl help me where I am getting wrong in CODE.
private boolean addData(){
String tag_string_req = "req_location";
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_PIECHART, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Location Response: " + response.toString());
hideDialog();
try {
JSONArray jTreeList = new JSONArray(response);
// boolean error = jObj.getBoolean("error");
// Check for error node in json
for (int i = 0; i < jTreeList.length(); i++) {
JSONObject location = jTreeList.getJSONObject(i);
String treecondition = location.getString("treecondition");
Integer count = location.getInt("Count");
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int j = 0; j < count; j++)
yVals1.add(new Entry((count), j));
ArrayList<String> xVals = new ArrayList<String>();
for(int j = 0; j < treecondition.length();j++)
xVals.add(treecondition);
//create pie data set
PieDataSet dataSet = new PieDataSet(yVals1,"First pie chart");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
//add many colours
ArrayList<Integer> colors = new ArrayList<Integer>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
//instantiate pie data object now
PieData data = new PieData(xVals,dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
//undo all highlight
mChart.highlightValue(null);
//update pie chart
mChart.invalidate();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
return false;
}
Upvotes: 2
Views: 2149
Reputation: 51411
I am 99% sure your code that I am showing below does not do what you expect it to do:
String treecondition = location.getString("treecondition");
Integer count = location.getInt("Count");
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int j = 0; j < count; j++)
yVals1.add(new Entry((count), j));
ArrayList<String> xVals = new ArrayList<String>();
for(int j = 0; j < treecondition.length();j++)
xVals.add(treecondition);
I'll evaluate what this code will do in terms of the PieChart
:
Your first for-loop
has as many iterations as specified in the count
variable. Then you add the count
variable to an Entry
. This will result in each slice having the same value and being equally sized, completely independent of your data.
Your second for-loop
has as many iterations as the String-variable
treecondition
has characters. Then you always add that same string to your x-values
array in each iteration. If you for example have a String
"Horse", this will result in an x-values
array of length 5 containing the String
"Horse" five times.
If that is not what you expect your code to do, then I suggest you check your for-loops
again.
EDIT:
I get it now. The issue is that your "count" variable is 1. This means only one Entry
will be added to the chart - and therefore you can see only one Entry
.
Adding more x-values
alone will not create any more slices on the PieChart
since there is no more data for the slices.
Upvotes: 1