chartkick is a library that makes it very easy to create graphs.
Ruby 2.5.3 Ruby on Rails 5.2.4 chartkick 3.3.1
Gemfile
gem "chartkick"
$ bundle install
app/javascripts/application.js
//= require chartkick
//= require Chart.bundle
I'm ready. You may want to shut down the server and start over with rails s.
ID, width, height
<%= line_chart data, id: "users-chart", width: "800px", height: "500px" %>
Axis title
<%= line_chart data, xtitle: "Time", ytitle: "Population" %>
Combining these makes it look like this.
Prepare sales (result) and sales date (result_date) in managemant,
app/controllers/managemants_controller.rb
def index
@managemants = Managemant.all
end
app/models/managemant.rb
#Data for chart kick
def self.chart_date
order(result_date: :asc).pluck('result_date', 'result').to_h
end
app/views/managemants/index.html.erb
<%= column_chart @managemants.chart_date, xtitle: "date", ytitle: "Earnings(Circle)", width: "600px", height: "200px" %>
You can change it to various graphs just by changing the column_chart part of the view. -Line graph-line_chart
Pie chart-pie_chart · Bar chart-pie_chart
Area chart-area_chart
You can easily create a beautiful graph, so please give it a try.
https://chartkick.com/
Recommended Posts