Refactor pie and bar graph code