【问题标题】:Rails 3.2 Chartkick line chart grouped query doesn't display correctlyRails 3.2 Chartkick 折线图分组查询无法正确显示
【发布时间】:2015-06-16 15:04:31
【问题描述】:

我正在尝试使用 chartkick 来显示一个多系列折线图,它跟踪 过去一年中按月划分的总入站和出站物料流。

查询应生成类似于以下内容的内容:

    SELECT SUM(ship_total_net_weight) AS sum_ship_total_net_weight,
    month(ship_date) AS month_ship_date, year(ship_date) 
    AS  year_ship_date 
    FROM [Downstream]
    WHERE is_active = 1 AND from_company_id = 89 
    AND
    (ship_date BETWEEN '2014-06-15 18:15:26.196' 
    AND '2015-06-15 18:15:26.196') 
    AND (to_company_id <> 89) 
    GROUP BY month(ship_date), year(ship_date)
    order By  year(ship_date), month(ship_date)

我的图表踢代码如下:

<%= line_chart [
              {name: "Inbound", data: Downstream.where(:to_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now).where('from_company_id <> ?', current_user.company_id ).group('month(ship_date), year(ship_date)').sum(:ship_total_net_weight)},
              {name: "Outbound", data: Downstream.where(:from_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now ).where('to_company_id <> ?', current_user.company_id ).group('month(ship_date), year(ship_date)').sum(:ship_total_net_weight)}
               ] %>

我从 chart kick 得到的结果是每个系列的一条线只有两个数据点,一个在 2013 年 12 月 31 日,一个在 2014 年 12 月 31 日。

对数据库运行上述 SQL 生成 9 个结果,范围从 6/2014 到 2/2015

为什么数据集不同?我的图表踢的语法错误吗?我需要以某种方式更改查询吗?

提前感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails-3.2 chartkick active-record-query


    【解决方案1】:

    我找到了解决方案,我的查询返回了单独的日期/月份列,因此我需要更改 group 子句,以下折线图可以正常工作:

    <%= line_chart [
                    {name: "Inbound", data: Downstream.where(:to_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now).where('from_company_id <> ?', current_user.company_id ).group('dateadd(month, datediff(month,1,ship_date),1)').sum(:ship_total_net_weight)},
                    {name: "Outbound", data: Downstream.where(:from_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now ).where('to_company_id <> ?', current_user.company_id ).group('dateadd(month, datediff(month,1,ship_date),1)').sum(:ship_total_net_weight)}
                            ] %>
    

    在 group 子句中使用 dateadd 和 datediff 函数允许按日期或日期时间字段的月/年排序,并在单个列中返回完整日期。

    -旁注-我正在使用通常与 chartkick 结合使用的 groupdate gem 不支持的 SQLServer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2023-03-24
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多