I have this problem. i think it is simple but i just couldn’t get it right.
Im building graph and table using gruff.I have a x axis scaled from month 01 to 12
The data shud hv bn plotted in month 09 and 10 but it is plotted to month 08 and 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)
here are some explanation
the output for
@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]]
trying to trace where went wrong..but ran out of idea.
please point something out to me.wish i could paste the graph but im not eligible yet say stackoverflow. 😉
thank u
OK got it.
the @keys are in unsorted order.So simply sort it first.
Thank you all. :))))) Sorry if anything. 😉