javascript - 加载第三个谷歌图表时出现问题

标签 javascript jquery charts google-visualization

我正在尝试在页面上加载三种不同的谷歌图表,一种仪表,一种折线图和一种条形图。但是当我尝试实现第三个图表时,出现了一些问题。有时会加载仪表图和折线图,但不加载条形图,有时仅加载仪表图和条形图。我无法同时加载所有三个。我可以寻求帮助吗?

这是我的代码:

<html>

<head>
  <title>Usage statistic</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <script type="text/javascript">
    google.load("visualization", "1", {
      packages: ["corechart", "gauge", "line", "bar"],
      'language': 'no'
    });
    google.setOnLoadCallback(init);

    function init() {
      drawLine();
      drawBar();
      drawGauge();


    }

    function drawGauge() {

      var data = google.visualization.arrayToDataTable([
        ['Label', 'Value'],
        ['Month', 73.6],
        ['Today', 62.7],
        ['Week', 73.6],
      ]);

      var options = {
        width: 800,
        height: 240,
        greenFrom: 40,
        greenTo: 100,
        redFrom: 0,
        redTo: 20,
        yellowFrom: 20,
        yellowTo: 40,
        minorTicks: 5
      };

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

    function drawLine() {
      var data = new google.visualization.DataTable();
      data.addColumn('date', 'Date');
      data.addColumn('number', 'Logins');

      data.addRows([

        [new Date(2015, 10, 16), 1971],
        [new Date(2015, 10, 17), 1973],
        [new Date(2015, 10, 18), 1964],
        [new Date(2015, 10, 19), 1981],
        [new Date(2015, 10, 20), 1919],
        [new Date(2015, 10, 21), 1185],
        [new Date(2015, 10, 22), 1104],
        [new Date(2015, 10, 23), 2009],
        [new Date(2015, 10, 24), 1955],
        [new Date(2015, 10, 25), 1933],
        [new Date(2015, 10, 26), 1923],
        [new Date(2015, 10, 27), 1854],
        [new Date(2015, 10, 28), 1159],
        [new Date(2015, 10, 29), 1107],
        [new Date(2015, 10, 30), 2006],
        [new Date(2015, 11, 01), 1998],
        [new Date(2015, 11, 02), 1951],
        [new Date(2015, 11, 03), 1921],
        [new Date(2015, 11, 04), 1870],
        [new Date(2015, 11, 05), 1174],
        [new Date(2015, 11, 06), 1128],
        [new Date(2015, 11, 07), 2030],
        [new Date(2015, 11, 08), 1912],
        [new Date(2015, 11, 09), 1956],
        [new Date(2015, 11, 10), 1942],
        [new Date(2015, 11, 11), 1895],
        [new Date(2015, 11, 12), 1168],
        [new Date(2015, 11, 13), 1148],
        [new Date(2015, 11, 14), 2004],
        [new Date(2015, 11, 15), 1954],
      ]);

      var options = {
        title: 'Logins',
        width: 800,
        height: 400,
        hAxis: {
          format: 'MMM d, y',
          gridlines: {
            count: 15
          }
        },
        vAxis: {
          gridlines: {
            color: 'none'
          },
          minValue: 0
        }
      };

      var chart = new google.charts.Line(document.getElementById('line_div'));
      chart.draw(data, options);
    }

    function drawBar() {
      var data = new google.visualization.DataTable();
      data.addColumn('timeofday', 'Time of Day');
      data.addColumn('number', 'Emails Received');

      data.addRows([
        [
          [8, 30, 45], 5
        ],
        [
          [9, 0, 0], 10
        ],
        [
          [10, 0, 0, 0], 12
        ],
        [
          [10, 45, 0, 0], 13
        ],
        [
          [11, 0, 0, 0], 15
        ],
        [
          [12, 15, 45, 0], 20
        ],
        [
          [13, 0, 0, 0], 22
        ],
        [
          [14, 30, 0, 0], 25
        ],
        [
          [15, 12, 0, 0], 30
        ],
        [
          [16, 45, 0], 32
        ],
        [
          [16, 59, 0], 42
        ]
      ]);

      var options = google.charts.Bar.convertOptions({
        title: 'Total Emails Received Throughout the Day',
        width: 800,
        height: 400
      });

      var chart = new google.charts.Bar(document.getElementById('bar_div'));
      chart.draw(data, options);
    }
  </script>

  <style>
    .menu {
      border-radius: 5px;
      border: 1px solid black;
      float: left;
      padding: 0px 6px;
      margin-right: 6px;
      color: #686868;
    }
    .menu a {
      text-decoration: none;
      color: #686868;
    }
    .active a {
      color: black !important;
    }
  </style>
</head>

<body style="background-color: #F0F0F0; margin: 20px;">
  <div>
    <h2>Logins in percentages</h2>
  </div>
  <div id="gauge_div"></div>
  <div>
    <h2>Logins last 30 days</h2>
  </div>
  <div id="line_div"></div>
  <div>
    <h2>Emails Received</h2>
  </div>
  <div id="bar_div"></div>

</body>

</html>

最佳答案

我无法真正回答为什么,但是如果您查看加载库的文档 link ,您会看到他们已经更新了操作方式。而且和以前有很大不同。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {
  packages: ["corechart", "gauge", "line", "bar"],
  'language': 'no'
});
google.setOnLoadCallback(init);

必须更改为

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load("current", {packages: ["corechart", "gauge", "line", "bar"]});
  google.charts.setOnLoadCallback(init);
  ....
</script>

看起来一切都在我的应用程序中正常工作,使用旧的加载样式,但是尝试您的场景对于旧样式根本不起作用。将其更改为即可。

Jsfiddle

编辑:

如果有人感兴趣,我会发现为什么我的东西可以工作。我专门使用 Wrappers,显然您应该使用 google.load(旧方式),而不是新的 google.charts.load

Source

关于javascript - 加载第三个谷歌图表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34309906/

相关文章:

javascript - 如何编写 Firebase 规则来检查访问 token ?

php - 如果 MySQL 结果为空显示确认框

php - WooCommerce:结帐验证失败后更新自定义字段

javascript - 如何在 d3 中显示分组条形图数据右侧的第二个 y 轴

javascript - Nvd3 - 尽管添加了所有外部资源,散点图仍不会加载

javascript - Highcharts JS 'columnrange' 图表值格式

javascript - 简单的?? onClick qTip 一些链接但不是全部

Javascript 字符串到 int 的转换

javascript - 如何以vb.net Web形式创建简单的 Accordion

javascript - 在 Javascript 中显示带有自定义消息的确认框