Approach for creating bar chart from data

Hello,

My app allows users to input each drink they have to track water consumption. These drinks are saved in CoreData. Each entry has the quantity of the drink and the time it was consumed.

Now, I want to create a bar graph that shows how much drink was consumed each hour in the current day. I want it to look exactly like the bar graph in the Apple Fitness app (attached), with 24 vertical lines representing each hour of the day of different height depending on how much drink was consumed in the given hour.

The issue is, I need to first fetch all the drinks the user consumed in the current day, then separate them into the different hours. I have no idea I should approach this. Out of desperation, I created 24 fetch requests for the different hours and now have 24 values of quantity (one for each hour). But this is extremely tedious and would just get near impossible to manage. The most efficient way to create a bar chart is to have a for each loop and draw lines for each hour, but I don't know how to get the data in a way that would let me do this.

Any guidance is appreciated! Thank you!

Have you found solutions for this question? I suggest using SectionedFetchRequest for the hour grouping and a Shape struct for creating the bar chart. If you need help, then respond to this comment. Regards, Michaela

Approach for creating bar chart from data
 
 
Q