1

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.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Liuba
  • 33
  • 8
  • 1
    I can't help you with chartkick but in modern versions of Ruby (>= 2.6.0) you can use an [endless range](https://ruby-doc.org/core-2.6/Range.html#class-Range-label-Endless+Ranges) in your query like `where(completed_time: 1.week.ago..)` which helps make the code a little cleaner. – anothermh Dec 17 '22 at 21:26
  • is there a chance that you might know someone who can help me? This is my final project at uni and the deadline is soon :( – Liuba Dec 18 '22 at 06:01
  • 1
    I'm not certain if this is what you're looking for but you can check [this answer](https://stackoverflow.com/a/22930017/3784008) for a possible solution. The person asks *I want to have a rolling 5 day chart* which sounds very similar to this. The person that provided the answer is the author of chartkick. There's additional detail in the [chartkick docs](https://github.com/ankane/groupdate#time-range). – anothermh Dec 18 '22 at 18:39

1 Answers1

1

The answer is:

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

Thanks to @anothermh

Liuba
  • 33
  • 8