本文介绍了Google图表不在Intellij上加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用谷歌图表做一个饼图。当我把这个代码放到一个HTML文件,并在自己的正常工作运行。



这是什么样子:





但是,当我在Intellij的项目中运行它时,什么都没有出现。



下面是我的代码:

 < DIV> 

<! - script type =text / javascriptsrc =views / jsapi.js>< / script - >
< script type =text / javascriptsrc =https://www.google.com/jsapi>< / script>

< script type =text / javascript>
console.log(1);
//加载Visualization API和饼图包。
google.load('visualization','1.1',{'packages':['corechart']});
console.log(2);

//设置回调以在加载Google Visualization API时运行。
google.setOnLoadCallback(drawChart);
console.log(3);

//创建并填充数据表的回调,
//实例化饼图,传入数据并
//绘制它。
函数drawChart(){
console.log(4);

//创建数据表。
var data = new google.visualization.DataTable();
data.addColumn('string','Topping');
data.addColumn('number','Slices');
data.addRows([
['Mushrooms',3],
['Onions',1],
['Olives',1],
[ '西葫芦',1],
['Pepperoni',2]
]);
console.log(5);

//设置图表选项
var options = {'title':'我昨晚吃了多少比萨',
'width':400,
'高度':300};

//实例化并绘制我们的图表,并传入一些选项。
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data,options);
console.log(6);

}
console.log(7);

< / script>
< div id =content>
< div id =chart_div>< / div>
< / div>
< / div>

为什么会发生这种情况?

解决方案

我通过修改这行代码来实现它:

  google.load('可视化','1',{packages:['corechart']}); 

要这样:

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

请参阅Zanona的答案以获得进一步解释:


I am trying to make a pie chart with Google Charts. When I put this code into an html file and run it on its own it works fine.

This is what it looks like:

However, when I run it within my project in Intellij nothing appears.

Here is my code:

<div>

        <!--Load the AJAX API-->
        <!--script type="text/javascript" src="views/jsapi.js"></script-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <script type="text/javascript">
console.log("1");
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.1', {'packages':['corechart']});
console.log("2");

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);
console.log("3");

    // Callback that creates and populates a data table,
    // instantiates the pie chart, passes in the data and
    // draws it.
    function drawChart() {
        console.log("4");

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1],
            ['Zucchini', 1],
            ['Pepperoni', 2]
        ]);
        console.log("5");

        // Set chart options
        var options = {'title':'How Much Pizza I Ate Last Night',
            'width':400,
            'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
        console.log("6");

    }
console.log("7");

</script>
    <!--Div that will hold the pie chart-->
    <div id="content">
        <div id="chart_div"></div>
    </div>
</div>

Why is this happening?

解决方案

I got it to work by modifying this line of code:

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

To this:

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

See Zanona's answer here for further explanation: google.load causes empty dom/screen

这篇关于Google图表不在Intellij上加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:13