I\'m total beginner in Android development and I\'m doing app for class. My assigment is to display JSON data in application as text and graphs. I\'m using Retrofit 2 to dis
Here is your solution.
First you have to add in dependencies
dependencies{
compile 'com.github.PhilJay:MPAndroidChart:v2.2.3'
....}
Now in your xml add below code
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And now in your Java code
ArrayList<Entry> x;
ArrayList<String> y;
private LineChart mChart;
public String TAG = "YOUR CLASS NAME";
Inside onCreate Method
x = new ArrayList<Entry>();
y = new ArrayList<String>();
mChart = (LineChart) view.findViewById(R.id.chart1);
mChart.setDrawGridBackground(false);
mChart.setDescription("");
mChart.setTouchEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
mChart.setMarkerView(mv);
XAxis xl = mChart.getXAxis();
xl.setAvoidFirstLastClipping(true);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setInverted(true);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setEnabled(false);
Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.LINE);
Call this method on button click.
private void drawChart() {
String tag_string_req = "req_chart";
StringRequest strReq = new StringRequest(Request.Method.POST, "YOUR URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
try {
JSONObject jsonObject = new JSONObject(response);
String id = jsonObject.getString("id");
JSONArray jsonArray = jsonObject.getJSONArray("measurements");
for (int i = 0; i < jsonArray.length(); i++) {
int value = jsonObject.getInt("value");
String date = jsonObject.getString("time");
x.add(new Entry(value, i));
y.add(date);
}
LineDataSet set1 = new LineDataSet(x, "NAV Data Value");
set1.setLineWidth(1.5f);
set1.setCircleRadius(4f);
LineData data = new LineData(y, set1);
mChart.setData(data);
mChart.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
}
});
strReq.setRetryPolicy(new RetryPolicy() {
@Override
public void retry(VolleyError arg0) throws VolleyError {
}
@Override
public int getCurrentTimeout() {
return 0;
}
@Override
public int getCurrentRetryCount() {
return 0;
}
});
strReq.setShouldCache(false);
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
AppController code as here
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
}
Hope this will help you. If any issue faced let me know.