Graphing new users by date in a Rails app using Seer

让人想犯罪 __ 提交于 2019-12-04 16:38:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!