2

我试图让我的谷歌图表在他们点击标签链接后显示,但谷歌图表不显示。

我在里面包含了谷歌图表代码ChartlyDaily。当它在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> 
4

2 回答 2

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

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

于 2013-08-25T00:29:42.780 回答
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});
于 2013-08-25T05:01:09.067 回答