Draw an extra line in the chartkick (for rails)

I got a line_chart line representing a dataset.

<%= 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 that goes through the whole graph and tells me the average of my data. Does anyone know if there is an option for this? I tried to draw a chart with two lines, where one line is made up of actual data and the other is made up of a single value repeated as many times to draw a line. But I just couldn't figure out how to do it in code ...

Somebody knows?

+3


source to share


3 answers


I had the same problem and found a solution. Here's 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 the user's heart pressure. I also wanted to show the line with its ideal value. I am using a class called Statistics that contains the presented methods. Both methods create a Hash {date =>} value, but in the second one value is constant. To create a graph with two lines, do the following:

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

      

+2


source


I had the same problem and ended up using highchart and there is a great lazyhighchart to help you create amazing js charts in rails env



0


source


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}]  

      

0


source







All Articles