2011-10-20 162 views
0

我有這個問題。我認爲這很簡單,但我無法做到。使用gruff.I 林建築圖表和表格從月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") 

=> [[ 「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") 

=> [[ 「10」,2]]

試圖追蹤哪裏出錯..但跑出了主意。 請向我指出一些東西。我可以粘貼圖表,但即時通訊不符合條件還說stackoverflow。 ;)

謝謝你

回答

0

行了。

@keys.collect {|k,v| @df[v].nil? ? 0 : @df[v] 

@keys是未排序的順序。所以先簡單地排序它。

@keys.sort.collect {|k,v| @df[v].nil? ? 0 : @df[v] 

謝謝大家。 :))))))對不起,如果有任何。 ;)