Organize CoreData items for use in bar chart

Hello,

I am building an app where users can log their water consumption. I want to create a bar chart which displays how much water they are drinking each hour in the current day (from 12 a.m. to 11:59 p.m.). I have a CoreData entity which stores the quantity of water consumed and the time of consumption. The chart will be hours on the x-axis and ounces on the y-axis.

To get the desired bar chart, I plan on making a ForEach loop to create 24 vertical lines to represent the water intake each hour, then adjust the height of those lines based on the quantity. In order to do this, I need an array of 24 items, each of which stores the quantity of water and the hour of the day. Something like this:

struct waterIntake: Identifiable {
     var id = UUID()
     var quantity: CGFloat
     var hour: Int
}

My question is: how do I get from a CoreData entity with the quantity of water and time of consumption to this array with hour numbers? Or, can I use the CoreData entity directly in the ForEach loop? Please remember I need some way to add the quantity of all water consumed in a given hour and group it into one quantity per one hour.

Organize CoreData items for use in bar chart
 
 
Q