如何用Gruff生成图表
手工轉自: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
總結
以上是生活随笔為你收集整理的如何用Gruff生成图表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gruff: Rails 经典图表生成库
- 下一篇: 招聘 风波