Error using Google graphics library

Asked

Viewed 78 times

0

I am performing a simple project, where I am using Google library to work with graphics, but I am facing a problem when "plotting" the results, the following error is presented:

You called the draw() method with the Wrong type of data rather than a Datatable or Dataview

Code excerpt from the code I’m using:

/* variável constante */
var candidatoA = 'A';
var candidatoC = 'C';

/* variável auxiliar */
var votosCandidatoA = 0;
var votosCandidatoC = 0;

/* requisição para o biblioteca do google */
google.load("visualization", "1", { packages: ["corechart"] });
google.charts.load('current', { 'packages': ['corechart'] });
google.setOnLoadCallback(processarPopulacao);
google.setOnLoadCallback(processarAmostra);
google.charts.setOnLoadCallback(graficoProporcaoVotos);

/* função para realizar o processamento dos dados de populacao */
function processarPopulacao() {
    var consultaString = encodeURIComponent('SELECT A, B, C');
    var magica = '/gviz/tq?gid=0&headers=1&tq=';
    var url = 'https://docs.google.com/spreadsheets/d/1unuhxkhDg2qTQnhTcYmC2GkEIPM18ai8PGtSsI44Bd8';
    var query = new google.visualization.Query(url + magica + consultaString);
    query.send(consumirDadosPopulacao);
}

/* função para realizar o processamento dos dados de amostra */
function processarAmostra() {
    var consultaString = encodeURIComponent('SELECT A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, AA, AB, AC, AD, AE, AG');
    var magica = '/gviz/tq?gid=0&headers=1&tq=';
    var url = 'https://docs.google.com/spreadsheets/d/1UUPbVQ6p8gOFRMznBhsxJXEUW-Axx8C2LnLKWutcL6Y';
    var query = new google.visualization.Query(url + magica + consultaString);
    query.send(consumirDadosAmostra);
}

/* função para consumir os dados de populacao */
function consumirDadosPopulacao(resposta) {
    if (resposta.isError()) {
        alert('Erro: ' + resposta.getMessage() + ' ' + resposta.getDetailedMessage());
        return;
    }
    var dados = resposta.getDataTable();
    dados.Nf.filter(function(valores) {
        proporcaoDeVotos(valores.c[2].v);
    });
    console.log("Total de votos para o candidato A: " + votosCandidatoA);
    console.log("Total de votos para o candidato C: " + votosCandidatoC);
}

/* função para consumir os dados de populacao */
function consumirDadosAmostra(resposta) {}

/* função para calcular a quantidade de votos para cada candidato */
function proporcaoDeVotos(voto) {
    if (voto == candidatoA) {
        votosCandidatoA++;
    } else {
        votosCandidatoC++;
    }
}

/* função para desenhar o gráfico de proporção de votos */
function graficoProporcaoVotos() {
    var data = google.visualization.arrayToDataTable([
        ['Candidato', 'Votos'],
        ['candidatoA', votosCandidatoA],
        ['candidatoC', votosCandidatoC]
    ]);

    var options = {
        backgroundColor: 'transparent',
    };
    var chart = new google.visualization.PieChart(document.getElementById('piechart'));
    chart.draw(data, options);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script src="https://www.gstatic.com/charts/loader.js" type="text/javascript"></script>
<div id="piechart"></div>

The rest of the code makes a request about the data using the google api to access the data in spreadsheets. It is important to realize that the data are presented correctly, IE, must be occurring some problem at the time of drawing the graph.

  • I entered your code inside the snippe to organize and test the same, and is working as you can verify, as is sending the data of votes to the js graph? you are using jquery what version? a div which will display the chart has id="piechart"?

  • I will add all the code here, so it is simpler to show the problem that is occurring.

  • This edits your question and inserts your problem here, this way it can help other people, external links may fail to work and the replies do not match the legal feat.

  • I added all my code, it is performing some requests using google library for spreadsheets, but as you can notice this does not seem to be quite the problem, but rather something related to the same graph.

  • I still don’t understand the problem, this link from Github which has passed to a page that when accessing it has the result, which is 58.6% at the Candidatoc and 41.1% at the Candidatoa, is not correct ?

  • No, I’ll even remove the link to access, that page has fixed values, in my case this can not be performed!

  • I actually need that script because I’m using that library to access the google drive spreadsheet

  • Sorry, I made a mess here, you want to update as the document is updated on Drive correct ?

  • Well actually that document will not occur any more updates, but I wanted to perform the calculation dynamically, without having to put fixed, my problem is time to draw the graph, it seems that some problem occurs with the values that are in the variables

Show 4 more comments
No answers

Browser other questions tagged

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