0

我使用了我的数据格式的各种排列来尝试渲染 Chartkick 的多折线图。我无处可去。我将在此处粘贴我用来返回数据的方法的当前迭代,但要知道我已经尝试了整个早上的变体:

def trailing_seven_day_profit(id)
    user = User.find id
    jobs     = Job.select("id")
                 .where("billed != 0.0")
                 .where("start_date >= ?", Date.today - 1.week)
                 .where(user_id: id).to_a
    mon_sell = tue_sell = wed_sell = thu_sell = fri_sell = sat_sell = sun_sell = 0
    mon_cost = tue_cost = wed_cost = thu_cost = fri_cost = sat_cost = sun_cost = 0
    products = Product.where("job_id IN (?)", jobs)
    products.each do |p|
      if p.created_at.strftime("%a") == 'Mon'
        mon_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        mon_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Tue'
        tue_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        tue_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Wed'
        wed_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        wed_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Thu'
        thu_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        thu_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Fri'
        fri_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        fri_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Sat'
        sat_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        sat_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Sun'
        sun_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        sun_cost += (p.quantity * p.cost).to_f
      end
    end
    @data = [
      ['Name', 'Day', 'Sell', 'Cost'],
      [user.name, 'Mon', mon_sell.round(2), mon_cost.round(2)],
      [user.name, 'Tue', tue_sell.round(2), tue_cost.round(2)],
      [user.name, 'Wed', wed_sell.round(2), wed_cost.round(2)],
      [user.name, 'Thu', thu_sell.round(2), thu_cost.round(2)],
      [user.name, 'Fri', fri_sell.round(2), fri_cost.round(2)],
      [user.name, 'Sat', sat_sell.round(2), sat_cost.round(2)],
      [user.name, 'Sun', sun_sell.round(2), sun_cost.round(2)]
    ]
    @data
  end

关于我的数据库的一些解释 - 乔布斯有很多产品。产品记录保存和项目添加到工作时的成本、售价(变化)、税收、数量和与工作的关系、我们获得项目描述的库存等,以及一些时间戳。

因此,对于具有三个产品的 Job #1,我们需要 Job.find(1).products.each 才能查看信息。

或 Product.find("job_id in (?)", [array_of_job_ids]) 这是我在这里采用的方法。

过去我曾经有过这份报告,但它都是使用 Google Charts 手动完成的,而且代码很糟糕。所以我找到了 Chartkick,我喜欢这些视图与我所做的相比是多么简单。

现在我无法渲染任何东西。

视图应该很简单:

              <% @users.each do |u| %>
                <div class="widget-content">
                  <%= line_chart name: u.name, data: ReportMaker.new.trailing_seven_day_profit(u.id) %>
                </div>
              <% end %>

但这没有任何意义。

我已经尝试过 as_json,尝试过作为哈希,它目前是数组格式,但是数据永远不会出现,图例也不会出现。

所以,很明显,出了点问题。但是谷歌诸如“chartkick 多系列格式”之类的东西,你会得到一堆非常无用的结果——来自 Chartkick 页面的文档(对不起,老兄),很烂。

他说:

<%= line_chart [
  {name: "Series A", data: series_a},
  {name: "Series B", data: series_b}
] %>

那么 wtf series_a 应该是什么样子?JSON?哈希?增强现实对象?大批?

我没有做简单的事情,例如“工作计数”或“按 created_at 分组产品”,这是所有示例所显示的。

我必须遍历集合,提取值并组合它们,然后渲染一些数据集。

另外 - 产品的不干燥性。下面的每个块都伤害了我的心。很高兴接受有关如何干燥的任何指示:)

4

1 回答 1

0

products.each块是丑陋的,因为这项工作应该在数据库级别完成。你需要这样的东西:

def trailing_seven_day_profit(id)
  Product.connection.select_all('
    SELECT u.name, DAYNAME(p.created_at) AS day, 
      p.price * p.quantity + p.tax * p.price * p.quantity AS sell, 
      p.quantity * p.cost AS cost
    FROM products p
    INNER JOIN jobs j ON j.id = p.job_id
    INNER JOIN users u ON u.id = j.user_id
    WHERE j.billed != 0 AND j.start_date >= CURDATE() - INTERVAL 1 WEEK
    WHERE u.id = #{id.to_i}
    GROUP BY DAYNAME(p.created_at)
  '
end

由 OP 编辑​​:

我修改了您的查询以获得任何结果:

SELECT to_char(p.created_at, 'day') AS day_of_week,
               p.price * p.quantity + p.tax * p.price * p.quantity AS sell,
               p.quantity * p.cost AS total_cost
FROM products p
  INNER JOIN jobs j ON j.id = p.job_id
  INNER JOIN users u ON u.id = j.user_id
WHERE j.billed != 0 AND j.start_date >= CURRENT_DATE - INTERVAL '7 DAY' AND u.id = 242
GROUP BY to_char(p.created_at, 'day'), sell, total_cost

但这打印出一些完全不正确的东西:

SQL 查询的结果

我希望将一周中的最后 7 天作为不同的行,并汇总成本和售价。我将为每个用户获取此查询以获取我想要的 Chartkick 数据。我不是数据库向导,我想更多地了解这个可能的解决方案。

在这里编辑是因为我的评论太长了...

于 2015-09-21T17:19:12.143 回答