我想检索firestore并将其插入数组,然后将其绘制到图表中。
我尝试使用此代码,但我的应用程序始终崩溃,因为我正在使用笔记本电脑1GB RAM运行Android Studio,所以我看不到日志错误。
使用此代码时我的应用程序崩溃:
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < x.length; i++)
entries.add(new Entry(x[i], y[i]));
但是当我将代码更改为:
时成功ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(1f, 10f));
entries.add(new Entry(2f, 20f));
entries.add(new Entry(3f, 30f));
因此,从上面的两个示例中,我很确定是哪个问题是由我的Firestore查询引起的,但是很难知道我错过了什么地方。这是我的整个代码:
public class AnalyticCharts extends AppCompatActivity {
private int[] x;
private int[] y;
private Button percobaan;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firestore;
private CombinedChart combinedChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charts);
firebaseAuth = FirebaseAuth.getInstance();
firestore = FirebaseFirestore.getInstance();
combinedChart = findViewById(R.id.home_combined_chart);
if (firebaseAuth.getCurrentUser() != null) {
insertChart();
}
}
public void insertChart() {
Query query = firestore
.collection("DemandHistory")
.orderBy("timestamp", Query.Direction.ASCENDING);
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null) {
int c = queryDocumentSnapshots.size();
x = new int[c];
y = new int[c];
int index = 0;
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
DemandHistory dem = doc.getDocument().toObject(DemandHistory.class);
x[index] = dem.getDay();
y[index] = dem.getSold();
index++;
}
combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{CombinedChart.DrawOrder.LINE,
CombinedChart.DrawOrder.SCATTER});
YAxis leftAxis = combinedChart.getAxisLeft();
//leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
XAxis xAxis = combinedChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
CombinedData data = new CombinedData();
data.setData(generateLineData());
data.setData(generateScatterData());
combinedChart.setData(data);
combinedChart.invalidate();
}
}
});
}
private LineData generateLineData() {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < x.length; i++)
entries.add(new Entry(x[i], y[i]));
LineDataSet set = new LineDataSet(entries, "Indikator Kualitas Kecepatan");
set.setColor(Color.rgb(49, 57, 66));
set.setLineWidth(2.5f);
set.setCircleColor(Color.rgb(49, 57, 66));
set.setCircleRadius(3f);
set.setFillColor(Color.rgb(49, 57, 66));
//set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set.setDrawValues(true);
//set.setValueTextSize(10f);
//set.setValueTextColor(Color.rgb(240, 238, 70));
//set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
return d;
}
private ScatterData generateScatterData() {
ScatterData d = new ScatterData();
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < x.length; i++)
entries.add(new Entry(x[i], y[i]));
ScatterDataSet set = new ScatterDataSet(entries, "Kehadiran vs Penjualan");
set.setColors(Color.rgb(0, 156, 223));
set.setScatterShapeSize(5f);
set.setDrawValues(false);
//set.setValueTextSize(10f);
d.addDataSet(set);
return d;
}
}