Get values from Firebase real database to make a chart with them

SilentKunZ :

I want to get values of sensors from Firebase and make a chart from that values. I wrote some code but it doesn't work. The graph should be automatically based on the values from the database, that is, there should be a complete synchronization of the graph with the values. Here my db:

..sensor
-Lh51ca_FMrRMOA0Ok9B
hum: 36.1
pressure: 1008
temp: 31.2
time: "2019-06-11T13:17:03Z"

-and a lot of nodes

I using GraphView for making a chart

protected void onCreate(){
    ....
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot myDataSnapshot : dataSnapshot.getChildren())
            {
                double temp = myDataSnapshot.child("temp").getValue(Double.class);
                double hum = myDataSnapshot.child("hum").getValue(Double.class);
                series.appendData(new DataPoint(lastXPoint, temp), false, 1000);
                series2.appendData(new DataPoint(lastXPoint, hum), false, 1000);
            }


        }

Only two points appeared on the graph and thats all.

Edit:

    protected void onCreate(){
        ....
        reference.addChildEventListener(new ChildEventListener() {
            public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
                for(DataSnapshot myDataSnapshot : dataSnapshot.getChildren())
                {
                    lastXPoint +=0.1d;
                    double temp = myDataSnapshot.child("temp").getValue(Double.class);
                    double hum = myDataSnapshot.child("hum").getValue(Double.class);
                    series.appendData(new DataPoint(lastXPoint, temp), false, 1000);
                    series2.appendData(new DataPoint(lastXPoint, hum), false, 1000);
                }
            }
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }
            public void onCancelled(DatabaseError firebaseError) { }
        });
}

UPDATE. Now the chart is updated in real time. Seem to be it's works well

reference.addChildEventListener(new ChildEventListener() {
            public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
            lastXPoint +=0.1d;
            double temp = dataSnapshot.child("temp").getValue(Double.class);
            double hum = dataSnapshot.child("hum").getValue(Double.class);
            series.appendData(new DataPoint(lastXPoint, temp), false, 1000);
            series2.appendData(new DataPoint(lastXPoint, hum), false, 1000); 
            }
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }
            public void onCancelled(DatabaseError firebaseError) { }
        });
Anjana :

lastXPoint variable seems to be constant within in the loop. So you append values to the same X point in the graph again and again through out the loop.

You have to vary the lastXPoint as well to draw a continuous graph with all the values in the DB nodes.

Edit: For duplicating value issue

addvalueeventlistner will bring all the nodes again again once the DB is updated. To bring the lastly added node only use below.

Firebase ref = new Firebase("YOUR-URL-HERE/PATH/TO/YOUR/STUFF");

ref.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
    }
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    }
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    }
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    }
    public void onCancelled(FirebaseError firebaseError) { }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156162&siteId=1