0👍
✅
You should use an query that provides aggregate data grouped by month (or week). While you could do it with your database’s date functions, I suggest using the Groupdate gem which will streamline the process in case you need it frequently. The you could do something like.
# this should probably be done in a controller action
<% @data = Order.where(date: 1.years.ago.beginning_of_month..Time.now).group_by_month(:date).count %>
# get an array of monthly labels in js file
...
labels: <%= @data.map{ |month, order_count| month } %>
...
# get the values for those months
...
data: <%= @data.map{ |month, order_count| order_count } %>
...
Needless to say, this example just shows a monthly order count. If you need other values you’d have to adjust the query. Furthermore Groupdate offers a lot of options to group this data. Be sure to read the docs: https://github.com/ankane/groupdate
Source:stackexchange.com