0

I want to display data using chartkick in rails. My data structure is very simple.

    //Table Drinks
    Created_at   Water (in l)    Beer (in l)   Coffee (in l)
    21/02/2016      1                2              1
    20/02/2016      2                1              0
    19/02/2016      2                2              2
    ...

I want to draw three line charts. Each line chart should show the days on the x-axis and the amount of liters on the y-axis (one chart for each kind of drink).

I am just too confused to figure out how to do the right queries.

    <%= line_chart ?????????? %>

Here is the link to the chartkick github repo: https://github.com/ankane/chartkick

Any help is very much appreciated!

1 Answer 1

0

You can use Pluck ActiveRecord function to pluck out data from table in an array. Chartkick supports array as input so it should work fine.

Ex:

<%= line_chart
{name: "Water", data: Drink.pluck(:created_at,:water)}
{name: "Beer", data: Drink.pluck(:created_at,:beer)}
{name: "Coffee", data: Drink.pluck(:created_at,:coffee)}
 %>
1
  • Unfortunately the code doesn't work. Could you post a solution by using collect/map without pluck?
    – Theo
    Commented Jan 25, 2016 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.