使用JSON数据绘制线图,Android

我是Android开发的初学者,我正在上课。 我的意思是在应用程序中显示JSON数据作为文本和图形。 我正在使用Retrofit 2在一个活动中将其显示为文本,但我遇到图形问题,我不知道该怎么做(我还在学习,到目前为止我所做的一切都是在帮助来自教程)。

数据看起来像这样(这是一个例子):

{ "id": 1, "measurements": [{ "time": "18:25:43", "value": 25.4 }, { "time": "18:35:43", "value": 27.3 }, { "time": "18:45:21", "value": 26.3 }, { "time": "18:55:43", "value": 25.2 }, { "time": "19:05:43", "value": 25.2 }, { "time": "19:15:43", "value": 25.2 }, { "time": "19:25:43", "value": 24.9 }] } 

我发现了这个 ,但我不知道下一步该做什么。 我应该使用数据创建两个数组列表(如果这是答案,如何做到这一点?),或者有更好的解决方案,直接需要时间和值,并将时间图绘制为X轴,值为Y轴。

这是你的解决方案。

首先,您必须添加依赖项

 dependencies{ compile 'com.github.PhilJay:MPAndroidChart:v2.2.3' ....} 

现在在你的xml中添加以下代码

  

现在在您的Java代码中

 ArrayList x; ArrayList y; private LineChart mChart; public String TAG = "YOUR CLASS NAME"; 

在onCreate方法中

  x = new ArrayList(); y = new ArrayList(); 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); 

按下按钮调用此方法。

 private void drawChart() { String tag_string_req = "req_chart"; StringRequest strReq = new StringRequest(Request.Method.POST, "YOUR URL", new Response.Listener() { @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代码如下所示

 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  void addToRequestQueue(Request req, String tag) { req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getRequestQueue().add(req); } } 

希望这会帮助你。 如果遇到任何问题请告诉我。