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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Gruff和缓存图表

發(fā)布時間:2024/1/1 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Gruff和缓存图表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前已經(jīng)介紹了一些關(guān)于Gruff的基礎(chǔ)知識和應(yīng)用,當然gruff的圖表是生成的,當時生產(chǎn),就存在緩存的問題。那么,我們看一個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的調(diào)用[/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]

總結(jié)

以上是生活随笔為你收集整理的Gruff和缓存图表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。