I was developing an app that records learning this time, and I want to graph the learning situation! I thought it was relatively easy to implement, so I will leave it as a memorandum. The author will implement it using rails6. Since it is a beginner, I would appreciate it if you could point out any mistakes!
To easily implement the graph function, use gem's chartkick. Add the following to the gem file and bundle install. groupdate is introduced because there is a convenient method that can group and retrieve various information from DB, but if you only use chartkick, gem "chartkick" is fine.
gem "chartkick"
gem "groupdate"
For rails6, execute the following command
yarn add chartkick chart.js
In chartkick, js file is described in app/javascript/packs/application.js for implementation using js. For rails6
require("chartkick")
require("chart.js")
For rails5
//= require chartkick
//= require Chart.bundle
If you go so far, you can easily create a graph just by writing the sample code on the official page in html. In my case I used it like this. <%= column_chart @tweets.group_by_day_of_week(:created_at, format: "%a").count %> https://gyazo.com/4c24263ba93c7e3bc2ea6974bfb0143f Here group_by_day_of_week is a method that can be used if the gem groupdate is installed. I used a bar graph this time, but you can easily implement line graphs and pie charts, so please take a look at chartkick's official website! !!
Recommended Posts