I am trying to plot a real-time graph in my app using the values present in the database. I am able to show the updated value in the TextView
. Also rather adding values with a different ID (allocated automatically in firebase), I am updating the same parameters. For example, I am updating the parameter 'temp' rather than adding the new child to the tree everytime I want to send data to firebase. I am not able to find relevant codes or suggestion on how to plot the 'temp' value in my graph.
I tried to use GraphView
tutorials and MpAndroidChart
but all of them are working with database structure like shown in this [tutorial] (https://www.youtube.com/watch?v=d_96yv3Ppu4). And I have a structure like [this] (https://i.stack.imgur.com/PPROZ.jpg). I was not able to get any point on my graph.
This is my plotting class. Temp is the value i want to plot, lastX is just the X axis variable.
Addentry() function had the line series.appendData(new DataPoint(lastX, Double.parseDouble(temp)),true,10);
but I removed it as temp was not accessible in that class.
reff = FirebaseDatabase.getInstance().getReference().child("sensor");
series = new LineGraphSeries<DataPoint>();
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
lastX++;
String temp = dataSnapshot.child("temp").getValue().toString();
int sajal = Integer.parseInt(temp);
series.appendData(new DataPoint(lastX, Double.parseDouble(temp)),true,10);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
GraphView graph = (GraphView) findViewById(R.id.temp_graph)
graph.addSeries(series);
Viewport viewport = graph.getViewport();
viewport.setYAxisBoundsManual(true);
viewport.setMinY(0);
viewport.setMaxY(10);
viewport.setScrollable(true);
@Override
public void onResume() {
super.onResume();
// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {
@Override
public void run() {
// we add 100 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addEntry();
}
});
// sleep to slow down the add of entries
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// manage error ...
}
}
}
}).start();
}
public void addEntry() {
// here, we choose to display max 10 points on the viewport and we scroll to end
}
}
I want the plot the values stored in parameter 'temp', but what I am getting now is an empty graph.