Rails: using groupdate & chartkick to create a custom user chart

I use groupdate and chartkick to try and display a graph showing the rise (and fall) of our user base over time.

Using the following, it works fine in a column chart, but goes into a line chart:

sum=0 User.group_by_day(:created_at).count.map { |x,y| { x => (sum += y)} }.reduce({}, :merge) 

Can someone point me in the right direction? Or is there a better way to make this work?

+7
source share
2 answers

Martin's answer was close, but I ended up using:

 User.group_by_week(:created_at).order("week asc").count.map { |x,y| { x => (sum += y)} }.reduce({}, :merge) 

To receive weekly - pay attention to the order ("week ascending") - this is what fixed it ...

+10
source

This is because you need to sort before you make a cumulative amount.

Try the following:

 sum=0 User.group_by_day(:created_at).count.to_a.sort{|x,y| x[0] <=> y[0]}.map { |x,y| { x => (sum += y)} }.reduce({}, :merge) 
+11
source

All Articles