在传递给 google.setOnLoadCallback() 的函数中使用参数;

发布于 2024-11-05 23:27:51 字数 2559 浏览 2 评论 0原文

我正在尝试使用 Google Visualization API 来显示从 MySQL 服务器收集的数据。我想使用 PHP 获取数据,然后将其传递到 javascript 函数调用中以创建图表。当我这样做时,我在将参数传递给传递给 google.setOnLoadCallback(); 的函数时遇到问题。我对网络编程还很陌生,所以请耐心等待。工作代码(几乎来自他们的文档)如下所示:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Year');
    data.addColumn('number', 'Sales');
    data.addRows(4);
    data.setValue(0, 0, '2004');
    data.setValue(0, 1, 1000);
    data.setValue(1, 0, '2005');
    data.setValue(1, 1, 1170);
    data.setValue(2, 0, '2006');
    data.setValue(2, 1, 660);
    data.setValue(3, 0, '2007');
    data.setValue(3, 1, 1000);

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

我试图首先看看是否可以在 drawChart() 函数之外设置数据并将其作为参数传递,如下所示:

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

  var data1 = new google.visualization.DataTable();
    data1.addColumn('string', 'Year');
    data1.addColumn('number', 'Sales');
    data1.addRows(4);
    data1.setValue(0, 0, '2004');
    data1.setValue(0, 1, 1000);
    data1.setValue(1, 0, '2005');
    data1.setValue(1, 1, 1170);
    data1.setValue(2, 0, '2006');
    data1.setValue(2, 1, 660);
    data1.setValue(3, 0, '2007');
    data1.setValue(3, 1, 1000);

  google.setOnLoadCallback(drawChart(data1));

  function drawChart(data) {
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

我不太确定为什么这是行不通的。使用从 PHP MySQL 调用动态收集的数据创建 DataTable 对象的最佳方法是什么?感谢您抽出时间。

I'm trying to use Google Visualization API to display data gathered from a MySQL server. I want to get the data using PHP and then pass it into the javascript function call to create a chart. When I do this, I'm having a problem passing parameters to the function passed to google.setOnLoadCallback();. I'm fairly new to web programming, so bear with me. The working code (pretty much from their docs) looks like this:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Year');
    data.addColumn('number', 'Sales');
    data.addRows(4);
    data.setValue(0, 0, '2004');
    data.setValue(0, 1, 1000);
    data.setValue(1, 0, '2005');
    data.setValue(1, 1, 1170);
    data.setValue(2, 0, '2006');
    data.setValue(2, 1, 660);
    data.setValue(3, 0, '2007');
    data.setValue(3, 1, 1000);

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

I was trying to first see if I could set up the data outside of the drawChart() function and pass it as a parameter as so:

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

  var data1 = new google.visualization.DataTable();
    data1.addColumn('string', 'Year');
    data1.addColumn('number', 'Sales');
    data1.addRows(4);
    data1.setValue(0, 0, '2004');
    data1.setValue(0, 1, 1000);
    data1.setValue(1, 0, '2005');
    data1.setValue(1, 1, 1170);
    data1.setValue(2, 0, '2006');
    data1.setValue(2, 1, 660);
    data1.setValue(3, 0, '2007');
    data1.setValue(3, 1, 1000);

  google.setOnLoadCallback(drawChart(data1));

  function drawChart(data) {
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

I'm not too sure why this doesn't work. What would be the best way to create the DataTable object using dynamically gathered data from a PHP MySQL call? Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-11-12 23:27:51

在第一个示例中,您将函数传递给回调,在第二个示例中,您调用该函数,然后将该调用的结果传递给回调。

尝试:

setOnLoadCallback(function(){ drawChart(data) })

in the first example you pass in the function to the callback, in the second example you call the function and then pass in the result of that call to the callback.

try:

setOnLoadCallback(function(){ drawChart(data) })
氛圍 2024-11-12 23:27:51

以下是使其工作的步骤:

  1. 将 google.load 和 google.setOnLoadCallback 放在单独的脚本标记中。
  2. 使用函数表达式。

这是工作代码:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(function() { drawChart(data1); });
</script>
<script type="text/javascript">

  var data1 = new google.visualization.DataTable();
    data1.addColumn('string', 'Year');
    data1.addColumn('number', 'Sales');
    data1.addRows(4);
    data1.setValue(0, 0, '2004');
    data1.setValue(0, 1, 1000);
    data1.setValue(1, 0, '2005');
    data1.setValue(1, 1, 1170);
    data1.setValue(2, 0, '2006');
    data1.setValue(2, 1, 660);
    data1.setValue(3, 0, '2007');
    data1.setValue(3, 1, 1000);

  function drawChart(data) {
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

The following are the steps to make it work:

  1. Place google.load and google.setOnLoadCallback in a separate script tag.
  2. Use a function expression.

Here is the working code:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(function() { drawChart(data1); });
</script>
<script type="text/javascript">

  var data1 = new google.visualization.DataTable();
    data1.addColumn('string', 'Year');
    data1.addColumn('number', 'Sales');
    data1.addRows(4);
    data1.setValue(0, 0, '2004');
    data1.setValue(0, 1, 1000);
    data1.setValue(1, 0, '2005');
    data1.setValue(1, 1, 1170);
    data1.setValue(2, 0, '2006');
    data1.setValue(2, 1, 660);
    data1.setValue(3, 0, '2007');
    data1.setValue(3, 1, 1000);

  function drawChart(data) {
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
                     });
  }
</script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文