Charts with Ruby
Hey there, Have you ever heard about gruff ? Its a nice library for making beautiful graphs out of data from ruby.
Install
The name of the gem is gruff
so add gem 'gruff'
to your Gemfile and execute bundle install
Use it
I am going to build a line chart of counted data. The Data is managed by DataMapper The Table looks like this:
Counter | Date |
---|---|
250 | 2014-06-01 |
200 | 2014-05-01 |
… | … |
require 'gruff'
def gen_graph(table,color,size)
label_index
g = Gruff:Line.new(size)
g.marker_font_size=15
g.theme = {
:colors => [color],
:maker_color => '#CCCCCC',
:font_color => 'Black',
:background_color => 'White'
}
g.title=table.to_s + " Counter"
data = []
table.all.each do |row|
data.push(row['counter'])
end
g.data table, data
system("mkdir -p public/graphs")
g.write("public/graphs/"+table.to_s.downcase+".png")
end
size=700
gen_graph(Water,"Blue",size)
gen_graph(Power,"Red",size)
gen_graph(Gas,"Yellow",size)