手工转自:http://blog.csdn.net/cnvisual/article/details/5958811
Gruff 是使用ruby编写的Ruby图标库,可以创建包括直线图,柱状图,饼状图在内的多种图表.
Github:http://github.com/topfunky/gruff
安装:
Ruby代码
gem install gruff
[Ruby] view plaincopy
gem install gruff
(1)直线图
Ruby代码
#请注意,这个文件千万不能命名为gruff.rb,否则后患无穷: uninitialized require 'rubygems'
require 'gruff'
g = Gruff::Line.new
g.title = "My Graph"
g.font = File .expand_path( 'c:/WINNT/Fonts/simsun.ttf' )
#指定一下font的路径,以便使用中文
g.data("火星組" , [1, 2, 3, 4, 4, 3])
g.data("水星組" , [4, 8, 7, 9, 8, 9])
g.data("土星組" , [2, 3, 1, 5, 6, 8])
g.data("木星組" , [9, 9, 10, 8, 7, 9])
g.labels = {0 => '2006' , 2 => '2008' , 4 => '2010' }
g.write('greport.png' )
[Ruby] view plaincopy
#请注意,这个文件千万不能命名为gruff.rb,否则后患无穷: uninitialized require 'rubygems'
require 'gruff'
g = Gruff::Line.new
g.title = "My Graph"
g.font = File.expand_path('c:/WINNT/Fonts/simsun.ttf')
#指定一下font的路径,以便使用中文
g.data("火星組", [1, 2, 3, 4, 4, 3])
g.data("水星組", [4, 8, 7, 9, 8, 9])
g.data("土星組", [2, 3, 1, 5, 6, 8])
g.data("木星組", [9, 9, 10, 8, 7, 9])
g.labels = {0 => '2006', 2 => '2008', 4 => '2010'}
g.write('greport.png')
会生成下面这张图:
(2)餅狀圖
只需修改上面代碼的Gruff::Line.new為Gruff::Pie.new,即可生成下圖:
詳細可參考:http://gruff.rubyforge.org/
用户还可以指定图标的颜色,背景图案甚至字体.
先來看看顏色吧:
Ruby代码
add_color( '#c0e9d3' )
[Ruby] view plaincopy
add_color('#c0e9d3')
參考其源碼:
Ruby代码
def add_color(colorname)
@colors << colorname
end
[Ruby] view plaincopy
def add_color(colorname)
@colors << colorname
end
針對上面的代碼,可以這樣處理:
Ruby代码
g.data( "火星組" , [1, 2, 3, 4, 4, 3], '#c0e9d3' )
[Ruby] view plaincopy
g.data("火星組", [1, 2, 3, 4, 4, 3],'#c0e9d3')
至于背景圖片,可以參考下面代碼:
Ruby代码
graph.theme = {
:colors => %w(orange purple green white red),
:marker_color => 'blue' ,
:background_colors => %w(black grey),
:background_image => 'squirrel.png'
}
[Ruby] view plaincopy
graph.theme = {
:colors => %w(orange purple green white red),
:marker_color => 'blue',
:background_colors => %w(black grey),
:background_image =>'squirrel.png'
}
針對上面例子,我的代碼如下:
Ruby代码
g.theme = {
:background_image => 'cd.png'
}
[Ruby] view plaincopy
g.theme = {
:background_image => 'cd.png'
}
其它不一一敘述,詳細還請參考下面鏈接:
http://gruff.rubyforge.org/
http://nubyonrails.com/pages/gruff
Ruby Gruff的图表功能
http://www.weekface.info/2010/07/25/ruby-gruff-chart