Skip to content Skip to sidebar Skip to footer

Pass Custom Parameter To Query.send Google Query Language

I am using google's charts API to create a web-based dashboard. I want to draw many graphs and need to pass custom parameters to the handleDataQueryResponse function from this link

Solution 1:

something like this work for you?

google.charts.load('current', {
  callback: function () {
    drawChart0();
    drawChart1();
  },
  packages:['corechart', 'table']
});

functiondrawChart0() {
  var chart = new google.visualization.BarChart(document.getElementById('barchart'));
  var query = "http://www.google.com/fusiontables/gvizdata?tq=SELECT 'Label' as beach, 'Pieces total' as Total FROM 1c-FiEDwdwMt55a4AlE8Xuu40rUBR_qeI8ENiHtPV";
  var options = {
    animation: {
      duration: 500,
      startup: true,
      easing: 'out'
    },
    chartArea: {
      width: '40%'
    }
  };
  sendQuery(query, chart, options);
}

functiondrawChart1() {
  var chart = new google.visualization.Table(document.getElementById('tablechart'));
  var query = "https://docs.google.com/spreadsheets/d/19olM1pEF5qQvvKhwSH3d_X4w2DVfOWDwDtZKNFlMY3w/edit#gid=0&tq=select A, B where A >= date '2016-01-01'";
  var options = {};
  sendQuery(query, chart, options);
}

functionsendQuery(query, chart, options) {
  new google.visualization.Query(query).send(function (response) {
    chart.draw(response.getDataTable(), options);
  });
}
<scriptsrc="https://www.gstatic.com/charts/loader.js"></script><divid="barchart"></div><divid="tablechart"></div>

Solution 2:

I figured out one solution, and let me tell you, it's not pretty. But it works. As always, happy for improvements and feedback! Thanks @WhiteHat for helping me out.

google.charts.setOnLoadCallback(function() {
    drawChart('chart1', 0, 'SELECT A, B, C, D, E', {title: 'Chart 1'})

    drawChart('chart2', 1, 'SELECT A, H, I, J', {title: 'Chart 2'})
});

var chartsArray = [];
var optionsArray = [];
var nextID = 0;

functiondrawChart(tag, id, sqlCommand, options= {}, sheetName='Sheet1', numHeaders=1) {
    chartsArray[id] = new google.visualization.ColumnChart(document.getElementById(tag));

    optionsArray[id] = options;

    var queryString = encodeURIComponent(sqlCommand);

    var query = new google.visualization.Query(
       'https://docs.google.com/spreadsheets/d/1dfRA_NDsdfED3OEdx-v3ZzA2-oWPS4kU_2mV-PY/gviz/tq?sheet=' + sheetName + '&headers=' + numHeaders.toString() + '&tq=' + queryString);
      query.send(handleDataQueryResponse);
}

functionhandleDataQueryResponse(response, id) {
    if (response.isError()) {
      alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
      return;
    }

    var data = response.getDataTable();
    chartsArray[nextID].draw(data, optionsArray[nextID]);
    nextID += 1;
}

Post a Comment for "Pass Custom Parameter To Query.send Google Query Language"