Can I draw charts with Google Visualization API from data that I have
in SQL database?
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.
var data = new google.visualization.DataTable();
length = stateArray.length
data.addRows(length);
data.addColumn('string', 'State');
data.addColumn('number', 'State Rank');
for( var i=0; i<length; i++)
{
data.setValue(i, 0, stateArray[i]);
data.setValue(i, 1, stateArray[i][stateRank]);
}
Thats how I do it. There are probably easier ways to do it.
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-visualization-api/-/oZYf8A46uUIJ.
It cannot read directly from the database - you have to use PHP or other server-side scripting to pull the data out of the database and put it in a format the API can understand. The easiest way using PHP is to query your database and output the data directly inside the javascript function (in place of the hard-coded data).
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-visualization-api/-/3k_CWKNeIY0J.
I created users.php file and added the following code. DON'T forget to add your own sql username/password and create a Users table in the database!!:
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Created Date');
data.addColumn('string', 'First Name');
data.addColumn('string', 'Last Name');
data.addColumn('string', 'Country');
data.addColumn('string', 'Gender');
data.addColumn('string', 'Birth Year-');
data.addColumn('string', 'Month-');
data.addColumn('string', 'Day');
data.addColumn('string', 'Email Encrypted');
data.addColumn('string', 'Password Encrypted');
data.addRows([
<?php
$con = mysql_connect("blabla","blabla","passwordblabla");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("robotfriend_org", $con);
$result = mysql_query("SELECT *
FROM Users
Order by fname ASC");
while($row = mysql_fetch_array($result))
{
echo "['" . $row['created_date'] . "',
'" . $row['fname'] . "',
'" . $row['lname'] . "',
'" . $row['country'] . "',
'" . $row['gender'] . "',
'" . $row['year'] . "',
'" . $row['month'] . "',
'" . $row['day'] . "',
'" . $row['email_encrypted'] . "',
'" . $row['password'] . "'],";
}
mysql_close($con);
echo "['', '', '', '', '', '', '', '', '', '']"; // MUST add NO "," in the end (after password encrypted and]!!
?>
]);
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true});
}
</script>
</head>
<body>
<div id='table_div'></div>
</body>
</html>
here is the source, thanks.
<html><head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Location', 'Timestamp', 'Count'], ]); // parse the data table for a list of locations var locations = google.visualization.data.group(data, [0], []); // build an array of data column definitions var columns = [1]; for (var i = 0; i < locations.getNumberOfRows(); i++) { var loc = locations.getValue(i, 0); columns.push({ label: loc, type: 'number', calc: function (dt, row) { // include data in this column only if the location matches return (dt.getValue(row, 0) == loc) ? dt.getValue(row, 2) : null; } }); } // create a DataView based on the DataTable to get the correct snapshot of the data for the chart var view = new google.visualization.DataView(data); // set the columns in the view to the columns we constructed above view.setColumns(columns);
var options = {
title: 'Selfcheck Stats'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
// draw the chart using the DataView instead of the DataTable
chart.draw(view, options);
}
</script> </head> <body> <div id="chart_div" style="width: 900px; height: 500px;"></div> </body> </html>