I'm using GraphView
,(refer this) to plot the graph.
Below is the activity I'm doing a network call using Volley
, where I'm getting the information to plot the graph. Currently, I'm using a test data to plot the graph. Look for my comments "Here It is working" and "This is where I need to call DoTheGraphs(), but not working." in the Activity.
Whenever I call the DoTheGraphs()
after the parsing the JSON
(Second Comment), GraphView
is not plotting the data and my phone becomes unresponsive without throwing any error. Any help would be greatly appreciated.
ReportDetailsActivity.java
public class ReportDetailsActivity extends Activity {
private ArrayList<Hemoglobin> hemoglobins;
private GraphView graph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_details);
graph = findViewById(R.id.graph);
DoingNetworkStuff(Endpoints.BASE_URL+Endpoints.GET_HEMOGLOBIN_READINGS);
//Here It is working
DoTheGraphs();
}
private void DoingNetworkStuff(String url) {
final String TAG = "ReportsActivity_DoingNetworkStuff";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
hemoglobins = new ArrayList<>();
JSONArray jsonResponse = new JSONObject(response).getJSONArray("info");
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject obj = jsonResponse.getJSONObject(i);
String date = obj.optString("lappointmentdate");
String value = obj.optString("lhemoglobinreading");
hemoglobins.add(new Hemoglobin(date,value));
}
//This is where I need to call DoTheGraphs(), but not working.
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hide the progress dialog
pDialog.hide();
pDialog.setMessage("Check your internet connection Technical Difficulties!");
pDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
pDialog.dismiss();
}
}, 5000);
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("patientid", "VIU1111111111433");
params.put("patientdependantid", "");
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
}
private void DoTheGraphs() {
Date d1 = null;
Date d2 = null;
Date d3 = null;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
d1 = formatter.parse("03/09/1995");
d2 = formatter.parse("04/09/1995");
d3 = formatter.parse("05/09/1995");
} catch (ParseException e) {
e.printStackTrace();
}
Date[] date = {d1,d2,d3};
DataPoint[] dataPoints = new DataPoint[3];
for(int i=0;i<3;i++){
dataPoints[i]=new DataPoint(date[i],1);//new DataPoint(hemoglobins.get(i).getDate(), hemoglobins.get(i).getValue());
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dataPoints);
graph.addSeries(series);
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(ReportDetailsActivity.this));
graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space
graph.getViewport().setMinX(date[0].getTime());
graph.getViewport().setMaxX(date[2].getTime());
graph.getViewport().setXAxisBoundsManual(true);
graph.getGridLabelRenderer().setHumanRounding(false);
}
}
I figured out graph.getGridLabelRenderer().setHumanRounding(false); is causing all the problem. I don't know why?
Same issue: https://github.com/jjoe64/GraphView/issues/440 I did the use the solution there, again same thing.