Refactor code for creating pie chart data.