与Chartkick&安培协会;轨 [英] Associations with Chartkick & Rails

查看:144
本文介绍了与Chartkick&安培协会;轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用chartkick创建的记录,这属于一个计划任务的线图。

I want to use chartkick to create a line graph of records, which belong to a planned task.

在换句话说: plantask的has_many记录

记录有两个字段我感兴趣的图形。该created_at,这将是我的X轴和数据(整数),这将是我的Y轴。

Records has two fields I'm interested in graphing. The created_at, which will be my X-axis, and data(An integer), which will be my Y-axis.

到目前为止,我已经得到了pretty接近。通过插入我的看法是:

So far I've gotten pretty close. By inserting this into my view:

<%= line_chart @plantask.records.group_by_day(:created_at).sum(:data) %>

我可以看到我的x轴的完美显示。然而,看来y轴未加载记录:created_at字段,但加载:从plantask模型内created_at。 (我所有的纪录在下午7:00被映射到昨天)这似乎很奇怪我。什么我搞砸任何提示吗?谢谢你们。

I can see that my x-axis is displaying perfectly. However, it appears that the y-axis is not loading the records :created_at field, but is loading the :created_at from within the plantask model. (All of my records are mapped to yesterday at 7:00pm) This seems strange to me. Any hints on what I've messed up? Thanks you guys.

推荐答案

原来,我是处理这个问题的错误的方式。通过一天总和组的每个任务合并成一个,并增加了该值。我真正需要的是这样的:

It turns out that I was approaching this problem the wrong way. Group by day with sum combines every task into one, and adds the value. What I really needed was this:

<%= line_chart @plantask.records.group(:created_at).sum(:data) %>

这篇关于与Chartkick&安培协会;轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆