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)?
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.
精彩评论