2

我正在尝试在 jboss seam 2 项目中使用谷歌可视化 api。

我创建了一个简单的示例,它实际上取自Google Quick Start page

<html>
   <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.0', {'packages':['corechart']});

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

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

       // Create the data table.
       var data = new google.visualization.DataTable();
       data.addColumn('string', 'Topping');
       data.addColumn('number', 'Slices');
      data.addRows([
        ['Mushrooms', 3],
        ['Onions', 1],
        ['Olives', 1],
        ['Zucchini', 1],
        ['Pepperoni', 2]
      ]);

       // Set chart options
       var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300};

       // Instantiate and draw our chart, passing in some options.
       var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>
</head>
<body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
</body>
</html>

当我在网络浏览器中将它作为文件打开时,它运行良好。但是当我将它放到我的 Jboss Seam 2 项目中并在 Web 浏览器中打开时,它会在红色背景上生成错误消息“b[c] is undefined”。

这是我在浏览器中打开页面源代码时看到的内容:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>

     <script src="/MCMS/a4j/g/3_3_3.Final/org/ajax4jsf/framework.pack.js" type="text/javascript"></script>
     <script src="/MCMS/a4j/g/3_3_3.Final/org/richfaces/ui.pack.js" type="text/javascript"></script>
     <link class="component" href="/MCMS/a4j/s/3_3_3.Final/org/richfaces/skin.xcss/DATB/eAGTKJ8eErp8hjQADcsDKg__" rel="stylesheet" type="text/css" />
     <script type="text/javascript" src="https://www.google.com/jsapi"></script>
     <script type="text/javascript">
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});

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

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

          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'Slices');
          data.addRows([
          ['Mushrooms', 3],
          ['Onions', 1],
          ['Olives', 1],
          ['Zucchini', 1],
          ['Pepperoni', 2]
        ]);

        // Set chart options
        var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
 </head>

 <body>

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

如您所见,Jboss Seam 为 a4j 添加了一些其他脚本 src。

a4j javascript 是否可能与谷歌可视化 api javascript 冲突?

有没有办法解决这个问题?

4

1 回答 1

3

终于在这里找到了解决方案

谷歌图表错误:无法读取未定义的属性“长度”;谷歌图表中的调试错误

和这里

http://code.google.com/p/google-visualization-api-issues/issues/detail?id=501

在脚本的开头添加以下 hack:

Array.prototype.reduce = undefined;

或更优雅地:

Array.prototype.reduce=function(fun){var len=this.length&#62;&#62;&#62;0;if(typeof fun!="function")throw new TypeError;if(len==0&#38;&#38;arguments.length==1)throw new TypeError;var i=0;if(arguments.length&#62;=2)var rv=arguments[1];else{do{if(i in this){var rv=this[i++];break}if(++i&#62;=len)throw new TypeError;}while(true)}for(;i&#60;len;i++)if(i in this)rv=fun.call(undefined,rv,this[i],i,this);return rv};

解决问题!

于 2013-01-17T10:39:40.537 回答