AnyChart Android无法在for循环中填充firestore中的值



下面我试图将FireStore中的值添加到LineChart中,但图表显示为空,其中作为参考。get方法正在检索数据并保存在我的模型中,但它没有添加到序列数据中

如果我手动在序列中添加数据data.add((,它会填充在图表中,我不知道我在哪里做了错误的

伙计们,请帮帮我。

private void initChart() {
SimpleDateFormat localDateFormat;
localDateFormat = new SimpleDateFormat("MMMM-yyyy");
String date1 = localDateFormat.format(date);
Cartesian cartesian = AnyChart.line();
cartesian.animation(true);
cartesian.padding(10d, 20d, 5d, 20d);
cartesian.crosshair().enabled(true);
cartesian.crosshair()
.yLabel(true)
// TODO ystroke
.yStroke((Stroke) null, null, null, (String) null, (String) null);
cartesian.tooltip().positionMode(TooltipPositionMode.POINT);
cartesian.title("Furnace Kwh Reading for the month of "+date1);
cartesian.yAxis(0).title("Reading");
cartesian.xAxis(0).labels().padding(5d, 5d, 5d, 5d);
SimpleDateFormat localDateFormat2;
localDateFormat2 = new SimpleDateFormat("dd-yy");
List<DataEntry> seriesData = new ArrayList<>();
reference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Energy_cons_model model=documentSnapshot.toObject(Energy_cons_model.class);
String da= localDateFormat2.format(model.getTs());
int ip1= Math.toIntExact(model.getHpdc1());
int ip2= Math.toIntExact(model.getHpdc2());
int ip3= Math.toIntExact(model.getHpdc3());
seriesData.add(new CustomDataEntry(da,ip1,ip2,ip3,model.getGspm2B()));
}
}
});

Set set = Set.instantiate();
set.data(seriesData);
// Number ip3,Number gspm2b,Number gspm3b1,Number gspm3b2,Number gspm3b3,Number roto
Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
Mapping series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");
Mapping series3Mapping = set.mapAs("{ x: 'x', value: 'value3' }");
Mapping series4Mapping = set.mapAs("{ x: 'x', value: 'value4' }");
Line series1 = cartesian.line(series1Mapping);
series1.name("HPDC-1");
series1.hovered().markers().enabled(true);
series1.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series1.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series2 = cartesian.line(series2Mapping);
series2.name("HPDC-2");
series2.hovered().markers().enabled(true);
series2.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series2.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series3 = cartesian.line(series3Mapping);
series3.name("HPDC-3");
series3.hovered().markers().enabled(true);
series3.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series3.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series4 = cartesian.line(series4Mapping);
series4.name("2B");
series4.hovered().markers().enabled(true);
series4.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series4.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);

cartesian.legend().enabled(true);
cartesian.legend().fontSize(13d);
cartesian.legend().padding(0d, 0d, 10d, 0d);
anyChartView.setChart(cartesian);
}

下面是我的CustomValues类

private class CustomDataEntry extends ValueDataEntry {
CustomDataEntry(String x, Number value, Number value2, Number value3,Number value4) {
super(x, value);
setValue("value2", value2);
setValue("value3", value3);
setValue("value4", value4);
}

[1]: https://i.stack.imgur.com/bC16K.png
[2]: https://i.stack.imgur.com/n9b1t.png

我认为问题是,在得到tiger之前,您正在初始化图表的数据集。

public void updateChart(List<DataEntry> seriesData)
{
Set set = Set.instantiate();
set.data(seriesData);
// Number ip3,Number gspm2b,Number gspm3b1,Number gspm3b2,Number gspm3b3,Number roto
Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
Mapping series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");
Mapping series3Mapping = set.mapAs("{ x: 'x', value: 'value3' }");
Mapping series4Mapping = set.mapAs("{ x: 'x', value: 'value4' }");
Line series1 = cartesian.line(series1Mapping);
series1.name("HPDC-1");
series1.hovered().markers().enabled(true);
series1.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series1.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series2 = cartesian.line(series2Mapping);
series2.name("HPDC-2");
series2.hovered().markers().enabled(true);
series2.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series2.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series3 = cartesian.line(series3Mapping);
series3.name("HPDC-3");
series3.hovered().markers().enabled(true);
series3.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series3.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line series4 = cartesian.line(series4Mapping);
series4.name("2B");
series4.hovered().markers().enabled(true);
series4.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
series4.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);

cartesian.legend().enabled(true);
cartesian.legend().fontSize(13d);
cartesian.legend().padding(0d, 0d, 10d, 0d);
anyChartView.setChart(cartesian);
}

调用更新功能。

reference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DataEntry> seriesData = new ArrayList<>();
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Energy_cons_model model=documentSnapshot.toObject(Energy_cons_model.class);
String da= localDateFormat2.format(model.getTs());
int ip1= Math.toIntExact(model.getHpdc1());
int ip2= Math.toIntExact(model.getHpdc2());
int ip3= Math.toIntExact(model.getHpdc3());
seriesData.add(new CustomDataEntry(da,ip1,ip2,ip3,model.getGspm2B()));
}
updateChart(seriesData);
}
});

相关内容

  • 没有找到相关文章

最新更新