使用Seer在Rails应用程序中按日期绘制新用户图形 [英] Graphing new users by date in a Rails app using Seer

查看:136
本文介绍了使用Seer在Rails应用程序中按日期绘制新用户图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经安装了Seer:

我想在前7天使用Seer实现一个日期显示新用户的滚动图表。 >

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

我正在努力让我的大脑围绕如何实现。



我有我想要绘制的用户数组:

@users = User.all(:conditions => {:created_at => 7.days.ago..Time。
$ b

无法看到正确的方法来实现:data_method以通过created_at日期将它们卷起。



任何人都可以在Seer示例页面(以上链接)完成此操作或与之类似吗?



p>

解决方案

我假设你ar e试图在最近7天内显示新的用户数量。如果是这样,您可以执行以下操作:

控制器代码

 #声明一个结构来保存结果
UserCountByDate = Struct.new(:date,:count)
$ b $ def报表
@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(日期,计数)
结束

结束

查看代码

 < div id =chart>< / DIV> 

<%= Seer :: visualize(
@user_counts,
:as =>:column_chart,
:in_e​​lement =>'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 =>最近7天的新用户,
:title_x =>'date',
:title_y =>'count'
}

- %>

data_method 应该出现在每行中用作图表输入的数组。 ActiveRecord count 方法返回一个散列,该散列被转换为 struct 的数组,该数组响应 date count 方法。


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.

这篇关于使用Seer在Rails应用程序中按日期绘制新用户图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆