Gruff和缓存图表
生活随笔
收集整理的這篇文章主要介紹了
Gruff和缓存图表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前已經介紹了一些關于Gruff的基礎知識和應用,當然gruff的圖表是生成的,當時生產,就存在緩存的問題。那么,我們看一個Gruff緩存的例子:
[b]
1. 安裝插件[/b]
sudo gem install gruff
[b]2. Rails中進行配置 [/b]
Rails::Initializer.run do |config|
config.gem 'gruff', :lib => 'gruff', :version => '0.3.4
end
[b]3. 在Controller的調用[/b]
class GruffController < PortalController
append_before_filter :load_graph,
:only => [total_orders_graph, :top_sellers_graph]
caches_action :recent_orders_graph, :expires_in => 1.day.to_i
def total_orders_graph
orders_graph = @graph.generate_total_orders
send_data orders_graph.to_blob , :type => 'image/png'
end
private
def load_graph
@graph = Graphs::ClientGraph.new(@CURRENT_CLIENT)
end
end
[b]4. 在model中進行cache[/b]
class Graphs::ClientGraph
def initialize(client)
@client = client
end
def generate_total_orders
total_orders_graph = Gruff::Bar.new
total_orders_graph.title = "Orders"
dates = working_week
total_orders_graph.labels = Hash[*dates.collect{|date|[dates.index(date), date.strftime("%d %b")]}.flatten]
datasets = []
for date in dates
datasets.push(@client.orders.on_day
(date.beginning_of_day,date.end_of_day).count)
end
total_orders_graph.data(:total_orders,datasets, '#009900')
total_orders_graph.x_axis_label = 'Date'
total_orders_graph.y_axis_label = "Total"
total_orders_graph.hide_legend = true
total_orders_graph
end
private
def working_week
days_in_a_week = 7
dates = []
days_in_a_week.times do |day_value|
date = Date.today - day_value.days
dates.push date if date.wday != 0 && date.wday != 6
end
dates.reverse
end
end
[b]5. Html的顯示[/b]
<%= image_tag( url_for(:action => :total_orders_graph, :controller => :client_dashboard), :size => '500x400') -%>
[b]6. 生成效果如下[/b]
[img]/upload/attachment/131711/0ab5b269-07de-3e92-ac7c-5d6774afcca6.png[/img]
[b]
1. 安裝插件[/b]
sudo gem install gruff
[b]2. Rails中進行配置 [/b]
Rails::Initializer.run do |config|
config.gem 'gruff', :lib => 'gruff', :version => '0.3.4
end
[b]3. 在Controller的調用[/b]
class GruffController < PortalController
append_before_filter :load_graph,
:only => [total_orders_graph, :top_sellers_graph]
caches_action :recent_orders_graph, :expires_in => 1.day.to_i
def total_orders_graph
orders_graph = @graph.generate_total_orders
send_data orders_graph.to_blob , :type => 'image/png'
end
private
def load_graph
@graph = Graphs::ClientGraph.new(@CURRENT_CLIENT)
end
end
[b]4. 在model中進行cache[/b]
class Graphs::ClientGraph
def initialize(client)
@client = client
end
def generate_total_orders
total_orders_graph = Gruff::Bar.new
total_orders_graph.title = "Orders"
dates = working_week
total_orders_graph.labels = Hash[*dates.collect{|date|[dates.index(date), date.strftime("%d %b")]}.flatten]
datasets = []
for date in dates
datasets.push(@client.orders.on_day
(date.beginning_of_day,date.end_of_day).count)
end
total_orders_graph.data(:total_orders,datasets, '#009900')
total_orders_graph.x_axis_label = 'Date'
total_orders_graph.y_axis_label = "Total"
total_orders_graph.hide_legend = true
total_orders_graph
end
private
def working_week
days_in_a_week = 7
dates = []
days_in_a_week.times do |day_value|
date = Date.today - day_value.days
dates.push date if date.wday != 0 && date.wday != 6
end
dates.reverse
end
end
[b]5. Html的顯示[/b]
<%= image_tag( url_for(:action => :total_orders_graph, :controller => :client_dashboard), :size => '500x400') -%>
[b]6. 生成效果如下[/b]
[img]/upload/attachment/131711/0ab5b269-07de-3e92-ac7c-5d6774afcca6.png[/img]
總結
以上是生活随笔為你收集整理的Gruff和缓存图表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web3j 操作
- 下一篇: faster rcnn 训练自己的数据集