【问题标题】:google visualization + jboss seam 2谷歌可视化 + jboss 接缝 2
【发布时间】:2013-01-16 16:36:28
【问题描述】:

我正在尝试在 jboss seam 2 项目中使用 google 可视化 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 是否可能与 google 可视化 api javascript 冲突?

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

【问题讨论】:

  • 如果您删除 google 代码并仅添加一些标准 HTML,它是否可以正常工作? (即可能是其他软件包之一中的错误?)
  • 是的。它无需谷歌代码即可工作。其他包中不可能有错误,因为它已经是一个长期存在的项目,Jboss Seam(+丰富的面孔)的所有功能都可以很好地工作。现在我们想在里面使用谷歌可视化来显示一些图表。
  • 我还发现一个人在 JSF webapp groups.google.com/forum/#!topic/google-visualization-api/… 中也有类似的问题

标签: google-visualization jboss-seam


【解决方案1】:

终于在这里找到了解决办法

Google charts error:Cannot read property 'length' of undefined; Debugging error in Google Charts

这里

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};

解决问题!

【讨论】:

  • 过去结合其他 JS 库可能应用到 Google Visualization API 的问题现在可能会有所不同。无论如何,强制 Array.prototype.reduce 未定义的解决方法必然会导致问题,实际上,您的代码现在很可能会失败。请不要这样做。我不确定这里建议的 reduce 的定义。
猜你喜欢
  • 2013-07-08
  • 2013-12-13
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多