AnyChart Android 无法在 for 循环中填充来自 firestore 的值
AnyChart Android unable to populate values from firestore in for loop
下面我尝试将 FireStore 中的值添加到折线图,但图表显示空白,而 reference.get 方法正在检索数据并保存在我的模型中,但它没有添加到系列数据中
如果我在图表中填充的 seriesData.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 class
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
我认为问题是您在获取老虎之前正在初始化图表数据集。
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);
}
});
下面我尝试将 FireStore 中的值添加到折线图,但图表显示空白,而 reference.get 方法正在检索数据并保存在我的模型中,但它没有添加到系列数据中
如果我在图表中填充的 seriesData.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 class
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
我认为问题是您在获取老虎之前正在初始化图表数据集。
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);
}
});