我有一个Firestore集合,其中包含一些文档,我想在Android Realtime图形中显示这些文档字段值。这是我遍历文档并在三个列表(温度,光和时间戳)中接收和添加值的方式。我想要两个实时折线图,第一个是yAxis中的温度和xAxis中的时间戳,第二个图中是light-timestamp。我将片段的代码提供给您。我使用jjoe64 android图形库。 Firestore collection and documents
package com.example.drodo.diamondbeacons;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.helper.StaticLabelsFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class TimelineFragment extends Fragment {
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private EditText regionText;
private Button regionChooseBtn;
public TimelineFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_timeline, container, false);
regionText = (EditText) view.findViewById(R.id.selectRegionText);
regionChooseBtn = (Button) view.findViewById(R.id.chooseRegionBtn);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
regionChooseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String select_text = regionText.getText().toString();
if (!select_text.isEmpty()) {
if(firebaseAuth.getCurrentUser() != null) {
firebaseFirestore.collection("PuckData").whereEqualTo("region", "Bedroom")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> dates_list = new ArrayList<>();
List<Integer> temperature_list = new ArrayList<>();
List<Integer> light_list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
int light = document.getLong("light").intValue();
int temperature = document.getLong("temperature").intValue();
Date date = document.getDate("timestamp");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy-HH:mm");
String stringDate = simpleDateFormat.format(date);
temperature_list.add(temperature);
light_list.add(light);
dates_list.add(stringDate);
}
Log.d("LIST", "DATES: " + dates_list.toString());
Log.d("LIST", "TEMPS: " + temperature_list.toString());
Log.d("LIST", "LIGHTS: " + light_list.toString());
} else {
Log.d("PUCK", "Error getting documents: ", task.getException());
}
}
});
}
}
regionText.setText("");
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
});
GraphView graph = (GraphView) view.findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(3, 1),
new DataPoint(5, 5),
new DataPoint(6, 3),
new DataPoint(7, 2),
new DataPoint(8, 6),
new DataPoint(9, 6),
new DataPoint(15, 6),
new DataPoint(25, 6)
});
GraphView graph2 = (GraphView) view.findViewById(R.id.graph2);
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(3, 1),
new DataPoint(5, 5),
new DataPoint(6, 3),
new DataPoint(7, 2),
new DataPoint(8, 6),
new DataPoint(9, 6),
new DataPoint(15, 6),
new DataPoint(25, 6)
});
series.setDrawBackground(true);
series.setAnimated(true);
series.setDrawDataPoints(true);
series.setTitle("Temperature(\u2103)");
graph.addSeries(series);
series2.setDrawBackground(true);
series2.setAnimated(true);
series2.setDrawDataPoints(true);
series2.setTitle("Light(%)");
graph2.addSeries(series2);
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
Viewport viewport = graph.getViewport();
viewport.setYAxisBoundsManual(true);
viewport.setMinY(0);
viewport.setMaxY(40);
viewport.setScrollable(true);
graph2.getLegendRenderer().setVisible(true);
graph2.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
Viewport viewport2 = graph2.getViewport();
viewport2.setYAxisBoundsManual(true);
viewport2.setMinY(0);
viewport2.setMaxY(100);
viewport2.setScrollable(true);
return view;
}
}