androidgoogle-cloud-firestorelinechartanychart

AnyChart Android unable to populate values from firestore in for loop


Below iam trying to add the values from FireStore to LineChart but chart shows blank,Where as reference.get methods is retreiving data and saving in my model but its not adding up in series data

if i manually add data in the seriesData.add() it populates in the chart, i dont know where i am doing wrong

Please help me guys.

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);

    }

below is my 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.sstatic.net/bC16K.png
  [2]: https://i.sstatic.net/n9b1t.png

Solution

  • I think issue is you are initializing dataset of chart before getting 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);
    }
    

    Call update function.

            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);
                }
            });