Gruff 绘图库
[size=medium]Gruff 是Geoffrey Grosenbach開發的圖表繪制gem.
文檔: [url=http://gruff.rubyforge.org/]http://gruff.rubyforge.org/[/url]
安裝: 需要ImageMagick 和 RMagick. 安裝完畢可以用Gruff主頁上的Code Sample測試一下是否安裝成功. 我運行的時候字體有些問題,gruff采用了自帶的font, 在自己的機器上跑的時候需要指定一下font的路徑:
g.font = File.expand_path('/your/font/path/yourfont.ttf')
Gruff 提供了很多有趣的圖類:
[list]
[*]Gruff::Line 就一般的線圖 股市行情什么的可以用用
[*]Gruff::Pie Pie圖 市場份額一類的比較方便
[*]Gruff::Bar 直方圖 年份變化一類都可用
[*]Gruff:Area 面積圖 似乎用處不大
[*]Gruff:Scene 場景圖 這個比較有趣 在文檔提供的例子中:
[/list]
g = Gruff::Scene.new("500x100", "path/to/city_scene_directory")
# Define order of layers, back to front 設定圖像層所在的目錄
g.layers = %w(background haze sky clouds)
# Define groups that will be controlled by the same input 可以將受同一控制的層
組成組
g.weather_group = %w(clouds)
g.time_group = %w(background sky)
# Set values for the layers or groups 設定控制輸入 對其進行匹配選擇圖片
g.weather = "cloudy"
g.time = Time.now
g.haze = true
# Write the final graph to disk
g.write "hazy_daytime_city_scene.png"
這個可以做背景圖的實時控制 比如早晚太陽的不同位置
[list]
[*]Gruff::Spider 這是我的應用中需要的圖片類型 中文叫相圖吧 Gruff::Net和這個類似 差
別在于只標識出包絡線 相圖標識了所圍成的區域 下面就以這個類別為例說說如何在rails
中用Gruff
[/list]
我的應用是大概就是在用戶口味的統計數據上進行相圖繪制 作為用戶資料的一個部分 因
此把相圖繪制寫在了model里面而不是在controller里 由于需要僅僅被本類調用 設置為
protected方法 由于用戶每更新或者創建的時候都重新繪制相表 因此設定了相應的
callback 同時在用戶注銷的時候刪除plot
require 'gruff'
class User < ActiveRecord::Base
after_save :spider_plot
after_create :spider_plot
after_update :spider_plot
after_destroy :spider_plot_destroy
protected
def spider_plot
# plotting
end
def spider_plot_destroy
# delete the plot
end
end
在controller中的代碼傳遞了圖像所在的路徑給view 在view中間就直接用image_tag顯示了
最后來張無恥的圖
[img]/upload/attachment/106200/bf16aa3a-005a-3059-9589-c5ba6cab051f.png[/img]
[/size]
文檔: [url=http://gruff.rubyforge.org/]http://gruff.rubyforge.org/[/url]
安裝: 需要ImageMagick 和 RMagick. 安裝完畢可以用Gruff主頁上的Code Sample測試一下是否安裝成功. 我運行的時候字體有些問題,gruff采用了自帶的font, 在自己的機器上跑的時候需要指定一下font的路徑:
g.font = File.expand_path('/your/font/path/yourfont.ttf')
Gruff 提供了很多有趣的圖類:
[list]
[*]Gruff::Line 就一般的線圖 股市行情什么的可以用用
[*]Gruff::Pie Pie圖 市場份額一類的比較方便
[*]Gruff::Bar 直方圖 年份變化一類都可用
[*]Gruff:Area 面積圖 似乎用處不大
[*]Gruff:Scene 場景圖 這個比較有趣 在文檔提供的例子中:
[/list]
g = Gruff::Scene.new("500x100", "path/to/city_scene_directory")
# Define order of layers, back to front 設定圖像層所在的目錄
g.layers = %w(background haze sky clouds)
# Define groups that will be controlled by the same input 可以將受同一控制的層
組成組
g.weather_group = %w(clouds)
g.time_group = %w(background sky)
# Set values for the layers or groups 設定控制輸入 對其進行匹配選擇圖片
g.weather = "cloudy"
g.time = Time.now
g.haze = true
# Write the final graph to disk
g.write "hazy_daytime_city_scene.png"
這個可以做背景圖的實時控制 比如早晚太陽的不同位置
[list]
[*]Gruff::Spider 這是我的應用中需要的圖片類型 中文叫相圖吧 Gruff::Net和這個類似 差
別在于只標識出包絡線 相圖標識了所圍成的區域 下面就以這個類別為例說說如何在rails
中用Gruff
[/list]
我的應用是大概就是在用戶口味的統計數據上進行相圖繪制 作為用戶資料的一個部分 因
此把相圖繪制寫在了model里面而不是在controller里 由于需要僅僅被本類調用 設置為
protected方法 由于用戶每更新或者創建的時候都重新繪制相表 因此設定了相應的
callback 同時在用戶注銷的時候刪除plot
require 'gruff'
class User < ActiveRecord::Base
after_save :spider_plot
after_create :spider_plot
after_update :spider_plot
after_destroy :spider_plot_destroy
protected
def spider_plot
# plotting
end
def spider_plot_destroy
# delete the plot
end
end
在controller中的代碼傳遞了圖像所在的路徑給view 在view中間就直接用image_tag顯示了
最后來張無恥的圖
[img]/upload/attachment/106200/bf16aa3a-005a-3059-9589-c5ba6cab051f.png[/img]
[/size]
總結
- 上一篇: 百家名企网上招聘地址
- 下一篇: 电脑显示器如何实现分屏 一台主机两台显