0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sajal
  • 89
  • 1
  • 14
  • 1
    Take a look at [this question](https://stackoverflow.com/q/40447073/4916627). The answer on that question explaines how you can handle this situation and what is causing it. – André Kool Mar 24 '19 at 11:18
  • @AndréKool Thank you for your reply. The question that you stated is regarding not being able to fetch data into the app, but my problem is not that. I can fetch data, but i am unable to plot it into the graph. – Sajal Mar 24 '19 at 11:24
  • 1
    Nope, same problem with different results. If you add log statements as stated in [Frank's answer](https://stackoverflow.com/a/40447525/4916627) on that question you can see that at the time you create the graph your data hasn't been loaded yet. – André Kool Mar 24 '19 at 11:28
  • Data is loaded from Firebase asynchronously. By the time your `graph.addSeries(series);` is called, the data hasn't been loaded yet, and your `onDataChange` hasn't been called yet. Any code that requires the data, must be inside the `onDataChange` callback that is invoked when the data is available from the server, So the call to `graph.addSeries(series);` should be **inside** `onDataChange`. – Frank van Puffelen Mar 24 '19 at 14:50

0 Answers0