使用JSON数据绘制线图,Android [英] Plotting Line Graph with JSON data, Android
问题描述
我是Android开发的初学者,我正在为课堂做app。我的意思是在应用程序中显示JSON数据作为文本和图形。我正在使用Retrofit 2在一个活动中将其显示为文本,但我遇到图形问题,我不知道该怎么做(我还在学习,到目前为止我所做的一切都是在帮助来自教程)。
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 display it as text in one activity, but I'm having problems with graphs, I don't know how to do it (I'm still learning, everything I've done so far was with help from tutorials).
数据看起来像这样(这是一个例子):
The data looks like this (this is an example):
{
"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轴。
I've found this, but I'm not sure what to do next. Should I make two array lists with data(if that's the answer, how to do that?), or there's better solution that directly takes time and value and plots graph with time as X-axis and value as Y-axis.
推荐答案
这是您的解决方案。
首先,您必须添加依赖项
First you have to add in dependencies
dependencies{
compile 'com.github.PhilJay:MPAndroidChart:v2.2.3'
....}
现在在你的xml中添加以下代码
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" />
现在在你的Java代码中
And now in your Java code
ArrayList<Entry> x;
ArrayList<String> y;
private LineChart mChart;
public String TAG = "YOUR CLASS NAME";
内部onCreate方法
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);
点击按钮调用此方法。
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代码
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.
这篇关于使用JSON数据绘制线图,Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!