2013-01-16 44 views
2

我想在jboss縫2項目中使用谷歌可視化API。谷歌可視化+ jboss縫2

我已經創建了一個簡單的例子,它實際上取自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項目中並在網絡瀏覽器中打開時,它會在紅色背景上生成一條錯誤消息「b [c]未定義」。

在這裏,我看到了什麼,當我在瀏覽器中打開網頁的源文件:

<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。

a4j JavaScript可能與谷歌可視化api javascript衝突嗎?

是否有任何想法如何解決這個問題?

+0

如果你刪除了谷歌代碼,只是添加了一些標準的HTML,它是否工作沒有錯誤? (也許是其他包裝中的錯誤?) – jmac

+0

是的。它工作沒有谷歌代碼。其他軟件包中不會出現錯誤,因爲它已經是一個長期生活的項目,其中Jboss Seam(+豐富人臉)的所有功能都可以很好地工作。現在我們想用谷歌可視化來顯示一些圖表。 –

+0

我還發現一個人在JSF webapp中有類似的問題https://groups.google.com/forum/#!topic/google-visualization-api/bRAXXqW06DA –

回答

3

終於找到了解決辦法在這裏

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

添加下面的技巧在腳本的開頭:

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

解決問題!

+0

可能適用於Google Visualization API的問題過去與其他JS庫的組合現在可能會有所不同。無論如何,迫使Array.prototype.reduce未定義的解決方法肯定會導致問題,實際上,您的代碼現在很可能會失敗。請不要這樣做。我不確定這裏提出的reduce的定義。 – dlaliberte