Graphview:将Arraylist显示为X轴标签

时间:2018-05-23 14:00:38

标签: android firebase firebase-realtime-database android-graphview

我正在使用GraphView绘制来自firebase数据库的数据。

Here's my firebase database structure.

我想要实现的是将tmpHr显示为y值,将Timestamp作为x轴中的标签。我从firebase中检索了数据,并且已经在y轴上成功绘制了tmpHr。我尝试了下面的方法,但它不起作用。我怎样才能以正确的方式获得这个?

任何帮助将不胜感激。非常感谢你。

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) {

            }
        });



    }


}

1 个答案:

答案 0 :(得分:0)

您可以使用静态标签格式化程序:

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

由于标签很长,可能你想为它们设置一个角度,使它们不重叠:

graph.getGridLabelRenderer().setHorizontalLabelsAngle(135);