I need to use the data I have obtained from my remote server (MySQL) using loopj to create a chart. I have managed to connect and receive the parameters but I do not know how to transfer them to the graph (MPAndroidChart, linear graph).
This is the graph. I need the temperature data in the Y axis, and the X axis in the months, in the X axis, there is no problem, but I have the problem in the Y axis, because in my case, 240 temperature records are created every day, One take every 3 minutes.
In OnCreate I have this code:
String idObjeto = (String) getIntent().getExtras().getSerializable("IdentidadEnviada");
CaptarParametros(idObjeto);
LineChart lineChart = (LineChart) findViewById(R.id.chart);
ArrayList<Entry> entries = new ArrayList<>();
****aquí debería ir los datos de Y que no se como enfocarlo.
LineDataSet dataset = new LineDataSet(entries, "# of Calls");
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
LineData data = new LineData(labels, dataset);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
dataset.setDrawCubic(true);
dataset.setDrawFilled(true);
lineChart.setData(data);
lineChart.animateY(5000);
The manual way to do it would be like this: (3f, 8f, etc ... are the values of "Y" and the next value is the position on the axis.) This is so since the MPAndrodiChart library requires it. / p>
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(2f, 3));
entries.add(new Entry(18f, 4));
entries.add(new Entry(9f, 5));
entries.add(new Entry(9n+1f..., n+1...));
.....
As there are many records, 240 every day, I can not do it that way, I need to automate this process but adapted to the MPAndrodiChart library.
This is the method that I have created and that contains the connection and the obtaining of the temperature of a sensor in particular that has a specific ID (IdObject). This works well. But now I would need all the temperature data to be used in order on the Y axis of the graph.
private void CaptarParametros(String idObjeto) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put(UtilitiesGlobal.SENSOR_ID, idObjeto);
RequestHandle post = client.post(this, SENSORS_URL, params, new JsonHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// called when response HTTP status is "200 OK"
JSONObject jsonobject = null;
JSONObject dht11JSONbject = null;
JSONArray dht11JSONarray = null;
try {
jsonobject = new JSONObject(String.valueOf(response));
//dht11JSONbject = jsonobject.getJSONObject("result");
List<String> allNames = new ArrayList<String>();
JSONArray cast = jsonobject.getJSONArray("result");
for (int i = 0; i < cast.length(); i++) {
JSONObject parametrosdht11 = cast.getJSONObject(i);
String temperatura = parametrosdht11.getString("temperatura");
String humedad = parametrosdht11.getString("humedad");
allNames.add(temperatura);
allNames.add(humedad);
//Log.i(UtilitiesGlobal.TAG, "onSuccess: loopj " + usuarioiJSONbject);
Log.i(UtilitiesGlobal.TAG, "onSuccess: loopj " + "temperatura: " + temperatura + " humedad: " + humedad);
}
} catch (JSONException e) {
e.printStackTrace();
}
}