Hey everyone,
I've been working on a jQuery Mobile app and trying to integrate Google Charts into it. However, I'm facing an issue where the chart isn't displaying when I move the code to my main.js file. It works fine when it's all in the head section of the HTML.
Here is the basic structure of the HTML page I'm using:
<!DOCTYPE html>
<html>
<head>
<title>Manufacturing Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./css/grey-orange.min.css" />
<link rel="stylesheet" href="./css/jquery.mobile.custom.structure.min" />
<link rel="stylesheet" href="./fonts/font-awesome/css/font-awesome.min.css"/>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/jquery.mobile.custom.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["piechart", "corechart", "geomap"]});
</script>
</head>
<body>
<div data-role="page" data-theme="a" id="main-project-page">
<div data-role="panel" class="project-menu" data-position="left" data-theme="c">
</div><!-- /panel -->
<div data-role="header" data-position="fixed">
</div><!-- /header -->
<div data-role="content">
<h3 id="project-name"></h3>
<div id="project-overall-chart"></div>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
</div><!-- /footer -->
</div><!-- /page -->
<script src="./js/main.js"></script>
</body>
</html>
Currently, I'm using a placeholder from Google's API Documentation as I call an API to fetch project information from a database. This is how I am approaching it...
//Global variables
var request;
var project_id = "null";
var equipment_id = "null";
//Main Project Page
$(document).on("pageinit", "#main-project-page", function() {
//Menu Panel slide effect
$('.menu-button').click(function(event) {
$('.project-menu').panel("open");
});
//Populate project view with current project...
populate_project_view();
});
function populate_project_view()
{
//Check if there is a project to retrieve
if (project_id != 'null')
{
//Construct the JSON
var json = new Object();
var info = new Object();
json.type = "info";
info.type = "project";
info.id = project_id;
json.info = info;
json = JSON.stringify(json);
//Make the request
request = getHTTPObject();
request.onreadystatechange = function () {
//If request object received response
if (request.readyState == 4)
{
var json = JSON.parse(request.responseText);
if (json.error == true)
{
alert('Error: ' + json.msg);
//Return to main screen
$.mobile.changePage('#main-page', 'slide', true, true);
}
else
{
//Populate the #main-project-page DOM with project object
var project = json.project;
//Populate Project's name
var name = document.createTextNode(project.name);
$('#project-name').append(name);
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// 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]
]);
// 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('project-overall-chart'));
chart.draw(data, options);
}
}
}
}
request.open("GET", "./php/api.php?package=" + json + '&qs=' + new Date().getTime(), true);
request.send(null);
}
}
/*
Supporting functions
*/
//Returns an HTTPObject
function getHTTPObject()
{
var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xhr = false;
}
}
}
return xhr;
}
I'm still learning about JavaScript's AJAX operations and its behavior in browsers. Any help or insight into this issue would be greatly appreciated!
Cheers, Nathan