draw an additional line in chartkick (for rails)

1.2k Views Asked by At

I got a line_chart form a punch of data.

<%= line_chart @images.group(:imagedatetime).average(:exposureindex), library: {discrete: true, pointSize: 1, lineWidth: 0, hAxis: {type: "category"}} %>

and now i want to have an extra line which goes through the whole chart and indicates me the average value of my data. Does anybody know if there is an option for that? I tried to draw a chart with two lines where as one line is made up from the actual data and the other line is made from a single value repeated as many times to draw a line. But I just couldn't figure out how to put that into code...

Does anyone has a clue?

3

There are 3 best solutions below

0
On

I had the same problem, and I found a solution. Here is my example:

def systolic
  data.group_by_day(:date).maximum(:systolic)
end

def ideal_diastolic
  Hash[data.map{|data| [Date.parse(data.date.to_s), 90]}]
end

I have some health data objects, with user heart pressure. I wanted to show also a line with ideal value of it. I use class called Statistics that contains presented methods. Both methods creates a Hash {date => value} but in second one value is constant. To generate graph with two lines do this:

 = line_chart [ {name: "systolic", data: @statistics.systolic}, {name:"ideal systolic", data: @statistics.ideal_systolic}]
1
On

i had the same problem and i ended up using highchart, and there is a great gem lazyhighchart that helps you create amazing js graphs in rails env

0
On
line_chart   [ {name: "Registered users", data: User.get_users_only.order("DATE(created_at)").group("DATE(created_at)").count}, {name:"Guest users", data: User.get_guest_users_only.order("DATE(created_at)").group("DATE(created_at)").count}]