为什么在功能内部没有调用谷歌图表功能?

时间:2013-08-25 00:24:11

标签: javascript jquery google-visualization

我在点击标签链接时试图制作我的谷歌图表,但谷歌图表没有显示。

我在ChartlyDaily中添加了Google图表代码。当它在ChartDaily函数之外时它可以正常工作,但在它里面却没有。

     <script type="text/javascript" src="https://www.google.com/jsapi"></script>
                <script type="text/javascript">
                google.load("visualization", "1", {packages:["corechart"]});
                function ChartDaily(){
                  google.setOnLoadCallback(drawChartDaily);
                  function drawChartDaily() {
                   var chart_data = ['3',7];
                   alert(chart_data);
                   var data = google.visualization.arrayToDataTable([
                    ['Scoring Activity', 'Points given'], chart_data]);

                   var options = {
                    title: 'Semis overall scoring activity'
                  };

                  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                  chart.draw(data, options);
                }
              }
              jQuery(document).ready(function() { 
                $('#tabs > div').hide();
                $('#tabs div:first').fadeIn('slow');
                $('#tabs ul li:first').addClass('active');
                $('#tabs ul li a').click(function(){
                    $('#tabs ul li.active').removeClass('active');  // <== Only what you need
                    $(this).parent().addClass('active');
                    var selectedTab=$(this).attr('href');
                    $('#tabs > div').fadeOut('slow', function() {       // <== Use a callback
                        $(selectedTab).delay(10).fadeIn('fast');          // <== add a delay
                      });        
                    return false;
                  });
                $('#menu_tab1').click(function(){
                  ChartDaily(); // supposed to show the chart
                }); 

                $('#menu_tab2').click(function(){
                 alert('tab-2 got clicked!!');

               }); 

                $('#menu_tab3').click(function(){
                 alert('tab-3 got clicked!!');

               });

                $('#menu_tab4').click(function(){
                 alert('tab-4 got clicked!!');

               });

              });

</script>

  <div id="chart_div" style=" margin-left:-280px;width: 800px; height: 500px;"></div> 

2 个答案:

答案 0 :(得分:0)

这是重新开始的事情。

function GoogleChart() {
    this.chart_data = ['3',7];
    this.data_table = google.visualization.arrayToDataTable([['Scoring Activity', 'Points given'], chart_data]);
    this.options = {
        title: 'Semis overall scoring activity'
    };

    this.chart = new google.visualization.PieChart(document.getElementById('chart_div'));    
}

GoogleChart.prototype.drawChartDaily = function () {
    this.chart.draw(this.data_table, this.options);
};

$(function () {
    function myOnLoadCallback() {
        var googleChart = new GoogleChart();

        $('#menu_tab1').click(function(){
            googleChart.drawChartDaily();
        });
    }

    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(myOnLoadCallback);
});

编辑:更改为未经测试的示例。

答案 1 :(得分:0)

Google加载器在其他函数内部调用问题,因此最好让它自行运行。尝试这样的事情:

function ChartDaily () {
    var chart_data = ['3',7];
    var data = google.visualization.arrayToDataTable([
        ['Scoring Activity', 'Points given'],
        chart_data
    ]);

    var options = {
        title: 'Semis overall scoring activity'
    };

    var chart = new oogle.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}
function init () {
    $('#menu_tab1').click(function(){
        ChartDaily();
    });
}
google.load("visualization", "1", {packages:["corechart"], callback: init});