I have a model Level that records level values from 8 different water boxes once every time that was defined. So, each level has a :box_id
, a :level_value
and a a :created_at
timestamp. I would like to plot these values in a line_chart where y axis has the :created_at
values for all boxes and the x axis has the levels. There will be 8 series with the level values registered along time.
I am using the Chartkick gem but I am not getting the right chart. My controller is this:
def chart
@levels = Level.all
end
And I am trying to do this on my view:
<%= line_chart @levels.group(:box_id).group(:created_at).count %>
But I am getting this:
It seems that the problem is the count() method, but I did not find a way to do this without this method.
I found the answer by passing name and date to the line_chart.
This is the action on the controller:
def get_all_tanks_graph
@hash = {}
@array = Array.new
@levels = Level.get_all_tanks_levels
@levels.each do |l|
l.each do |i|
@hash[i.created_at] = i.level
end
@array << @hash
@hash = Hash.new
end
render json: @array.each_with_index.map {
|a, index| {
name: "Caixa #{index + 1}", data: a
}
}
end
This is the helper to call the action.
line_chart get_all_tanks_graph_path