1

I am using GraphView to plot a data from firebase database.

Here's my firebase database structure.

What i want to achieve is display tmpHr as y value, with Timestamp as the label in x axis. I have retrieved the data from firebase, and I have successfully plotted the tmpHr in y axis. I have tried the method below, but it doesn't work. How can I get this the right way?

Any help would be appreciated. Thank you very much.

public class RetrieveApp extends AppCompatActivity {

    FirebaseAuth auth;
    FirebaseUser user;


    int hrValue;
    String hrValueTimestamp;

    ArrayList<Integer> array2; //array for tmpHr value
    ArrayList<String> array7; //array for hrValueTimestamp
    LineGraphSeries<DataPoint> series ;
    int x=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_app);
        DatabaseReference userdatabase = FirebaseDatabase.getInstance().getReference().child("Users");
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        final GraphView graph = (GraphView)findViewById(R.id.graph);
        series = new LineGraphSeries<>();
        graph.addSeries(series);


        DatabaseReference ref = userdatabase.child(user.getUid());

        array2 = new ArrayList<>(); //array for tmpHR
        array7 = new ArrayList<>(); //array for hrValueTimestamp



       ref.child("hrvalue").child("nilaihr").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            hrValue = dataSnapshot.child("tmpHR").getValue(Integer.class);
            hrValueTimestamp = dataSnapshot.child("Timestamp").getValue(String.class);

            Log.i("timestamp value", "timestamp value " + hrValueTimestamp);
            Log.i("hr value", "hr value " + hrValue);
            array2.add(hrValue);
            array7.add(hrValueTimestamp);
                x = x+1;
                DataPoint point = new DataPoint(x, hrValue);
                series.appendData(point, false, 1000);
                graph.setCustomLabelFormatter(new CustomLabelFormatter() {
                @Override
                public String formatLabel(double value, boolean isValueX) {
                    if (isValueX) {
                        return (hrValueTimestamp);
                    }
                    return null;
                }
            });

        }




            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



    }


}
Astrid Gloria
  • 11
  • 1
  • 5

1 Answers1

0

You can do it using static labels formatter:

StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(array7); // <- array with you labels
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

Since the labels are very long, maybe you'll want to set an angle for them so that they do not overlap:

graph.getGridLabelRenderer().setHorizontalLabelsAngle(135);
Psytho
  • 3,313
  • 2
  • 19
  • 27