0

我一直致力于用 JavaScript 设计一个计算器,将数据提供给谷歌柱形图。基本思想是计算的结果决定了图表中某一列的高度。它在 Firefox 和 Chrome 中以应有的方式工作,但在 Internet Explorer 中则不然,图表根本不显示。我已经尝试解决这个问题两天了,但没有成功,所以如果有人可以帮助n00b,那将非常感激。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <SCRIPT LANGUAGE="JavaScript">
function CalculateSum(Atext, Btext, Ctext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
form.Answer.value = A * B * C;
}
function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.Answer.value = "";
}
</script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work', Number(a.value)],
          ['Eat', 2],
          ['Commute', 2],
          ['Watch TV', 2],
          ['Sleep', 7]
        ]);

        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization')).
            draw(data, {title:"So, how was your day?"});
      }


      google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <FORM NAME="Calculator" METHOD="post">
<P>Population (billions): <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P>
<P>Affluence (PPP US$): <INPUT TYPE=TEXT NAME="input_B" SIZE=10></P>
<p>Technology (tonnes per $): <Input TYPE=TEXT NAME="input_C" SIZE=10></p>
<P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form); drawVisualization()"></P>
<P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P>
<P>Impact (billions of tonnes): <INPUT TYPE=TEXT NAME="Answer" SIZE=12 id="a"></P>
</FORM>
    <div id="visualization" style="width: 600px; height: 400px;"></div>
  </body>
</html>
4

1 回答 1

0

['Work', Number(a.value)],应该['Work', Number(document.getElementById("a").value)],

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <FORM NAME="Calculator" METHOD="post">
<P>Population (billions): <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P>
<P>Affluence (PPP US$): <INPUT TYPE=TEXT NAME="input_B" SIZE=10></P>
<p>Technology (tonnes per $): <Input TYPE=TEXT NAME="input_C" SIZE=10></p>
<P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form); drawVisualization()"></P>
<P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P>
<P>Impact (billions of tonnes): <INPUT TYPE=TEXT NAME="Answer" SIZE=12 id="a"></P>
</FORM>
    <div id="visualization" style="width: 600px; height: 400px;"></div>


    <script type="text/javascript">
function CalculateSum(Atext, Btext, Ctext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
form.Answer.value = A * B * C;
}
function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.Answer.value = "";
}
</script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work', Number(document.getElementById("a").value)],
          ['Eat', 2],
          ['Commute', 2],
          ['Watch TV', 2],
          ['Sleep', 7]
        ]);

        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization')).
            draw(data, {title:"So, how was your day?"});
      }


      google.setOnLoadCallback(drawVisualization);
    </script>
  </body>
</html>
于 2013-01-13T11:41:26.503 回答