Search code examples
ruby-on-railsrubychartschartkickcolumn-chart

How to make column charts x axis present exactly one week in Ruby on Rails?


I am using "chartkick" gem for presenting data with the help of column chart. Shortly, I am doing "ToDo" project and want to show how many tasks have been completed by the user each day of the last week.

However, I want the x axis represent exactly 7 days, even if there are no completed tasks.

A picture of column chart now

1

Here is how I make column chart.

<%= column_chart @completed_tasks.where("completed_time >= ?", 1.week.ago).group_by_day(:completed_time).count%>

And in case if needed, here is how i get @completed_tasks variable.

@completed_tasks = Task.where(user_id: current_user.id, completed: true)

I have been thinking about it for a couple of days, but did not come up to anything, in addition i could not find any ideas on the Internet, so I hope someone here knows how to do it.


Solution

  • The answer is:

    <%= column_chart @completed_tasks.group_by_day(:completed_time, last:7).count%>
    

    Thanks to @anothermh