Rails 如何为两个日期之间的每个月创建图表数据? [英] Rails how to create chart data for each month between 2 dates?

查看:47
本文介绍了Rails 如何为两个日期之间的每个月创建图表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有:

  def self.chart_data(start, slut)
  if start.nil?
  start = 2.weeks.ago
  end

  if slut.nil?
  slut = Date.today
  end

    range = start.to_date..(slut.to_date + 1.day)

  if start.to_date.beginning_of_month.to_s != slut.to_date.beginning_of_month.to_s

kliks = count(
  :group => 'date(month)',
  :conditions => { :created_at => range }
)
  # CREATE JSON DATA FOR EACH MONTH - PROBLEM HERE
  (range).map(&:beginning_of_month).uniq.map(&:to_s).each do |month|
    {
      created_at: month,
      clicks: kliks[month] || 0
    }
  end


  else

  kliks = count(
  :group => 'date(created_at)',
  :conditions => { :created_at => range }
)

  #WORKS FINE DATA FOR EACH DAY
  (start.to_date..slut.to_date).map do |date|
    {
      created_at: date.strftime("%F"),
      clicks: kliks[date] || 0
    }
  end
 end
end

我在表格中添加了一个列月份.这是 created_at 列的 begin_of_month .所以很容易按月分组.但是我如何为每个月创建 JSON 数据?目前它不起作用.但是每天的数据工作正常.

I have added a column month to my table. That is the beginning_of_month of the created_at column. So that is easy to group by month. But how do I create the JSON data for each month? Currently it dosen't work. But the data for each day works fine.

Klik.chart_data("1/8/2012", "22/9/2012") 的结果

    <div data-clicks="[{&quot;created_at&quot;:&quot;2012-09-08&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-09&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-10&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-11&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-12&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-13&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-14&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-15&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-16&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-17&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-18&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-19&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-20&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-21&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-22&quot;,&quot;clicks&quot;:11}]" id="click_chart">

第一个回答结果:

<div data-clicks="[{&quot;created_at&quot;:&quot;2012-09-08&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-09&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-10&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-11&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-12&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-13&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-14&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-15&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-16&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-17&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-18&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-19&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-20&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-21&quot;,&quot;clicks&quot;:0},{&quot;created_at&quot;:&quot;2012-09-22&quot;,&quot;clicks&quot;:11}]" id="click_chart">

推荐答案

尝试使用

(start.to_date..slut.to_date).map(&:beginning_of_month).uniq.map do |date|
  {
    created_at: date.strftime("%F"),
    clicks: kliks[date] || 0
  }
end

(start.to_date.beginning_of_month..slut.to_date).reject{|date| date.day !=1}.map do |date|
  {
    created_at: date.strftime("%F"),
    clicks: kliks[date] || 0
  }
end      

这篇关于Rails 如何为两个日期之间的每个月创建图表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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