我已经使用了我的数据格式的各种排列来尝试获取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])这是我在这里采用的方法 .
我曾经在过去的某个时刻报道过这个报告,但是使用谷歌图表都是手动的,而且代码太可怕了 . 所以我找到了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?哈希? AR对象?阵列?
我不是在做简单的事情,比如“工作次数”或“由created_at组产品”,这是所有示例所示 .
我必须遍历集合,提取值并组合它们,然后渲染一些数据集 .
此外 - 产品的不干净 . 下面的区块伤害了我的心 . 很高兴接受关于如何干涸的任何指针:)
1 回答
products.each
块很难看,因为这个工作应该在数据库级别完成 . 你需要这样的东西:OPIT编辑:
我修改了您的查询以获得任何结果:
但这打印出一些完全不正确的东西:
我希望将一周中的7天作为不同的行,以及汇总成本和卖出价格 . 我将为每个用户获取此查询以获取我想要的Chartkick数据 . 我不是数据库向导,我想更多地了解这个可能的解决方案 .
在这里编辑,因为我的评论太长了......