refactored javascript for pie charts