Rails:Chartkick累积用户图 [英] Rails: Chartkick cummulative user graph
问题描述
我在活动的管理控制台中使用chartkick,并试图跟踪一段时间内的累积用户群。
I'm using chartkick in an active admin dashboard and trying to track a cumulative user base over time.
使用groupdate按周进行计数,我可以成功创建一个显示时间点计数的图表,但很难显示累积结果。
Using groupdate to count by week I can successfully create a chart displaying point in time counts but struggling to display cumulative results.
关于未找到类似问题的最佳方法的任何建议?
Any suggestion on best way to approach as I have not found a similar question?
图表局部
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
<%= line_chart User.group_by_week(:created_at).count %>
dashboard.rb
column do
panel "Recent User Additions" do
table_for User.order("id desc").limit(10).each do |_user|
column(:email) { |user| link_to(user.email, admin_user_path(user)) }
column(:state_waters) { |user| link_to(user.state_waters, admin_user_path(user)) }
column(:state_waters) { |user| link_to(user.home_port, admin_user_path(user)) }
end
end
推荐答案
如果我理解您的问题,则需要操纵数据的哈希。
If I understand your question, you need to manipulate the hash of data.
在控制器中执行以下操作:
In controller do somehing like this:
@data = User.group_by_week(:created_at).count
accumulator = 0
@data.transform_values! do |val|
val += accumulator
accumulator = val
end
然后将视图显示为<%= line_chart @data%>
如果检查哈希<%= User.group_by_week(:created_at).count.inspect%>
它变得清楚。
If you inspect the hash <%= User.group_by_week(:created_at).count.inspect %>
it becomes clear.
在为了在任何哈希上使用累加器,将自定义方法添加到类哈希中可能很有用。
In order to use accumulator over any hash, could be useful to add a custom method to Class hash.
module HashPatch
def accumulate_values!
accumulator = 0
transform_values! do |val|
val+= accumulator
accumulator = val
end
end
end
Hash.include HashPatch
points = {'x1' => 0, 'x2' => 10, 'x3' => 10, 'x4' => 10.1}
points.accumulate_values! #=> {"x1"=>0, "x2"=>10, "x3"=>20, "x4"=>30.1}
transform_values!
自Ruby v4.2.1起
transform_values!
since Ruby v4.2.1
这篇关于Rails:Chartkick累积用户图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!