Chartkick define data for each axis Rails - ruby-on-rails

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) %>

Related

Using sum with chartkick on multiple attributes

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.

How to graph impressions with chartkick and impressionist?

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) %>

Loading variable to ChartKicks column chart gives Error Loading Chart:

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.

Labels and Chartkick

Chartkick is amazing, and I can do super simple things like
<%= column_chart Device.joins(:reviews).group(:brand).average(:average_rating) %>
and get results like:
Now, the problem is that the labels aren't fed through to chartkick. This is what chartkick uses to render the chart:
Chartkick.ColumnChart("chart-5", {"#<Brand:0x00000008a3c048>":"78.8","#<Brand:0x0000000879e840>":"80.70000712076823","#<Brand:0x0000000853e9d8>":"81.11111111111111","#<Brand:0x000000082808e0>":"73.42857142857143"}, {});
So, the big question is... How can I use this super easy querying method but get the brand names on the chart?
I tried:
<%= column_chart Device.joins(:reviews).group(:'brand.name').average(:average_rating) %>
...and other variations to no avail...
Basically I just needed to make a small change to my query:
<%= column_chart Device.joins(:reviews).includes(:brand).group(:'brands.name').average(:average_rating) %>
Then I ended up re-ordering this by average ratings instead of brand name:
devices = Device.joins(:reviews).includes(:brand).group('brands.name').average(:average_rating)
arr = styles.to_a
arr.map! { |pair| [pair.first, pair.second.to_f] }
arr.sort! { |a, b| a.second <=> b.second }
#sorted_hash = ActiveSupport::OrderedHash[arr]
View:
<%= column_chart #sorted_hash %>
Bingo:

Adding Rails data to Highcharts series

I am trying to make a chart to display a users weight, using highcharts. I have all of the correct js files included and when passed a simple array of integers, it works just fine.
However I am unsure, having read the highcharts docs, how to pass the correct data into the series options using rails.
my user model is as follows:
A user has many weigh_ins, which has weight:float user_id:integer id:integer created_at:datetime fields. (if this is relevant)
Below is my view, as you can see i have tried to iterate through the users weights and then pass that as the data. I know it needs to be fed json and an array, but im not sure how to format my data to fit in that way.
borderWidth: 0
},
series: [{
pointInterval: <%= 1.day * 1000 %>,
name: 'weight',
data: <% #user.weigh_ins.each do |weight| %>
<%= weight.weight.to_json %>
<% end %>
}]
If there is any more code that needs posting, just shout.
any suggestions would be greatly appreciated.
Thata defo on the right track, however, the data is dynamic so a user can update their weights everyday, so the chart needs to update aswell. If i use the square brackets like above then the graph doesnt even render. Now i have this but it doesnt produce the correct output either. I need to find the weigh_ins for the correct user, then iterate through all of them and select the weight figure which is a float. Not sure how to tackle it.
data: <% user = #user.weigh_ins %>
<% user.each do |user_weight| %>
[<%= user_weight.weight %> ]
<% end %>
I'm something similar with this code:
<% bio_array = Array.new(#bio.size) %>
<% #biometrics.each_with_index {|x, index| %>
<% bio_array += [x[:weight]] %>
<% } %>
(... some code ...)
series: [{
name: 'Some Name',
data: <%= bio_array %>
Please note that this code is not optimized for you case (in my case i have other logic that i omitted for being useless to your case - i had a multidimensional arrays with [data and value]).
if you want to parse data like that , you don't really have to use Json , becoz since yo don't update dynamically , you can just echo it like [34,32,24,25,12] bt first you hv to make that String from Ruby , and that's exactly what is json is used for too , check your Json output,
btw try echo some Ruby String like
=> 1,2,3,4,5 [<% echo str %> ]
and if your json out put hv no squre brackets [] then try adding them
data:[ <% #user.weigh_ins.each do |weight| %> ]

Resources