Android MP折线图数据可视化

时间:2019-12-03 14:31:07

标签: android firebase linechart

嗨,我想制作一个图表,将数据库中的日期显示为xAxis,并在图表中写入与该日期的列相对应的值。值(必须浮动)对应于xAxis中的日期 下面的代码不打印任何内容 我的创建方法

setContentView(R.layout.chart_diabetes);
    lineChart = (LineChart) findViewById(R.id.chart);
    db = FirebaseFirestore.getInstance();
    getDataFromDb();
    LineDataSet lineDataSet = new LineDataSet(getData(), "glicemia");
    lineDataSet.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
    lineDataSet.setValueTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
    XAxis xAxis = lineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

    xAxis.setValueFormatter(new IndexAxisValueFormatter(getDate()));
    xAxis.setGranularity(1f);

    YAxis yAxisRight = lineChart.getAxisRight();
    yAxisRight.setEnabled(false);

    YAxis yAxisLeft = lineChart.getAxisLeft();
    yAxisLeft.setGranularity(1f);

    LineData data = new LineData(lineDataSet);
    lineChart.setData(data);
    lineChart.animateX(500);
    lineChart.invalidate();

从数据库检索数据的方法

private ArrayList getDataFromDb(){
    db.collection("sampleData")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d("db", document.getId() + " => " + document.getData());
                            dataset.add(document.toObject(Misurazioni.class));

                        }
                    } else {
                        Log.d("db", "Error getting documents: ", task.getException());
                    }
                    Toast.makeText(DiabeteChart.this, "val= "+ dataset.get(0).getData(), Toast.LENGTH_SHORT).show();
                }
            });
    return dataset;
}

为xAxis和数据获取数据的方法

private ArrayList<String> getDate(){
    ArrayList<String> date = new ArrayList<>();
    for (int i=0;i<dataset.size();i++){
        date.add(dataset.get(i).getData());
    }
    return date;
}

private ArrayList getData(){

    ArrayList<Entry> entries = new ArrayList<>();
    Date date = null;
    for (int i=0;i<dataset.size();i++){
        try {
             date = new SimpleDateFormat("dd/MM/yyyy").parse((dataset.get(i).getData()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        float x = date.getTime();
        entries.add(   new Entry(x,Integer.valueOf(dataset.get(i).getGlicemia())));
    }
    return  entries;
}

请帮助我:)

0 个答案:

没有答案