0

here is my code for showing text lable for displaying data in pie chart now i want to hide text lable value if data is not preasent for perticular text value, how can i achive that below is my code

final ArrayList<PieEntry> yEntrys = new ArrayList<>();
                            //List<String> entries = new ArrayList<>();
                            Toast.makeText(getContext(), "Toase"+response.toString(), Toast.LENGTH_SHORT).show();
                             Log.d("pieData",response.toString());
                            try {

                                JSONArray jsonarray = (JSONArray) response.get("piechartlist");




                                     for (int i = 0; i < jsonarray.length(); i++) {
                                         JSONObject jsonobject = jsonarray.getJSONObject(i);



                                             String messageSent = jsonobject.getString("messageSent").trim();
                                             String failed = jsonobject.getString("failed").trim();
                                             String rejected = jsonobject.getString("rejected").trim();
                                             String expired = jsonobject.getString("expired").trim();
                                             String unDelivered = jsonobject.getString("unDelivered");
                                             String delivered = jsonobject.getString("delivered");
                                             String ndnc = jsonobject.getString("ndnc");
                                             yEntrys.add(new PieEntry(Integer.valueOf(messageSent), "messageSent"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(failed), "failed"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(rejected), "rejected"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(expired), "expired"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(unDelivered), "unDelivered"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(delivered), "delivered"));
                                             yEntrys.add(new PieEntry(Integer.valueOf(ndnc), "ndnc"));





                                     }



                            } catch (JSONException e) {
                                e.printStackTrace();


                            }
ashish
  • 307
  • 4
  • 15

1 Answers1

2

Just don't add the Entry to the PieChart.

if (Integer.valueOf(messageSent) > 0)
{
    yEntrys.add(new PieEntry(Integer.valueOf(messageSent), "messageSent"));
}

btw: your JSON parsing is kinda wired. I would suggest POJOs and GSON for parsing JSON Strings.

woodii
  • 763
  • 7
  • 23
  • its working fine but its also hide bottom legend data ,any solution? – ashish Oct 31 '17 at 10:37
  • Either you add it to the piechart and use a custom ValueFormatter to hide the label there but still show it in the legend. The other way would be to not edit the Labels but tweak the legend with a custom legend like in this answer https://stackoverflow.com/a/40777867/5268730 – woodii Oct 31 '17 at 11:58
  • i also found an example usage of the ValueFormatter which targets your problem: https://github.com/PhilJay/MPAndroidChart/issues/535 – woodii Oct 31 '17 at 12:15
  • is there any solution like that adding custom legend in bottom – ashish Oct 31 '17 at 12:38
  • look into that stackoverflow answer i mentioned above (haven't tried or needed something like this yet - so just try it out) – woodii Oct 31 '17 at 12:45
  • woodii can you provide me example for legend entry – ashish Nov 03 '17 at 07:30
  • you should find necessary stuff here https://stackoverflow.com/questions/40645322/mpandroidchart-piechart-how-to-set-label-text/40777867#40777867 and here https://stackoverflow.com/questions/40645322/mpandroidchart-piechart-how-to-set-label-text/40777867#40777867 get the chart's label and add all your entries – woodii Nov 03 '17 at 12:08
  • @woodii why you don't like author JSON parse way?) What do POJO ang GSON brings in this situation so it's needs to drag a side libraries? – Acuna Apr 03 '18 at 02:55
  • @Acuna in my opinion it is much cleaner to use a lib like Gson or Jackson for handling JSON Objects. If you use it you can transform the JSON from a string to a Java object where you can access all properties directly or by getters and setters. Especially if you have to access the properties multiple times it helps a lot in terms of code readability and re-usability. – woodii Apr 16 '18 at 12:56
  • @woodii well, but default JSON lib can handle different type of data with various type of methods, for example, `object.getString ("key1")` can get a string value, `object.getLong ("key2")` can get long value, `object.get ("key3")` can get a plain object to work with it like an object. Can anything be more clear especially to use side libraries? – Acuna Apr 19 '18 at 09:07
  • @woodii that's what I thought :D It's a main reason when apps which have only one activity weight 60 Mb :/ – Acuna Apr 28 '18 at 14:19
  • Sorry for my late response. 60MB? You will face many other Problems before you get to an App with 60MB. GSON is also kinda lightweight. As I mentioned before GSON gets really handy if you have to deal with bigger JSON Objects (maybe with nested classes). You can also use it in combination with HTTP Libraries like OkHTTP/Retrofit and get a java object as response. If you use the JSON properties once it might be ok to use `getString("key")` but if your JSON is bigger and more complex it will be really painful without GSON/Jackson. – woodii Apr 30 '18 at 13:56
  • Furthermore your code is much cleaner as you can just use your properties after parsing it with e.g. `BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);` then you can always call `obj2.value1` For more examples look into https://github.com/google/gson/blob/master/UserGuide.md – woodii Apr 30 '18 at 13:56