Restore functionality broken in pull request #245

It was necessary to leave the javascript as a function in the controller (albeit rewritten) as it is also needed by the charts module.