日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何用Gruff生成图表

發布時間:2024/1/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何用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生成图表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。