I want to display the chart of revenue which is total * quantity
I can do charts like this to display the total:
<%= line_chart #line_items_sold.group_by_day(:created_at).sum(:total) %>
Although doing sum with multiple attributes:
<%= line_chart #line_items_sold.group_by_day(:created_at).sum{|li| li.store_price.to_d * li.store_fulfillable_quantity} %>
doesn't work...
error:
ActionView::Template::Error (PG::GroupingError: ERROR: column "line_items.id" must appear in the GROUP BY clause or be used in an aggregate function
Doing:
<%= line_chart #line_items_sold.sum{|li| li.store_price.to_d * li.store_fulfillable_quantity}.group_by_day(:created_at) %>
Error: Can't group_by nBigDecimal
Any ideas on how I can accomplish this?
I want to show the revenue (price * quantity) on a per day basis.
Related
I have a Data model with 2 columns, :month_name (string) and :net_sales (integer)
I'm trying to make a chart with the Y axis for the month and the X axis for the net_sales.
I can't see documentation of how to achieve this, any help? Thanks!
<%= line_chart #business_data.pluck(:net_sales, :month_name) %>
<%= line_chart #business_data.group(:net_sales).maximum(:month_name) %>
I'm using Impressionist gem to capture impressions for records, and I'm using chartkick to graph the results. What I would like to do, is count the impressions for the current_user's records, and display the count on chartkick. Its a simple issue, but I cannot seem to figure out why the impressions are not being displayed on the graph. My code is below
analytics.html.erb
<%= line_chart current_user.posts.sum(&:impressionist_count).group_by_hour(:created_at).count, refresh: 60, xtitle: 'Hourly Posts Impression Count', ytitle: 'Amount of Views', label: 'View Count' %>
I found a solution to this problem. Just in case anyone has it in the future. The code below, will inject the sum of the impressions into a hash, and then it will display correctly on the graph.
<%= line_chart current_user.posts.group_by_day(:created_at).count.inject(Hash.new(0)) { |acc, (k,v)| acc[:impressionist_count] += v; acc[k] = acc[:impressionist_count]; acc }.except(:impressionist_count) %>
I'm stuck on a tiny problem regarding chartkick. I have a rail app where you can create different currencies. You can then create a expense with a title, a amount and choose the currency from a list and the user_id. The relations are made and working. I have in my user controller something like this :
#user_spendings = #current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 15)
#sums_by_currency = Currency.joins(:spendings).
select(:symb, 'SUM(spendings.amount) AS amount').
where(spendings: { id: #user_spendings.map(&:id) }).
group(:symb)
And in my show view (as I want the expense from each user to be shown there) something like this :
<% #sums_by_currency.each do |currency| %>
<%= '%.02f' % "#{currency.amount}" %> <%= "#{currency.symb}" %>
<% end %>
That shows me the sum for each spending depending on the currency.
I would like to use this total and use chartkick to display the spending, with the date when this spending has been created.
I've tried several things already
First I went with this just to see :
<% #sums_by_currency.each do |currency| %>
<%= bar_chart currency.amount %>
<% end %>
Well I have to charts appearing but nothing shows up. Maybe the loop isn't the solution. Then I thought about the .map but I don't really know how to put that in place to be honnest.
I tried this aswell :
<%= line_chart #current_user.spendings.group(:date).sum(:amount) %>
That shows me the total spendings from all the currencies. I have to find out how to split all the currencies in different charts and show only the total amount from each currency.
If anyone can give me a clue I would appreciate it.
Thanks alot.
Ok guys I got it !
Took me 2 days only...
For the one interested in the answer here is what I did. I actually didn't change anything in the controller and I let the #sums_by_currency like it is.
Instead I went for that :
<%= column_chart #current_user.spendings.all.joins(:currency).group('currencies.symb').group_by_month(:date, format: "%B %Y").sum(:amount) %>
Give me all the spendings from the current_user from where I joined the currency that I grouped by symb. Then I grouped everything by month and I get the sum from the amount.
Yeah, you need to pass a set of data to the chart, not lots of individual pieces of data for individual charts (ie what you've got with the loop). Using map to convert your currency-objects to their amounts makes sense eg
<%= bar_chart #sums_by_currency.map{|c| c.amount } %>
or if you need a name something like:
<%= bar_chart #sums_by_currency.map{|c| {name: c.unit, data: c.amount} } %>
Where unit is whatever currency unit eg AUD
I would like to add sort links for the following query:
#q = Order.group(:suburb).select("suburb, COUNT(*) as count, SUM(total) as total, AVG(total) as average").ransack(params[:q])
However when i use:
<%= sort_link(#q, :average) %>
The URL gets populated correctly, but no sorting is in place!
When I inspect #q.sorts, the columns are correctly there.
There is no average column in database so it won't work like that. You could try hack this with ransacker:
ransacker :average do
Arel.sql('average')
end
so this average will be used when average is found.
I'm trying to load this variable:
#electro_total = current_user.electros.sum(:electricity_kwst)
to a graph in my users/show.html.
I want the chart to only show one column for now, a column with the sum of all the numbers in each :electricity_kwst field.
The variable is currently located in the applications_controller, but I have moved it to the show method in users_controller, it didn't work either.
I've also tried to hardcode this to the chart, but it didn't work:
<%= column_chart [
{name: "KWst per m²", data: current_user.electros.sum(:electricity_kwst)}] %>
This is the code in my view:
<%= column_chart #electro_total %>
It doesn't break the site but it always gives me the error:
Error Loading Chart:
where the chart should be.
How am I able to do this without getting this error all the time?
The only thing I had to do was to rewrite the code like this:
<%= column_chart [
{name: "KWst per m²", data: current_user.electros.group_by_year(:created_at).sum(:electricity_kwst)}] %>
This gives me the one column I wanted and it shows the sum of all :electricity_kwst, in this case for the whole year.