How to pass values from an Ajax request to Google Chart?

5

I started to tweak Google Chart today, and I can graph the values out of the ajax request.

Only my application needs this data coming from a database and I'm not sure how to pass them correctly to the Google Chart array.

NOTE: I do not know if inside the ajax code would function this function drawChart ...

Here is my code with the attempt to pass parameters:

$(document).on("change", "#cbExercicio", function () {
    var caminho = "/EBITDA/AjaxObterEBITDA";

    if ($(this).val() != 0) {
        var num_exercicio = $(this).val();
        $.ajax({
            type: "POST",
            url: caminho,
            data: "{ 'num_exercicio': '" + num_exercicio + "'}",
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (resp) {
                if (resp.Num_Periodos == 6) {
                    google.load("visualization", "1", { packages: ["corechart"] });

                    google.setOnLoadCallback(drawChart(resp));

                    function drawChart(resp) {
                        var data = google.visualization.arrayToDataTable([
                          ['Year', 'Sales', 'Expenses'],
                          ['1º Bimestre', resp.Meta1, resp.Real1],
                          ['2º Bimestre', resp.Meta2, resp.Real2],
                          ['3º Bimestre', resp.Meta3, resp.Real3],
                          ['4º Bimestre', resp.Meta4, resp.Real4],
                          ['5º Bimestre', resp.Meta5, resp.Real5],
                          ['6º Bimestre', resp.Meta6, resp.Real6]
                        ]);

                        var options = {
                            title: 'Company Performance',
                            hAxis: { title: 'Year', titleTextStyle: { color: 'red' } }
                        };

                        var chart = new google.visualization.ColumnChart(document.getElementById('grafico4'));
                        chart.draw(data, options);
                    }

                }
          }
     });
   }
});

The screen goes blank when I try to do so. You can not debug it as it is inside ajax.

The original graphic example is this:

    <html>
      <head>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script><scripttype="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Year', 'Sales', 'Expenses'],
              ['2004',  1000,      400],
              ['2005',  1170,      460],
              ['2006',  660,       1120],
              ['2007',  1030,      540]
            ]);

            var options = {
              title: 'Company Performance',
              hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
            };

            var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
        </script>
      </head>
      <body>
        <div id="chart_div" style="width: 900px; height: 500px;"></div>
      </body>
    </html>

I think the error is in one of these excerpts:

google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart(resp));    
function drawChart(resp) { 

When I comment on the google charts part everything happens normal. But when I unzip the html page it goes without source code and the whole page appears blank

    
asked by anonymous 21.01.2014 / 19:28

1 answer

3

I was able to solve the problem and solve the problem.

Follow correct code:

Before request ajax initialize object json and call load from Google:

var resp: [];
google.load("visualization", "1", { packages: ["corechart"] });

After request ajax only calling function with json as parameter:

$(document).on("change", "#cbExercicio", function () {
  var caminho = "/EBITDA/AjaxObterEBITDA";

  if ($(this).val() != 0) {
    var num_exercicio = $(this).val();

    $.ajax({
      type: "POST",
      url: caminho,
      data: "{ 'num_exercicio': '" + num_exercicio + "'}",
      dataType: 'json',
      contentType: "application/json; charset=utf-8",
      success: function (resp) {
        if (resp.Num_Periodos == 6) {
          drawChart(resp);
        }
      }
    });
  }
});

This code snippet was left out and lastly:

function drawChart(resp) {
  var data = google.visualization.arrayToDataTable([
    ['Year', 'Sales', 'Expenses'],
    ['1º Bimestre', resp.Meta1, resp.Real1],
    ['2º Bimestre', resp.Meta2, resp.Real2],
    ['3º Bimestre', resp.Meta3, resp.Real3],
    ['4º Bimestre', resp.Meta4, resp.Real4],
    ['5º Bimestre', resp.Meta5, resp.Real5],
    ['6º Bimestre', resp.Meta6, resp.Real6]
  ]);

  var options = {
    title: 'Company Performance',
    hAxis: { title: 'Year', titleTextStyle: { color: 'red' } }
  };

  var chart = new google.visualization.ColumnChart(document.getElementById('grafico4'));
  chart.draw(data, options);
}
    
21.01.2014 / 20:16