javascript - Google Charts 外部 Javascript 问题

标签 javascript charts external

我的问题是,当我将任何谷歌图表的 js 代码放入外部 javascript 文件时。它开始加载页面并且不显示任何内容。如果是内联 javascript,它工作正常。

以下是我的 HTML 代码“google barchart.html”

 <html>
     <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
       <script type="text/javascript" src="jquery.js"></script>
       <script type="text/javascript" src="https://www.google.com/jsapi"></script>
       <script type="text/javascript" src="test.js"></script>
       <script type="text/javascript"></script>
    </head>
    <body>
      <input type="button" id="btn" value="Show Graph" />
      <div id="chart_div" style="width: 441px; height: 300px;"></div>
    </body>
 </html>

这是我的 js 文件“test.js”

 $(document).ready(function()  {    $('#btn').click(function()      {   //alert("hi");
       google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['', 'Your Restaurant', 'Other Restaurants'],
          ['Question1',  5, 4],
          ['Question2',  4, 5],
          ['Question3',  3, 2],
          ['Question4',  5, 1]
        ]);

        var options = {
          //title: 'Company Performance',
          hAxis: {title: 'Questions', titleTextStyle: {color: 'red'}},
          vAxis: {title: '1 = POOR, 5 = EXCELLENT', titleTextStyle: {color: '#FF0000'}, maxValue:'5', minValue:'1'},
        };

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

*注意:当这段代码不在任何 js 函数中时,它在外部 js 中也能正常工作。但我想在 Javascript 函数中使用它。

提前致谢。

莫阿兹

最佳答案

我将您的代码修正为您可能会使用的 2 个可行的解决方案(已在 IE、Chrome 和 Mozilla 上测试过)。

  1. JavaScript 加载索引页
  2. 点击按钮后加载 JavaScript

.


解决方案 1:JavaScript 使用索引页加载

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<input type="button" id="btn" value="Show Graph">
<div id="chart_div" style="width: 441px; height: 300px;"></div>
</html>

测试.js

google.load('visualization', '1', {'packages':['corechart']});

$(document).ready(function() {    
$("#btn").click(function() {
$("#chart_div").load("", function(){
var data = google.visualization.arrayToDataTable([
           ['', 'Your Restaurant', 'Other Restaurants'],
           ['Question1',  5, 4],
           ['Question2',  4, 5],
           ['Question3',  3, 2],
           ['Question4',  5, 1]
           ]);

var options = {
    title: 'Company Performance',
    hAxis: {title: 'Questions', titleTextStyle: {color: 'red'}},
    vAxis: {title: '1 = POOR, 5 = EXCELLENT', titleTextStyle: {color: '#FF0000'}, maxValue:'5', minValue:'1'},
    tooltip: {trigger: 'hover'}};

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

解决方案 2:点击按钮后加载 JavaScript

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});

function loadjsfile(filename, filetype) 
     {
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
  document.getElementsByTagName("head")[0].appendChild(fileref)
     }
</script>
</head>
<input type="button" id="btn" value="Show Graph" onclick="loadjsfile('test2.js','js')">
<div id="chart_div" style="width: 441px; height: 300px;"></div>
</html>

test2.js

$("#chart_div").load("",function(){
var data = new google.visualization.arrayToDataTable([
           ['', 'Your Restaurant', 'Other Restaurants'],
           ['Question1',  5, 4],
           ['Question2',  4, 5],
           ['Question3',  3, 2],
           ['Question4',  5, 1]
           ]);

var options = {
    title: 'Company Performance',
    hAxis: {title: 'Questions', titleTextStyle: {color: 'red'}},
    vAxis: {title: '1 = POOR, 5 = EXCELLENT', titleTextStyle: {color: '#FF0000'}, maxValue:'5', minValue:'1'},
    tooltip:{trigger: 'hover'}};

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

关于javascript - Google Charts 外部 Javascript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219015/

相关文章:

jquery - 只有 x 轴上的整数在带有 angularjs 的流程图中刻度

javascript - 如何仅在延迟的 Javascript 外部文件加载后才执行 Javascript?

c++ - 在cpp文件中列出外部变量

jquery从外部页面循环链接到特定幻灯片?

javascript - 如何从 Javascript 中的 Date() 函数获取斯里兰卡时间缩写

javascript - ie8 上的 Jquery 脚本错误

database - 从充满数字的数据库创建图形或图表的软件包?

charts - 如何向 c3.js 图表添加标题

javascript - 将图像放在彼此之上时,CSS/JS 顶部和左侧会变形

javascript - 移动浏览器中的视频背景