Jquery loop to read 3 objects and create 3 dataPoints for Charts canvas

Asked

Viewed 39 times

0

I’m trying to accomplish the loop on the return of this request AJAX. In the first event success, I make a second call AJAX which will return - in this case - 3 objects JSON.

My doubt is how to create, in the second event success, 3 dataPoints that will generate and popular 3 graphics made in canvas?

Code jQuery:

$.ajax({
            type: 'get',
            url: 'dadosPS.php?ps=getGerencialPergunta&pesquisa='+slct_ps+'&datainicio='+dtin+'&datafim='+dtf,
            dataType: 'json',
            async: true,
            beforeSend: function () {

            },
            success: function (data, textStatus, jqXHR) {
              var dataPoints = [];
              var dataPoints2 = [];
              var tt = 0;
              var count = $.map(data.data, function(n, i) { return i; }).length;//conta qtd de perguntas

              $.each(data.data, function (key, val) {
               id = val.id;
               dataPoints.push({y:parseInt(val.qtd),label:val.pergunta});
                    if(val.nr_pergunta == '1'){
                       tt = val.qtd++;
                    }
                        $.ajax({
                            type: 'get',
                            url: 'dadosPS.php?ps=getGerencialResposta&id='+id+'&pesquisa='+slct_ps+'&datainicio='+dtin+'&datafim='+dtf,
                            dataType: 'json',
                            async: false,
                            beforeSend: function () {

                            },
                            success: function (data, textStatus, jqXHR) {
                             console.log(data);                      
                            },
                            complete: function () {

                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                            }
                        });  



              });

               //grafico das perguntas
                    var chart = new CanvasJS.Chart("teste0", {
                    //colorSet:  "customColorSet1",
                    title: {
                        text: tt,
                          //horizontalAlign: "left"
                          verticalAlign: "center",
                          dockInsidePlotArea: true
                    },
                    animationEnabled: true,
                    exportEnabled: true,
                legend: {
                maxWidth: 350,
                itemWidth: 120
            },
                    data: [{
                            type: "doughnut",
                            indexLabel: "{label} {y}",
                            //startAngle: 240,
                            //showInLegend: true,
                //legendText: "{label}",
                            dataPoints: dataPoints
                        }]
                });
                chart.render();




            },
            complete: function () {

            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
       }); 

Return of the second success

inserir a descrição da imagem aqui

  • explain a little better please, you already make the creation of datapoints in the first call, why not replicate the strategy in the second call?

  • Then in the first I give a given and the code has an id I read in each and pass that id to the second ajax.... in this case are three ids and on the return of the second ajax I need to create the 3 Graphics but I am not getting.

  • I haven’t gone over the details, but just seeing an Ajax being called into a loop each gives me the creeps. Imagine calling one Ajax after another without doing "queue"... I don’t know if this works.

  • Of course it’s not the best way but if you have another way, please give me a hint :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.