Refactored javascript for charts