|
UserDocumentation
Documentation for using the MC_Google_Visualization library
IntroductionInstallation/RequirementsDownload the latest MC_Google_Visualization and extract the contents of the lib folder to your PHP include_path. You should now find a top-level MC directory which contains the parser and visualization compontents, and you're done! System Requirements
Handling Visualization Query RequestsSimple usage follows this pattern: <?php
require_once 'MC/Google/Visualization.php';
$db = new PDO('sqlite:example.db');
$vis = new MC_Google_Visualization($db, 'sqlite');
$vis->addEntity('some_table', array(
'fields' => array(
'col1' => array('field' => 'col1', 'type' => 'text'),
'col2' => array('field' => 'col2', 'type' => 'number')
)
));
$vis->handleRequest();
?>If you take the above code and save it to /vis.php on your web server, you can start making visualization requests against your database like so: <html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["piechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
//Tell Google Visualization where your script is
var query = new google.visualization.Query('/vis.php');
query.setQuery('select col1, col2 from some_table order by col2 desc');
query.send(function(result) {
if(result.isError()) {
alert(result.getDetailedMessage());
} else {
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(result.getDataTable(), {width: 400, height: 240});
}
});
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>Mapping Your DatabaseBefore you can start querying against your database, you must tell MC_Google_Visualization which fields and tables will be allowed be queried against. Any fields or tables in your database will not be exposed to visualization queries and will throw an error if attempted. EntitiesYou make your database tables and fields available for visualization queries by defining entities. An entity is one or more joined database tables that expose a set of fields that can be queried against. Fields can be simple database fields, the result of SQL functions, or use PHP callback functions to calculate the value. Entities are defined by call addEntity($name, $spec) with the name of the entity (to be used in the "from" clause of queries) and a spec array defining the table, fields, and joins that make up the entity. Entity Spec Array
Each field defined in the entity also has a spec array that defines the mapping for each field. Field Spec Array
More Information |
Sign in to add a comment
I see a problem with being able to write SQL queries directly from the javascript. How do you protect against malicious behavior?
Someone could easily make a request using your API to "delete from some_table", right?
You don't actually write SQL directly. The queries you write are in the Google Visualization Query Language, which is a variant on the syntax for SQL SELECT queries. So no modifying or destructive queries are allowed.
On top of that, you limit the fields that are accessible via javascript through defining entities. If you have any fields that should not be user-visible, simply leave them off of the entity definition. That will make them unavailable to the javascript API.
@hrovira: who allows a db user with insert, update and delete rights to be used in such a script anyway??
where do I have to put the "Group by" elements ?
Your API documentation appears to be down.
I am trying to use the function toDate(dateTime myDateTime) in a "select" clause. Could you give me an example of use ? Thank you