Graphing mysql data with PHP + jQuery
Posted in visualization on March 24th, 2009 by Michael Ewens – 5 CommentsFor our paper on apartment rental discrimination, I maintain a database of apartment rents across 30 US cities for 1 bedrooms or studios. Once it is in mysql (easy), I can access it with PHP and send the data to a javascript file to make simple interactive graphs.
Average 1 Bedroom and Studio Rents in Major Cities (x-axis is week number since Feb 15th)
Here is how it was done:
1. PHP script to look through city names (I assume the database exists)
2. SQL statement:
SELECT WEEKOFYEAR( DATE ) AS woy, AVG( rent ) , COUNT( * ) FROM city_counts WHERE city = CITYNAME GROUP BY woy
3. For each city create a simple javascript-friendly string in PHP that looks like this:
{"LA": { label: "LA", data: [[8,1194.9206349206],[9,1261.7650969529]]},
4. Put it all together with Flot (this example) + jQuery.
I know very little (if any) Javascript, but got this up in 15 minutes.