Javascript 对象没有getColumnType方法

Javascript 对象没有getColumnType方法,javascript,playframework,google-visualization,Javascript,Playframework,Google Visualization,在我的play应用程序中,我向视图发送一些JSON输出,以便显示google饼图。当它呈现js时,会出现问题 Object {cols: [{id: 'title', label: 'Title' , type: 'string'},{id: 'unitPrice', label: 'Unit Price', type: 'int'}],rows: [ has no method 'getColumnType'× 这条信息就是我尝试使用GoogleChartAPI绘制饼图所得到的全部信息。 这

在我的play应用程序中,我向视图发送一些JSON输出,以便显示google饼图。当它呈现js时,会出现问题

Object {cols: [{id: 'title', label: 'Title' , type: 'string'},{id: 'unitPrice', label: 'Unit Price', type: 'int'}],rows: [ has no method 'getColumnType'×
这条信息就是我尝试使用GoogleChartAPI绘制饼图所得到的全部信息。 这到底是什么意思?“没有方法'getColumnType'”

任何洞见都将受到极大的赞赏。我的代码,到目前为止:

<!DOCTYPE html>

<html>
<head>
<title>PayView</title>
   <head>    
        <script type="text/javascript">var pieChartData;</script>
        <script type="text/javascript">var loads = 0;</script>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>  
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("visualization", "1", {packages:["corechart"]});      
            google.setOnLoadCallback(function() {
                ++loads;
                if (loads == 2)
                drawVisualization();
            });

            function drawVisualization() {
                $('.log').html('pieChartData: ' + pieChartData);
                new google.visualization.PieChart(document.getElementById('visualization')).
                draw(pieChartData, {title:"Purchases"});
            }               

            $(document).ready(function() {
                $(function() {
                    pieChartData = new google.visualization.DataTable();
                    pieChartData.addColumn('string', 'Title');
                    pieChartData.addColumn('number', 'Unit Price');

                    var getJSon2 = $.ajax({
                        url: '@routes.Application.getJson()',
                        processData:false,
                        type: 'GET',
                        beforeSend:function(jqXHR, settings){                   
                            jqXHR.setRequestHeader("Content-Type", "application/json");
                        },
                        success: function(data, textStatus, jqXHR){                         
                            ++loads;
                            if (loads == 2)
                                drawVisualization();
                            process_items(data);   
                        },
                        error: function(jqXHR, textStatus, errorThrown){
                        },
                        complete: function(jqXHR,textStatus){                   
                        }   
                    );

                    var process_items = function(data){                     
                        pieChartData.addRows(data.length);
                        $.each(data, function(index, item) {
                            $("#purchases").append("<li>" + item.name + "</li>");
                            pieChartData.setCell(index, 0, item.title);
                            pieChartData.setCell(index, 1, item.unitPrice); 
                        });
                    });
                });         
            };  
        });     
        </script>       
    </head>
    <body>
        <div class="log"></div>
        <div id="purchases"></div>
        <div id="visualization" style="width: 500px; height: 300px;"></div>
    </body>
</html>

支付视图
var数据;
无功负荷=0;
load(“可视化”、“1”、{packages:[“corechart”]});
setOnLoadCallback(函数(){
++负载;
如果(负载==2)
绘图可视化();
});
函数drawVisualization(){
$('.log').html('pieChartData:'+pieChartData');
新的google.visualization.PieChart(document.getElementById('visualization'))。
绘制(pieChartData,{标题:“采购”});
}               
$(文档).ready(函数(){
$(函数(){
pieChartData=newGoogle.visualization.DataTable();
addColumn('string','Title');
pieChartData.addColumn('编号','单价');
var getJSon2=$.ajax({
url:“@routes.Application.getJson()”,
processData:false,
键入:“GET”,
beforeSend:函数(jqXHR,设置){
setRequestHeader(“内容类型”、“应用程序/json”);
},
成功:函数(数据、文本状态、jqXHR){
++负载;
如果(负载==2)
绘图可视化();
处理项目(数据);
},
错误:函数(jqXHR、textStatus、errorshown){
},
完成:函数(jqXHR,textStatus){
}   
);
var进程_项=函数(数据){
pieChartData.addRows(data.length);
$。每个(数据、功能(索引、项目){
$(“#采购”)。追加(“
  • ”+item.name+”
  • ”; pieChartData.setCell(索引,0,项.标题); pieChartData.setCell(指数,1,项目.单价); }); }); }); }; });
    您试图在调用完成之前使用根据ajax调用结果生成的“googleData”字符串

    您将不得不装配一些东西,等待google库加载和ajax调用的完成。可能看起来像这样:

            var loads = 0;
            google.load("visualization", "1", {packages:["corechart"]});      
            google.setOnLoadCallback(function() {
              ++loads;
              if (loads == 2)
                drawVisualization();
            });
    
        // then in your "ready" handler:
    
    
                success: function(data, textStatus, jqXHR){
                    process_items(data);
                    ++loads:
                    if (loads == 2)
                      drawVisualization();
                },
    

    当ajax调用完成和google任务完成时,这种(不雅观的)设置会增加一个计数器。两个处理程序都会检查它们是否是最后一个完成的处理程序,如果是,则调用“draw”函数。

    您还可以为该函数提供一个参数

    function drawChart(draw)
    {
        if(draw > 0)
        {
          var data=....;
          var options = ....;
          var chart = ....;
          chart.draw(data,options)
        }
    }
    

    在ajax中调用这个函数,给出一个类似于
    drawChart(1)

    Spot-on,Pointy的值。问题是你提到的延迟。如果我从firebug运行JS时,断点会暂时停止Execution。。。然后完美地绘制出饼图。我已经插入了你的代码,但运气不好。我已经根据你的建议编辑了问题中的代码。我做得对吗?顺便问一下,为什么不雅观?如果它成功了。。。在这种情况下有什么最佳实践吗?jQuery中有一个“延迟”机制,这会更整洁一些。您可以将GoogleAPI调用封装在其中,然后使用jQuery机制将两者结合起来。我会重读这个问题,看看是否能找到问题。@Pointy我在我的帖子中也有类似的问题。也许你能给我一些帮助。我已尝试实施您在此处建议的修复,但似乎无法将其应用于我的问题。非常感谢您的帮助。确保您声明“加载”为全局-只需将
    var加载=0在顶部,在谷歌的东西之前。