简体   繁体   中英

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.

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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM