0

在我的模型,我有:Rails如何在2個日期之間爲每個月創建圖表數據?

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列的開始月份。所以很容易按月分組。但是,我如何爲每個月創建JSON數據?目前它沒有工作。但是,每天的數據工作正常。

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"> 

回答

1

嘗試使用

(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  
+0

我已經嘗試了您的第一個解決方案,現在圖表出現,但created_at日期錯誤。 –

+0

我想獲得[{「created_at」:「2012-08-01」,「clicks」:0},{「created_at」:「2012-09-01」,「clicks」:11}]等數據 –

相關問題