0

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:

enter image description here

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?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • If you open the developer tools are any errors thrown in the console? – Charles Clayton Jul 06 '15 at 17:36
  • No there are no errors. – Wyetro Jul 06 '15 at 17:36
  • Put in some `console.log`s at different places in the javascript. Does it run the entire function? – Charles Clayton Jul 06 '15 at 17:38
  • I added 7 console.log's (see above) and none of them printed. – Wyetro Jul 06 '15 at 17:41
  • Then when you say you run it with your project in Intellij, I just don't think the website that pops up is the same as your code. If you looks at the source or inspect element, is your code there? – Charles Clayton Jul 06 '15 at 17:44
  • It definitely is, given that the hundreds of lines of code in that file run. – Wyetro Jul 06 '15 at 17:45
  • Also, the div where the chart appears (as empty space) is the right size. – Wyetro Jul 06 '15 at 17:46
  • If you replace your current script import and `google.load` line with this: `` , what happens? – Charles Clayton Jul 06 '15 at 17:48
  • Seems like a pretty straightforward problem. You have a section of code that's apparently in 100s of lines of code that's not running. If you can't even see `console.log("1")` then it doesn't seem like it has anything to do with google charts. Replace the entire thing with `alert("hello?")` and start removing stuff. – Charles Clayton Jul 06 '15 at 18:16
  • Do you have the javascript in a separate file in IntelliJ? More importantly, is your javascript wrapped in a $(document).ready() block? – Jonathan Michalik Jul 06 '15 at 18:55
  • It's in my main html file. Not rapped in a $(document).ready() block – Wyetro Jul 06 '15 at 19:03
  • 1
    Debunks that thought then...the `setOnLoadCallback` function has scoping conflicts with `$(document).ready()` so the callback never actually fires. If IntelliJ is trying to be smart and wrap your javascript under the covers, that would certainly act exactly as you describe. As a test, you can try calling the `drawChart()` function explicitly after the `google.load()` function completes – Jonathan Michalik Jul 06 '15 at 19:06
  • Manually calling drawChart() didn't do anything, especially given that console.logs aren't being called either. – Wyetro Jul 06 '15 at 20:07

1 Answers1

0

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

Community
  • 1
  • 1
Wyetro
  • 8,439
  • 9
  • 46
  • 64