DNorthrup
DNorthrup

Reputation: 847

Using a Model with ChartKick (Rails5)

Trying to use the Timeline Graph from Google Graphs (Ala ChartKick Gem

It seems to be want an Array of data such as

<%= timeline [
  ["Washington", "1789-04-29", "1797-03-03"],
  ["Adams", "1797-03-03", "1801-03-03"],
  ["Jefferson", "1801-03-03", "1809-03-03"]
] %>

I'm a bit confused on how I could get it to fetch from a Model's data? In this case I would like the Proposal and the start_date and completion_date fields. Both are stored in typical timestamps.

I tried iterating through an each but I get an error on Error Loading Chart: Cannot read property 'match' of undefined

I believe I will need to make a function within the controller but I'm not sure how to get return the data in the proper format, or how to even reference the controller.

Upvotes: 0

Views: 139

Answers (1)

Sajin
Sajin

Reputation: 1638

In your controller, do (I don't know your model name)

1) If you are saving DateTime object in your db

@array = Model.all.map{|a| [a.proposal, a.start_date.strftime('%Y-%m-%d'), a.completion_date.strftime('%Y-%m-%d')]}

2) If you are saving integer timestamp

@array = Model.all.map{|a| [a.proposal, DateTime.strptime(a.start_date.to_s,'%s').strftime('%Y-%m-%d'), DateTime.strptime(a.completion_date.to_s,'%s').strftime('%Y-%m-%d')]}

and in your view, do

<%= timeline @array %>

Upvotes: 1

Related Questions