ruby-on-railsruby-on-rails-3ruby-on-rails-4chartkick

how to create line chart and appear data in rails?


I have column called speed in table Gspeed. I need to create a line chart group by minute (created_at) and point appear speed.

For example p1 (110.4) , p2 (113.5), and more.

There can be many values of speed.

I have two models: user and Gspeed. Gspeed contains name type string , speed type float.

Example of database:

db image

appear speed for only current user

Example of shape

image of chart

when try with code in controller @line_chart_data =Gspeed.all.group_by_minute(:created_at).average(:speed)

in view

appear only value

shape


Solution

  • Try if this can fit your request. Maybe you need to manipulate further the array of data.

    Build in the controller the set of data (I used average, but it's up to you):

    @line_chart_data = User.all.map { |user|
        {name: user.name, data: user.gspeeds.group_by_minute(:created_at).average(:speed)}
        }
    

    Then draw the graph in the view:

    <%= line_chart @line_chart_data %>