【发布时间】:2022-12-01 15:10:36
【问题描述】:
Im trying to display the data i have in firestore database to my chart, ive set up an trial data on firestore to use. im new to using mpchart and firestore so im confused on how to display the data on the piechart and linegraph
This is my code so far
public class MainActivity extends AppCompatActivity {
private PieChart pieChart;
LineChart lineChart;
LineData lineData;
LineDataSet lineDataSet;
ArrayList lineEntries;
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieChart = findViewById(R.id.piechartmain);
setPieChart();
loadPiechartData();
lineChart = findViewById(R.id.linechart);
lineChart.setTouchEnabled(true);
lineChart.setPinchZoom(true);
getEntries();
lineDataSet = new LineDataSet(lineEntries, "");
lineData = new LineData(lineDataSet);
lineChart.setData(lineData);
lineData.setDrawValues(true);
lineData.setValueTextSize(12f);
lineData.setValueTextColor(Color.BLACK);
lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
lineDataSet.setValueTextColor(Color.BLACK);
lineDataSet.setValueTextSize(18f);
lineChart.getDescription().setEnabled(false);
}
private void getEntries() {
lineEntries = new ArrayList<>();
lineEntries.add(new Entry(1f, 0));
lineEntries.add(new Entry(2f, 1));
lineEntries.add(new Entry(3f, 2));
lineEntries.add(new Entry(4f, 3));
lineEntries.add(new Entry(5f, 4));
}
private void setPieChart() {
pieChart.setDrawHoleEnabled(true);
pieChart.setUsePercentValues(true);
pieChart.setEntryLabelTextSize(12);
pieChart.setEntryLabelColor(Color.BLACK);
pieChart.setCenterText("expenses");
pieChart.setCenterTextSize(24);
pieChart.getDescription().setEnabled(false);
Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setEnabled(true);
}
private void loadPiechartData() {
ArrayList<PieEntry> entries = new ArrayList<>();
String label = "type";
db.collection("Warranty")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("TAG", document.getId() + " => " + document.getData());
}
} else {
Log.d("TAG", "Error getting documents: ", task.getException());
}
}
});
entries.add(new PieEntry(0.2f,"food"));
entries.add(new PieEntry(0.15f,"elec"));
entries.add(new PieEntry(0.20f,"school"));
entries.add(new PieEntry(0.12f,"entertainment"));
entries.add(new PieEntry(0.25f,"Gas"));
ArrayList<Integer> colors = new ArrayList<>();
for (int color: ColorTemplate.MATERIAL_COLORS){
colors.add(color);
}
for (int color: ColorTemplate.VORDIPLOM_COLORS) {
colors.add(color);
}
PieDataSet dataSet = new PieDataSet(entries, "monthly expense");
dataSet.setColors(colors);
PieData data = new PieData(dataSet);
data.setDrawValues(true);
data.setValueFormatter(new PercentFormatter(pieChart));
data.setValueTextSize(12f);
data.setValueTextColor(Color.BLACK);
pieChart.setData(data);
pieChart.invalidate();
pieChart.animateY(1000, Easing.EaseInOutQuad);
}
}
</androidx.constraintlayout.widget.ConstraintLayout>
here is my database enter image description here
im kinda lost on how to display data from firestore since i searched their query already, though i cant display it on the chart. im new so please guide me, thanks!
【问题讨论】:
-
Please edit your question and add your database structure as a screenshot. Besides that, what exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors?
-
You never do anything with the data you fetch inside
onCompletewith the query - you would need to add it to an array and set that on the chart (either insideonCompleteor by calling a separate method fromonCompletewith the data you retrieved). -
@AlexMamo thanks ill change it, there's nothing wrong with the code, i just dont know how to display the data i got after i get in from the firebase. i hope to know how to code it since i cant find a tutorial that explains how because im new to coding.
-
@TylerV oh okay thanks! can you please maybe show how to add that array, since ive tried adding it on the Arraylist<pieEntries> but it doesnt seem to work. i kinda get the i should put it in a new arraylist but i dont know how to code and place the data from the oncomplete to the arraylist.
标签: java android-studio google-cloud-firestore mpandroidchart