I’m using google charts and I need to grab data from a PHP file( or php inside of the HTML file? ) to feed data to the chart . So how would I do this , and make sure that I grab the data before drawing the chart .
I already went though figuring out how to grab data from a Mysql database with PHP
Here’s the code I need to put vars in
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// 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', 'Color');
data.addColumn('number', 'Votees ');
data.addRows([
['Value from Mysql', 3000],
['Value2 from Mysql', 13000],
]);
// Set chart options
var options = {'title':'Table Name From MySql',
'width':600,
'height':450};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
There are no variable as such in HTML – I think what you mean is that you want to be able to use PHP values with the code you use for the Google charts. I have not had much experience using Goggle charts but from what I can see it uses some JavaScript API’s. Perhaps what you are looking for is a way to get your values into JavaScript variables so that you can use them when dealing with your charts plugins.
This is exactly what PHP was made for – taking static static content and embedding scripts giving it dynamic content.
For example, you could have your PHP initializing a JavaScript variable –
Now you’ll have a JavaScript variable called
stackand its value will be ‘overflow’.The same technique can be used in any place – even in the middle of a variable definition. Lets take your code as an example –
…