douzhuo1858 2017-07-24 19:47
浏览 96
已采纳

未捕获的TypeError:无法读取未定义的属性“ChartWrapper”

I'm trying to integrate Google Charts with Wordpress, using Advanced Custom Fields input to populate the data. After just one iteration in the Javascript, it crashes, despite proper data formatting.

From other questions I gathered google.visualization must not be loading in time since it's undefined, but the authors of those questions didn't use setOnLoadCallback. I have, which should address that. What am I misunderstanding?

functions.php:

function create_chart(){  
    if( have_rows('chart') ){  
        wp_enqueue_script( 'google-charts', 'https://www.gstatic.com/charts/loader.js' );  

        $chart_data = array();  
        while( have_rows('chart') )  
            array_push( $chart_data, the_row(true) );  

        wp_register_script( 'chart-generator', get_stylesheet_directory_uri() . '/js/chart-generator.js' );  
        wp_enqueue_script( 'chart-generator', true );  
        wp_localize_script( 'chart-generator', 'chart_data', $chart_data);  
    }  
}  
add_action( 'wp_head', 'create_chart');

chart-generator.js

(function($){  
    google.charts.load('current', {packages: ['corechart']});  
    for(i=0; i<chart_data.length; i++){  

        // Reencodes objects as arrays  
        next_chart = chart_data[i];  
        for(j=0; j<next_chart.entry.length; j++)  
            next_chart.entry[j] = Object.values( next_chart.entry[j] );  
        console.log(next_chart);  

        google.charts.setOnLoadCallback(  
            drawPieChart(  
                next_chart.title,  
                next_chart.id,  
                next_chart.colors,  
                next_chart.entry));  
    }  
})(jQuery);  

function drawPieChart( title, id, colors, entries) {  
    // Appends to beginning of array  
    entries.unshift( ['title', 'quantity'] );  

    var wrapper = new google.visualization.ChartWrapper({              
        chartType: 'PieChart',  
        dataTable: google.visualization.arrayToDataTable(entries),  
        options: {  
            'title': title,  
            pieHole: 0.4,  
        },  
        containerId: id  
    });  
    wrapper.draw();  
}

HTML:

<div id="onechart" style="width:200px; height: 200px;"></div>  
<div id="twochart" style="width:200px; height: 200px;"></div>  
<div id="redchart" style="width:200px; height: 200px;"></div>  
<div id="vis_div" style="width:200px; height: 200px;"></div>  
<div id="burger" style="width:200px; height: 200px;"></div>
  • 写回答

1条回答 默认 最新

  • douxian7534 2017-07-24 19:59
    关注

    first, you need to load the 'controls' package,
    when using ChartWrapper or ControlWrapper

    google.charts.load('current', {packages: ['corechart', 'controls']});
    

    next, setOnLoadCallback takes a reference to a function,
    not the result of a function()

    it should be --> google.charts.setOnLoadCallback(drawPieChart);

    if you need to pass arguments to the callback,
    add an anonymous function, or something similar in between...

    google.charts.setOnLoadCallback(function () {
      drawPieChart(  
        next_chart.title,  
        next_chart.id,  
        next_chart.colors,  
        next_chart.entry
      );
    });  
    

    also, the callback only needs to be used once,
    after it fires the first time, you can drawn as many charts as needed

    recommend restructure as follows...

    google.charts.load('current', {packages: ['corechart', 'controls']});  
    google.charts.setOnLoadCallback(function () {
      for(i=0; i<chart_data.length; i++){  
          // Reencodes objects as arrays  
          next_chart = chart_data[i];  
          for(j=0; j<next_chart.entry.length; j++)  
              next_chart.entry[j] = Object.values( next_chart.entry[j] );  
          console.log(next_chart);  
    
              drawPieChart(  
                  next_chart.title,  
                  next_chart.id,  
                  next_chart.colors,  
                  next_chart.entry);  
      }  
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装