简体   繁体   English

通过ChartKick使用模型(Rails5)

[英]Using a Model with ChartKick (Rails5)

Trying to use the Timeline Graph from Google Graphs (Ala ChartKick Gem 尝试使用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. 在这种情况下,我希望使用“ Proposal以及“开始start_date和“ completion_date字段。 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 我尝试遍历each但在Error Loading Chart: Cannot read property 'match' of undefined上遇到错误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 1)如果您要在数据库中保存DateTime对象

@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 2)如果要保存整数时间戳记

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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