如何用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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值