Graphing new users by date in a Rails app using Seer
Asked Answered
I

1

3

I'd like to implement a rolling graph showing new users by day over the last 7 days using Seer.

I've got Seer installed:

http://www.idolhands.com/ruby-on-rails/gems-plugins-and-engines/graphing-for-ruby-on-rails-with-seer

I'm struggling to get my brain around how to implement.

I've got an array of the Users I want to plot:

@users = User.all( :conditions => {:created_at => 7.days.ago..Time.zone.now})

Can't see the right way to implement the :data_method to roll them up by created_at date.

Anyone done this or similar with Seer?

Anyone smarter than me able to explain this after looking at the Seer sample page (linked above)?

Iveson answered 14/3, 2010 at 2:20 Comment(0)
E
6

I am assuming you are trying to show the new user count by day in last 7 days. If so you can do the following

Controller code

# declare a struct to hold the results
UserCountByDate = Struct.new(:date, :count) 

def report
  @user_counts = User.count( :group => "DATE(created_at)", 
                   :conditions => ["created_at >= ? ", 7.days.ago], 
                   :order => "DATE(created_at) ASC"
                 ).collect do |date, count| 
                   UserCountByDate.new(date, count)
                 end

end

View code

<div id="chart"></div>

<%= Seer::visualize(
      @user_counts, 
      :as => :column_chart,
      :in_element =>'chart', 
      :series => {
        :series_label => 'date',
        :data_method => 'count'
      },
      :chart_options => {
        :height => 300,
        :width => 100 * @user_counts.size,
        :is_3_d => true,
        :legend => 'none',
        :colors => "[{color:'#990000', darker:'#660000'}]",
        :title => "New users in last 7 days",
        :title_x => 'date',
        :title_y => 'count'
      }
    )
 -%>

The data_method should be present in each row of the array used as the input for the chart. The ActiveRecord count method returns a hash, which is converted to an array of struct that responds to date and count methods.

Elaina answered 14/3, 2010 at 8:47 Comment(2)
I think I follow you. I really appreciate the help. I'm one of those guys that knows enough to get something done, but my lack of professional training and experience shows when I get into the weeds :-) I'll let you know how it goes.Iveson
Brilliant...that nailed it. I grok your code, but need to spend some time reading to make sure I understand the count and collect methods fully. Thanks a ton...I really appreciate it.Iveson

© 2022 - 2024 — McMap. All rights reserved.