如何用Gruff生成图表

Gruff 是使用ruby编写的Ruby图标库,可以创建包括直线图,柱状图,饼状图在内的多种图表.

Github:http://github.com/topfunky/gruff

安装:

Ruby代码
  1. gem install gruff  



(1)直线图

Ruby代码
  1. #请注意,这个文件千万不能命名为gruff.rb,否则后患无穷: uninitialized require 'rubygems'   
  2. require 'gruff'   
  3.   
  4. g = Gruff::Line.new   
  5. g.title = "My Graph"   
  6. g.font = File .expand_path( 'c:/WINNT/Fonts/simsun.ttf' )   
  7. #指定一下font的路径,以便使用中文   
  8. g.data("火星組" , [1, 2, 3, 4, 4, 3])  
  9. g.data("水星組" , [4, 8, 7, 9, 8, 9])  
  10. g.data("土星組" , [2, 3, 1, 5, 6, 8])  
  11. g.data("木星組" , [9, 9, 10, 8, 7, 9])  
  12.   
  13. g.labels = {0 => '2006' , 2 =>  '2008' , 4 =>  '2010' }  
  14.   
  15. g.write('greport.png' )  



会生成下面这张图:


(2)餅狀圖
只需修改上面代碼的Gruff::Line.new為Gruff::Pie.new,即可生成下圖:
詳細可參考:http://gruff.rubyforge.org/




用户还可以指定图标的颜色,背景图案甚至字体.

先來看看顏色吧:

Ruby代码
  1. add_color( '#c0e9d3' )  



參考其源碼:

Ruby代码
  1. def  add_color(colorname)  
  2.       @colors  << colorname  
  3.  end   



針對上面的代碼,可以這樣處理:

Ruby代码
  1. g.data( "火星組" , [1, 2, 3, 4, 4, 3], '#c0e9d3' )   



至于背景圖片,可以參考下面代碼:

Ruby代码
  1. graph.theme = {  
  2.   :colors  => %w(orange purple green white red),  
  3.   :marker_color  =>  'blue' ,  
  4.   :background_colors  => %w(black grey),  
  5.   :background_image  => 'squirrel.png'   
  6. }  


針對上面例子,我的代碼如下:

Ruby代码
  1. g.theme = {  
  2.    :background_image  =>  'cd.png'   
  3.  }  


其它不一一敘述,詳細還請參考下面鏈接:

http://gruff.rubyforge.org/
http://nubyonrails.com/pages/gruff
Ruby Gruff的图表功能
http://www.weekface.info/2010/07/25/ruby-gruff-chart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值