I've used various permutations of my data format to try and get Chartkick's multiple line chart to render. I'm getting nowhere. I'll paste in the current iteration of the method I'm using to return the data here, but know that I've tried variations of this all morning:
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
Some explanation on my database - Jobs have many products. Product records hold and item's cost when it was added to the job, sell price (varies), tax, quantity, and relations to the job, inventory where we get the item's description, etc., and some timestamps.
So for Job #1 that has three products, we would need to Job.find(1).products.each in order to see information.
Or Product.find("job_id in (?)", [array_of_job_ids]) which is the approach I'm taking here.
I had this report working at one point in the past, but it was all manual using Google Charts and the code was horrible. So I found Chartkick and I like how simple the views are compared to what I was doing.
Now I can't get anything to render.
The view should be as simple as :
<% @users.each do |u| %>
<div class="widget-content">
<%= line_chart name: u.name, data: ReportMaker.new.trailing_seven_day_profit(u.id) %>
</div>
<% end %>
But that renders nothing.
I've tried it as_json, tried it as a hash, it's currently in an array format, but the data just never appears, nor does the legend.
So, clearly, something is wrong. But google something like "chartkick multi series format" and you get a bunch of really unhelpful results - the documentation from the Chartkick page (sorry dude), sucks.
He says:
<%= line_chart [
{name: "Series A", data: series_a},
{name: "Series B", data: series_b}
] %>
So wtf is series_a supposed to look like? JSON? Hash? AR Object? Array?
I'm not doing simple things like "count of jobs" or "group products by created_at" which is all the examples show.
I have to iterate through collections, pull out values and combine them, and then render some data set.
Also - the unDRYness of the products.each block below hurts my heart. Happy to accept any pointers on how to DRY that up :)
Aucun commentaire:
Enregistrer un commentaire