【问题标题】:google charts toolbar not showing/executing谷歌图表工具栏未显示/执行
【发布时间】:2016-04-02 05:06:12
【问题描述】:

我是新来的,是朋友介绍的。我正在使用谷歌图表,连接到谷歌表格。我的图表工作正常。下一步是在其底部添加一个工具栏。但是,无论如何,我都无法使代码正常工作。我什至尝试将Google's example code 复制并粘贴到空白页中进行测试,但我什至无法展示他们的示例。文档中是否缺少阻止这种情况的内容? 这是我的代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!-- datasheet link https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/edit?usp=sharing 
https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/pubhtml
-->

    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.charts.load('current', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.charts.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
       function drawAll() {drawChart(); drawToolbar();}

        function drawChart() {
          var query = new google.visualization.Query("https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5");
          query.send(handleQueryResponse);
        }

        function handleQueryResponse(response) {
        var data = response.getDataTable();
        var options = {
            'title':'College Readiness',
            'titleTextStyle': {fontSize: '24', color: 'teal'},
            //'width':800,
            'height':600,
            hAxis: {'title': 'Academic Year', 'textStyle': {bold: true, fontSize: '16'}},
            vAxis: {'title': 'Percent of Students Ready', 'format': 'percent','textStyle': {color: 'gray', fontSize: '9'}},
            legend: {'position': 'top'},
            series: {
                0: {pointsVisible: true, color: 'orange'},
                1: {pointsVisible: true, color: 'blue'},
                2: {pointsVisible: true, color: 'black'},
                3: {pointsVisible: true, pointShape: 'square', pointSize: '14', color: 'maroon'}
            }
            };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        //chart.draw(data, options);

    function drawToolbar() {
      var components = [
          {type: 'html', datasource: 'https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5'},
          {type: 'csv', datasource: 'https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5'}
      ];

      var container = document.getElementById('toolbar_div');
      google.visualization.drawToolbar(container, components);
          };

google.charts.setOnLoadCallback(drawAll);
        }
  </script>

        </head>

<body>
<!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
    <div id="toolbar_div"></div>
</body>
</html>

【问题讨论】:

    标签: google-visualization


    【解决方案1】:

    似乎在这里工作,有几个小的调整。

    通常,setOnLoadCallback 每页只能调用一次。
    这很可能是问题所在。
    在这里,我在load 语句中定义了callback

    // Load the Visualization API and the piechart package.
    google.charts.load('current', {
      callback: drawChart,
      packages:['corechart']
    });
    
    function drawChart() {
      var query = new google.visualization.Query("https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5");
      query.send(handleQueryResponse);
    }
    
    function handleQueryResponse(response) {
      var data = response.getDataTable();
      var options = {
        'title':'College Readiness',
        'titleTextStyle': {fontSize: '24', color: 'teal'},
        //'width':800,
        'height':600,
        hAxis: {'title': 'Academic Year', 'textStyle': {bold: true, fontSize: '16'}},
        vAxis: {'title': 'Percent of Students Ready', 'format': 'percent','textStyle': {color: 'gray', fontSize: '9'}},
        legend: {'position': 'top'},
        series: {
            0: {pointsVisible: true, color: 'orange'},
            1: {pointsVisible: true, color: 'blue'},
            2: {pointsVisible: true, color: 'black'},
            3: {pointsVisible: true, pointShape: 'square', pointSize: '14', color: 'maroon'}
        }
      };
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    
      var components = [
        {type: 'html', datasource: 'https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5'},
        {type: 'csv', datasource: 'https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:E5'}
      ];
    
      var container = document.getElementById('toolbar_div');
      google.visualization.drawToolbar(container, components);
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="toolbar_div"></div>
    <div id="chart_div"></div>

    【讨论】:

    • 非常感谢。这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 2013-03-20
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多