Pass Custom Parameter to query.send Google Query Language

末鹿安然 提交于 2020-01-17 04:44:07

问题


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: https://developers.google.com/chart/interactive/docs/spreadsheets#sheet-name. This function is called via a query.send(handleDataQueryResponse) call. I would have thought I could do this by calling: query.send(function() { handleDataQueryResponse(parameters) }); but this hasn't been working for me. Any ideas? Open to other approaches to making the query and its handler reusable!

More info on google's javascript chart API here: https://developers.google.com/chart/interactive/docs/quick_start.


回答1:


something like this work for you?

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

function drawChart0() {
  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);
}

function drawChart1() {
  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);
}

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



回答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;

  function drawChart(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);
  }

  function handleDataQueryResponse(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;
  }


来源:https://stackoverflow.com/questions/38135081/pass-custom-parameter-to-query-send-google-query-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!