Nav links

Thursday, 25 November 2010

Exciting Weather Chart revamp

The Chaucery Weather Chart has had the biggest refresh of its long and productive career. Out goes the laboriously hand-crafted browser-specific Javascript, and in comes the browser-agnostic fully-interactive chart of the future. Never has seeing the temperature and rainfall for the previous 72 hours been so exciting!

Most of the enhancements are visual and easy to spot. Some bonus features that are not immediately obvious are that you can click on a series in the legend to hide or display it, and you can zoom in by dragging the mouse to create a box in the area of interest.

Tech Talk

The old weather chart was mostly done in PHP, with some light Javascript icing on top. This rewrite was initially aimed at replacing all of the server-side PHP with browser-based Javascript. This seemed feasible because the source weather data is provided by BOM in JSON format (not JSONP). However, I quickly discovered that browsers cannot retrieve JSON data from third-party servers. This meant that in this case they would only accept JSON data from the Chaucery site itself. Luckily this proved to be a temporary setback.

What the browser wants the browser shall get, so I wrote a simple caching proxy in PHP to fetch JSON data from BOM when requested, and pass it unchanged to the browser. This meant I could still have all of the interesting code in Javascript, and add the bonus of caching popular requests for faster retrieval.

The final and most important part of the new site is the use of the great Highcharts charting library. This does all the heavy lifting of producing great-looking interactive charts. All I have to do is lightly process the JSON data to pass to it.

The old process of viewing a weather chart was:
  1. browser asks Chaucery for chart
  2. Chaucery asks BOM for data
  3. Chaucery processes data into an image
  4. Chaucery sends image back to browser

The new process is:
  1. browser asks Chaucery for chart
  2. Chaucery tells browser where data is and how to draw with it
  3. browser asks Chaucery for data (this was the bit I hoped could be "browser asks BOM for data)
  4. Chaucery asks BOM for data
  5. Chaucery sends data back to browser
  6. browser processes data into an image
Because the browser gets the data it opens up much more scope for interactivity. Although it is a longer process the structure allows easier expansion in the future. I hope you like it.