我有这个问题。我认为这很简单,但我无法做对。 我使用gruff建立图形和表格。我有一个x轴从01月到12日缩放 数据shud hv bn在09和10月绘制,但它绘制到08和10月。
def stat1
@yeara = params[:year]
@games = Games.find(:all)
g = Gruff::StackedBar.new('800x450')
g.theme = {
:colors => ['#138F6A','#330000','#ff6600', '#3bb000', '#1e90ff', '#efba00', '#0aaafd'],
:marker_color => '#aaa',
:background_colors => ['#eaeaea', '#fff']
}
g.hide_title = true
@dr = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y')= ?",1,@yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
@df = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y') = ?",2,@yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
# all 12 month a year
@full = Hash["01",0,"02",0,"03",0,"04",0,"05",0,"06",0,"07",0,"08",0,"09",0,"10",0,"11",0,"12",0]
year = (@dr.keys |@df.keys|@full.keys).sort
@keys = Hash[*year.collect {|v| [year.index(v),v.to_s] }.flatten]
# Plot data to table
@dfdr = Array.new
@dfdr << @keys.collect {|k,v| @df[v].nil? ? 0 : @df[v]}
@dfdr << @keys.collect {|k,v| @dr[v].nil? ? 0 : @dr[v]}
# Plot data to graph
g.data("Adam", @keys.collect {|k,v| @dr[v].nil? ? 0 : @dr[v]})
g.data("Eve", @keys.collect {|k,v| @df[v].nil? ? 0 : @df[v]})
g.labels = @keys
g.write("#{RAILS_ROOT}/public/images/game.png")
render(:layout => false)
这里有一些解释
的输出@dr = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y')= ?",1,@yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
=&GT; [[ “09”,3],[ “10”,1]
@df = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y') = ?",2,@yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
=&GT; [[ “10”,2]]
试图追踪出错的地方..但是没想到。 请向我指出一些事情。希望我可以粘贴图表,但我还没有资格说stackoverflow。 ;)
谢谢你
答案 0 :(得分:0)
好的。
@keys.collect {|k,v| @df[v].nil? ? 0 : @df[v]
@keys按未排序顺序。所以只需先排序。
@keys.sort.collect {|k,v| @df[v].nil? ? 0 : @df[v]
谢谢大家。 :)))))抱歉,如果有的话。 ;)