如何创建具有多个行和组的morris.js折线图

时间:2014-02-05 19:11:26

标签: javascript jquery ruby-on-rails-3 postgresql morris.js

我有很多@employees接受投票。

我使用下面的代码在图表上显示每月所有业务中所有员工收到的总票数。

在视图中:

<%= content_tag :div, "", id: "employee_vote_count_chart", data: {votes: Vote.chart_data(2.months.ago)} %>

在模型和控制器中:

Vote belongs_to :employee
Employee has_many :votes

Vote.chart_data

def self.chart_data
 total_count = 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|
  {
   month: month,
   total_votes: total_count[month] || 0
  }
 end
end

def self.count_by_month(start)
 enquiries = unscoped.where(created_at: start.beginning_of_day..Time.zone.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

我想修改代码,以便它返回每个员工每月在特定企业中收到的票数。

目前,它每月为所有员工和所有企业返回total_votes的数据:

data: [
    { month: 'january 2013', total_votes: 180 },
    { month: 'february 2013', total_votes: 190 },
    { month: 'march 2013', total_votes: 180 }
  ]

我希望代码每月向每位员工返回数据,并将其限制为某个业务:

  data: [
    { month: 'january 2013', employee_1: 100, employee_2: 80 },
    { month: 'february 2013', employee_1: 100, employee_2: 90 },
    { month: 'march 2013', employee_1: 80, employee_2: 100 }
  ]

我正在使用rails 3,postgresql,morris.js。

0 个答案:

没有答案