簡體   English   中英

通過ChartKick使用模型(Rails5)

[英]Using a Model with ChartKick (Rails5)

嘗試使用Google Graphs中的時間線圖(Ala ChartKick Gem

似乎想要一個數據數組,例如

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

我對如何從模型的數據中獲取數據感到困惑? 在這種情況下,我希望使用“ Proposal以及“開始start_date和“ completion_date字段。 兩者均存儲在典型的時間戳中。

我嘗試遍歷each但在Error Loading Chart: Cannot read property 'match' of undefined上遇到錯誤Error Loading Chart: Cannot read property 'match' of undefined

我相信我將需要在控制器內創建一個函數,但是我不確定如何以正確的格式返回數據,或者甚至如何引用控制器。

在您的控制器中,執行(我不知道您的型號名稱)

1)如果您要在數據庫中保存DateTime對象

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

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')]}

在您看來,

<%= timeline @array %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM