Refactoring to sorting and bugfix to averaging

Bug fixes and updates to support charts I wanted to get working on the front page.

There will be another task to rework the ordering function used, as I want to test some improvements, but I will do that under another task as sorting by value is working at the moment (I would like to add completely custom sorting by key as well).

Post aggregation calculations have been updating in Grouping as well, to fix a bug in the old rolling mean calculation and implement it post aggregation instead for simplicity.  When grouping, the simplicity of this approach outweighs any minor performance benefit of not looping at the end.