I improved the * family todo list* created last time and tried to draw a graph.
main.rb
require 'sinatra'
require 'sinatra/reloader'
require 'date'
require 'active_record'
require 'rack/csrf'
require 'chartkick' #add to
bash.sh
$ ls public/
chartkick.js main.js style.css
Put chartkick.js
in public
main.rb
get '/' do
con = ActiveRecord::Base.connection
dat = con.execute("select users.name, scores.score, scores.point from users inner join scores on users.id = scores.user_id where scores.flag=0 order by users.name, scores.point")
h = {name: '', data: {}}
@graph_data = []
tname = ''
dat.each do |x|
if x["name"] == tname
h[:data][x["point"]] = x["score"]
else
tname = x["name"]
if h[:name] != ''
@graph_data << h.dup
end
h[:name] = x["name"]
h[:data] = {}
h[:data][x["point"]] = x["score"]
end
end
@graph_data << h.dup
erb :index
end
Originally, you should use ʻActiveRecord well, but you can get the data by typing
SQL` directly.
index.erb
<%= line_chart @graph_data, min: 40, max: 100 %>
It is thanks to gem
that it is drawn beautifully.
Referenced site Create beautiful JavaScript charts with one line of Ruby
Recommended Posts