Добавление данных arraylist <float>и arraylist <string>к линейной диаграмме MP Android Chart - PullRequest
0 голосов
/ 01 марта 2020

Я пытаюсь создать температуру данных линейной диаграммы из JsonArray. следующие данные jsonarray, которые у меня есть

"code": 200,
"result": [
    {
        "id_record": 4,
        "id_sensor": 1,
        "value": 5.5999999999999996447286321199499070644378662109375,
        "date": "2019-10-16 23:37:17",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 5,
        "id_sensor": 1,
        "value": 6,
        "date": "2019-10-16 23:37:18",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 6,
        "id_sensor": 1,
        "value": 30,
        "date": "2019-10-16 23:37:19",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 7,
        "id_sensor": 1,
        "value": 13.5,
        "date": "2019-10-16 23:37:21",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 8,
        "id_sensor": 1,
        "value": 5.1699999999999999289457264239899814128875732421875,
        "date": "2019-10-16 23:37:25",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 9,
        "id_sensor": 1,
        "value": 12.5,
        "date": "2019-10-16 23:37:26",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 10,
        "id_sensor": 1,
        "value": 16.5,
        "date": "2019-10-16 23:37:27",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 11,
        "id_sensor": 1,
        "value": 5.5,
        "date": "2019-10-16 23:37:29",
        "sensor": "Suhu",
        "satuan": "°C"
    },
    {
        "id_record": 12,
        "id_sensor": 1,
        "value": 3.3300000000000000710542735760100185871124267578125,
        "date": "2019-10-16 23:37:31",
        "sensor": "Suhu",
        "satuan": "°C"
    },

А затем у меня есть данные модели функций

public class DataSensor {

@SerializedName("id_record")
@Expose
private int kodeRecord;

@SerializedName("id_sensor")
@Expose
private int kodeSensor;

@SerializedName("value")
@Expose
private float nilai;

@SerializedName("date")
@Expose
private String tanggal;

@SerializedName("sensor")
@Expose
private String namaSensor;

@SerializedName("satuan")
@Expose
private String  satuan;

public DataSensor( int kodeRecord, int kodeSensor,int nilai, String tanggal, String namaSensor, String satuan){
    this.kodeRecord = kodeRecord;
    this.kodeSensor = kodeSensor;
    this.nilai = nilai;
    this.tanggal = tanggal;
    this.namaSensor = namaSensor;
    this.satuan = satuan;


}

public int getKodeRecord() {
    return kodeRecord;
}

public void setKodeRecord(int kodeRecord) {
    this.kodeRecord = kodeRecord;
}

public int getKodeSensor() {
    return kodeSensor;
}

public void setKodeSensor(int kodeSensor) {
    this.kodeSensor = kodeSensor;
}

public float getNilai() {
    return nilai;
}

public void setNilai(float nilai) {
    this.nilai = nilai;
}

public String getTanggal() {
    return tanggal;
}

public void setTanggal(String tanggal) {
    this.tanggal = tanggal;
}

public String getNamaSensor() {
    return namaSensor;
}

public void setNamaSensor(String namaSensor) {
    this.namaSensor = namaSensor;
}

public String getSatuan() {
    return satuan;
}

public void setSatuan(String satuan) {
    this.satuan = satuan;
}

, и уже есть код для обработки ответа API следующим образом

public class DataSensorRespon {
@SerializedName("code")
@Expose
private int code;

@SerializedName("result")
@Expose
private ArrayList<DataSensor> dataSensors = null;

public DataSensorRespon(int code, ArrayList<DataSensor> dataSensors){
    this.code = code;
    this.dataSensors = dataSensors;
}

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public ArrayList<DataSensor> getDataSensors() {
    return dataSensors;
}

public void setDataSensors(ArrayList<DataSensor> dataSensors) {
    this.dataSensors = dataSensors;
}

в функции деятельности, я написал следующий код

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_air_pakan);

    setData();
    drawLineChart();
}

private void setData() {
    SharedPreferences shfm = getSharedPreferences("spAndroid", MODE_PRIVATE);
    String token = shfm.getString("token", "");
    Call<DataSensorRespon> call = ApiClient
            .getInstance()
            .getApi()
            .dataHasilSensor(token);
    call.enqueue(new Callback<DataSensorRespon>() {
        @Override
        public void onResponse(retrofit2.Call<DataSensorRespon> call, Response<DataSensorRespon> response) {
            DataSensorRespon dataSensorRespon = response.body();
            if (response.isSuccessful()) {
                if (dataSensorRespon.getCode() == 200) {
                    ArrayList<DataSensor> arrayDataSensor = dataSensorRespon.getDataSensors();
                    if (arrayDataSensor.size() > 0) {
                        ArrayList<DataSensor> filterDataSuhu = new ArrayList<>();
                        temperature_data_entry = new ArrayList<Float>();
                        temperature_dates_entry = new ArrayList<>();
                        for (int i = 0; i < arrayDataSensor.size(); i++) {
                            DataSensor dataSensor = arrayDataSensor.get(i);
                            if (dataSensor.getKodeSensor() == 4) {
                                filterDataSuhu.add(dataSensor);
                                for (int a = 0; a < filterDataSuhu.size(); a++) {
                                    nilai = filterDataSuhu.get(a).getNilai();
                                    temperature_data_entry.add((filterDataSuhu.get(a).getNilai()));
                                    temperature_dates_entry.add(Float.parseFloat(filterDataSuhu.get(a).getTanggal()));
                                  Log.e("status", "e");values.add(new Entry(Float.parseFloat(decimalFormat.format(filterDataSuhu.get(a).getNilai()))));
                                }
                            }
                        }

                    }

                }
            }
        }

        @Override
        public void onFailure(retrofit2.Call<DataSensorRespon> call, Throwable t) {
            Log.e("debug", "onFailure: ERROR > " + t.toString());
            Toast.makeText(mContex, "Something wrong. Please try again later.", Toast.LENGTH_SHORT).show();
        }
    });
}

private void drawLineChart() {
    LineChart lineChart = findViewById(R.id.chartSuhu);
    List<Entry> lineEntries = getDataSet();
    LineDataSet lineDataSet = new LineDataSet(lineEntries, "Air");
    lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    lineDataSet.setHighlightEnabled(true);
    lineDataSet.setLineWidth(2);
    lineDataSet.setColor(Color.RED);
    lineDataSet.setCircleColor(Color.YELLOW);
    lineDataSet.setCircleRadius(6);
    lineDataSet.setCircleHoleRadius(3);
    lineDataSet.setDrawHighlightIndicators(true);
    lineDataSet.setHighLightColor(Color.RED);
    lineDataSet.setValueTextSize(12);
    lineDataSet.setValueTextColor(Color.DKGRAY);

    LineData lineData = new LineData(lineDataSet);
    lineChart.getDescription().setText("Air");
    lineChart.getDescription().setTextSize(12);
    lineChart.setDrawMarkers(true);
    lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
    lineChart.animateY(1000);
    lineChart.getXAxis().setGranularityEnabled(true);
    lineChart.getXAxis().setGranularity(1.0f);
    lineChart.getXAxis().setLabelCount(lineDataSet.getEntryCount());
    lineChart.setData(lineData);
}

как мне добавить arraylist Temperature_data_entry и Temperature_dates_entry Arraylist?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...