
Lately I’ve wanted to do more with wordpress, and adding dynamic data seemed like a cool project. Although I’m still a few days from posting my ‘mortgage rates’ page to monsterhash.com, I thought I’d offer a glimpse into the technology behind it.
First I had to ingest the data and schedule the data. For the first task, I used a simple HTTP proxy for the XML data published on Freddie Mac’s website, something I covered in a previous lesson, How to set up an HTTP Proxy in PHP. I then posted the ingested data to a SQL table, which is a fairly trivial task covered in any MySQL tutorial. Next, I had to schedule that task (also covered in a previous lesson on chron jobs).
Now that the data is coming in predictably, it would be nice to display it graphically. I explored a number of solutions, including Flash and jQuery implementations. But for sheer simplicity and wide compatability, the Google Charts Tool won hands down.
In theory, this tool allows you to ping the Google servers with comma-delimited data, and then the server pings you back with a PNG image. In practice, both the data and the styling needed a little massaging to get the beautiful graph you see above.
First off, I wanted to grab the data from my server, but I only wanted to grab the most recent data. The problem here was that, when you pull the most recent rows from a database, it returns them in the order of newest to oldest. If I feed them into the Google API, it’ll read from left to right, causing the first point in the graph to be the most recent date, and the last point in the graph to be the oldest point before the cutoff. To reverse the order, I throw the data into two arrays, one for x variables and one for y variables, and then reverse sort them by key before passing them to the function that interacts with the Google server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $query = " SELECT `date` , `thirty_year` FROM `MORTGAGE_RATE` ORDER BY `date` DESC LIMIT 0 , 7 "; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $xArray[] = date("M-j", strtotime($row['date'])); $yArray[] = $row['thirty_year'] * 100; } mysql_close(); krsort($xArray); krsort($yArray); $url = getLineGraph($xArray,$yArray); |
So all I’m passing into the function are the x and y coordinates, which will make my code ultra-reusable. That function looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | function getLineGraph($xArray,$yArray){ $yNumSteps = 4; $yData = implode(",", $yArray); $yMax = max($yArray); $yMin = min($yArray); $yStep = ($yMax - $yMin) / $yNumSteps; $yBottom = $yMin - $yStep; $yTop = $yMax + $yStep; $yGridStep = round(100 / ($yNumSteps + 2),1); $xData = implode("|", $xArray); $chart_width = 585; $chart_height = 200; $line_color = "00a8ec"; $area_shading = "ebf5fc"; $grid_weight = 1; $url = "http://chart.apis.google.com/chart" . "?chtt=$chartTitle" . "&chd=t:$yData" . "&chl=$xData" . "&chs=$chart_width" . "x" . "$chart_height" . "&chco=$line_color" . "&chm=B,$area_shading,0,0,0" . "&chds=$yBottom,$yTop" . "&chxr=1,$yBottom,$yTop" . "&chg=10,$yGridStep,$grid_weight" . "&cht=lc" . "&chxt=x,y"; return $url; } |
With a modest effort, I’ve converted raw data into something visual. The next, and final step, will be to also set this process on a timer, so that the image gets re-cached to the same location every week. I’ll set this process to fire shortly after the data gets loaded into the table on Saturday, and then I’ll be able to dynamically track mortgage rates in my wordpress blog!
Economic Analysis
Economic Calendar
Real Clear Markets
Undervalued stocks
Value Cruncher
Wikinvest
2 Comments
Really great post, well written, concise and comprehensive. Thank you.
@Webkatalog
Thanks webkatalog! Just a quick note: if you’re using this code, I recently updated the getLineGraph function above by adding quotes around each part of the google api query. While the old code worked fine for outputting the graph on demand, these quotes are necessary to take spaces out of the query, so that the query also works with image caching mechanisms. Sorry I didn’t catch this sooner, but the fresh code (above) should work fine for this purpose.
One Trackback
[...] year, I wrote a piece on how to chart financial data with php. That was before I discovered the flexibility of google docs. A pure php / mysql solution with [...]