나는 이것을 Railscast 따르고 내 문의 모델에 대한 morris.js 꺾은 선형 차트를 만들려고합니다.레일 개월 주어진 주어진 범위의 반복
date_trunc
을 사용하여 수를 그룹화했지만 몇 달이 지난 지금 (예 : 2012 년 6 월, 7 월) X 축을 반복하는 방법에 대해서는 잘 모르겠습니다. railscasts 노트.
범위 # 단계 method here을 시도했지만 이제 그래프에는 계산없이 하나의 날짜 (2012-07-01) 만 표시됩니다. 범위 변수에서 메서드를 주석 처리하면 그래프는 제대로 작동하지만 x 축은 날짜별로 반복됩니다.
class Enquiry < ActiveRecord::Base
def self.chart_data(start = 1.year.ago)
total_count = total_count_by_month(start)
start = start.to_date.beginning_of_month
today = Date.today.beginning_of_month
range = (start..today).step(1.month)
range.map do |month|
{
created_at: month,
total_enquiries: total_count[] || 0
}
end
end
def self.total_count_by_month(start)
enquiries = unscoped.where(created_at: start.beginning_of_month..Time.now)
enquiries = enquiries.group("date_trunc('month', created_at)")
enquiries = enquiries.select("date_trunc('month', created_at) as created_at, count(*) as count")
enquiries.each_with_object({}) do |enquiry, counts|
counts[enquiry.created_at.to_date] = enquiry.count
end
end
end
차트의 x 축을 날짜가 아닌 몇 개월 (2013 년 6 월, 2013 년 7 월 ...) 반복하는 방법은 무엇입니까? 월별로
def self.chart_data(start = 1.year.ago)
total_count = total_count_by_month(start)
##############################################
start = start.to_date.beginning_of_month
today = Date.today.beginning_of_month
range = (start..today).select {|d| d.day == 1}
##############################################
range.map do |month|
{
created_at: month,
total_enquiries: total_count[] || 0
}
end
end
차트의 x 축 지금 반복 : 같은 문제에 직면 다른 사람들을위한
가능한 복제본 [날짜 개체를 사용하여 매월 반복] (0120-385-301) – oldergod
omg 나는 그런 바보 같은 기분이 든다! .select (| d |)로 .step (1.month)를 대체하십시오. d.day == 1}은 매력처럼 작동합니다. –
아이러니하다. 나는 실제로 포기하기 전에 몇 시간 동안 그 솔루션을 작동 시키려고 노력했다. 항상 그렇듯이 솔루션은 항상 한 발 앞선 것입니다. 많은 감사 –