御存知を使用してのRailsアプリで日付ごとにグラフ化新規ユーザー
-
19-09-2019 - |
質問
私はシーアを使用して、最後の7日間にわたり日までに新規ユーザーを示すローリンググラフを実装したいと思います。
私が持っている御存知がインストールされます:
<のhref = "http://www.idolhands.com/ruby-on-rails/gems-plugins-and-engines/graphing-for-ruby-on-rails-with-seer" のrel = "nofollowをnoreferrer "> http://www.idolhands.com/ruby-on-rails/gems-plugins-and-engines/graphing-for-ruby-on-rails-with-seer の
私は、実装する方法を中心に私の脳を取得するために苦労しています。
私はプロットしたいユーザーの配列を持ってます:
@users = User.all(:条件=> {:のcreated_at => 7.days.ago..Time.zone.now})
実装するための正しい方法を参照することはできません:created_atと日付によってそれらをロールアップするdata_methodを
誰もが御存知でこのまたは類似の行われていますか。
私より賢く、誰でもできる(上記のリンク)シーアサンプル・ページを見た後、これを説明するには?
解決
私はあなたが最後の7日間で日までに、新規ユーザー数を表示しようとしていると仮定しています。もしそうなら、あなたは次の
を行うことができますのコントローラのコードの
# 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
を見るコードの
<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'
}
)
-%>
data_method
グラフの入力として使用されるアレイの各行に存在すべきです。 ActiveRecord count
方法はstruct
とに応答date
の配列に変換されたハッシュを返すcount
方法。