简体   繁体   English

Google Charts API和jQuery

[英]Google Charts API and jQuery

I'm beginning to use the Google Charts API and am seeing some curious aspects about its behavior. 我开始使用Google Charts API,并看到一些有关其行为的奇怪方面。 I first noticed that my sample chart wouldn't load when I placed the google chart code inside of the document.ready of jQuery. 我首先注意到将Google图表代码放在document.ready jQuery中时不会加载示例图表。 So then I played around and did the following: 因此,我玩耍并执行以下操作:

  google.load('visualization', '1.0', { 'packages': ['corechart'] });

        //jquery part
            $(document).ready(function () {
                google.setOnLoadCallback(drawStuff);
                function drawStuff() {
                    var data = new google.visualization.DataTable();
                    data.addColumn('string', 'Topping');
                    data.addColumn('number', 'Slices');
                    data.addRows([
                    ['Mushrooms', 3],
                    ['Onion', 1],
                    ['Olives', 1],
                    ['Zucchini', 3],
                    ['Pepperoni', 2]
                ]);
                    //set the chart options
                    var options = {
                        title: 'Pizza Consumed',
                        width: 400,
                        height: 500
                    };
                    //instantiate and draw our chart, passing in the options
                    var chart = new google.visualization.ColumnChart(document.querySelector('#chart'));
                    chart.draw(data, options);
                    $('#chart').fadeIn();
                };
            });

This works as I hoped it would but when I open up developer tools I see that the GET request for google.com/jsapi apparently failed (indicated by the red X in Chrome dev tools). 这可以按我希望的那样工作,但是当我打开开发人员工具时,我发现google.com/jsapi的GET请求显然失败了(由Chrome开发工具中的红色X表示)。 The chart certainly comes up on the page and works as I would expect it would, however. 该图表肯定出现在页面上,并且可以按我期望的那样工作。 Why does this current iteration work whereas placing everything inside of the document.ready does not? 为什么当前的迭代有效,而将所有内容都放置在document.ready中却不起作用? If I wanted to use Google charts on a project along with jQuery would this be a viable method of doing so? 如果我想与jQuery一起在项目上使用Google图表,这是否可行?

There is no need to put the chart code inside a document ready call - in fact, you are more likely to encounter problems doing that than you are if you just leave it on its own: 无需将图表代码放入文档就绪调用中-实际上,与仅将其单独放置相比,这样做更有可能遇到问题:

function drawStuff() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
        ['Mushrooms', 3],
        ['Onion', 1],
        ['Olives', 1],
        ['Zucchini', 3],
        ['Pepperoni', 2]
    ]);
    //set the chart options
    var options = {
        title: 'Pizza Consumed',
        width: 400,
        height: 500
    };
    //instantiate and draw our chart, passing in the options
    var chart = new google.visualization.ColumnChart(document.querySelector('#chart'));
    chart.draw(data, options);
}
google.load('visualization', '1.0', { 'packages': ['corechart'], callback: drawStuff });

$(document).ready(function () {
    // do stuff on "ready" event
});

On another note, I see you are calling $('#chart').fadeIn(); 另一方面,我看到您正在调用$('#chart').fadeIn(); after drawing the chart. 绘制图表后。 I presume that this means that chart is hidden prior to drawing, which can cause problems in some browsers. 我认为这意味着chart在绘制之前是隐藏的,这可能在某些浏览器中引起问题。 The recommended course of action is to unhide the div prior to drawing and hide it again immediately after drawing (from a "ready" event handler). 推荐的操作过程是在绘制之前取消隐藏div,并在绘制之后立即将其再次隐藏(从“就绪”事件处理程序中)。 You can then call the fadeIn effect: 然后可以调用fadeIn效果:

$('#chart').show();
var chart = new google.visualization.ColumnChart(document.querySelector('#chart'));
google.visualization.events.addListener(chart, 'ready', function () {
    $('#chart').hide();
    $('#chart').fadeIn();
});
chart.draw(data, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM