For anyone that uses FusionCharts, this little class may be very helpful.
It is for building xml files to feed FusionCharts 'Stacked' charts, and it has already saved my ass.
class Stacker
include Utils
attr_reader :chart_to_go
attr_accessor :caption
attr_accessor :start_date
attr_accessor :input_records
attr_accessor :date_string
def initialize(caption, start_date, y_axis)
@caption = caption
@start_date = start_date
@y_axis = y_axis
@input_records = []
@games = []
@final = []
@numrecs=30
end
def change_numrecs(x)
@numrecs=x.to_i
end
def read_line(title, count, date, year)
@input_records << [title, count, date, year]
end
def build_date_string(differential)
if differential == 0 then
@date_string = "#{pad_month(@start_date.month)}/#{pad_month(@start_date.day)}/#{@start_date.year}"
else
cur_date = @start_date + differential.days
@date_string = "#{pad_month(cur_date.month)}/#{pad_month(cur_date.day)}/#{cur_date.year}"
end
end
def run
build_query
build_it
end
def build_query
collect_game_types
#step through dates and build new final table
#build date string
for i in 1..@numrecs do
build_date_string(i-1)
#loop through games
@games.each do |look_for_this_game|
game_matched_today = false
#now loop through input recs
@input_records.each do |this_line|
if @date_string == this_line[2] && look_for_this_game == this_line[0]
#exact match
@final << [this_line[0], this_line[1], this_line[2], this_line[3]]
game_matched_today = true
end
end
#end game loop
if !game_matched_today then
@final << [look_for_this_game, 0, @date_string, @date_string.split("/")[2]]
end
end #game loop
end # i loop
end
def collect_game_types
#loop through and collect games
@input_records.each do |this_game_type|
@games << this_game_type[0]
end
@games.uniq!
@games
end
def build_it
@chart_to_go = ""
#set
@chart_to_go << ""
#add categories
for i in 1..@numrecs do
build_date_string(i-1)
@chart_to_go << " "
end
@chart_to_go << " "
#add datasets
@games.each do |look_for_this_game|
@chart_to_go << ""
for i in 1..@numrecs do
build_date_string(i-1)
@chart_to_go << " "
end
@chart_to_go << " "
end
@chart_to_go << " "
end
def final
@final
end
def get_value_for_game_day(game, day)
return_val=0
@final.each do |this_rec|
if this_rec[0] == game && this_rec[2] == day then
return_val = this_rec[1].to_i
end
end
return_val
end
end
>>>>>>>>The only kicker is that you need your own 'pad_month' function to add a pad zero to any single digits in your day.
So get rid of the Utils module and stick in this pad_month method and you should be golden.
def pad_month(this)
if this.to_i < 10
return "0#{this}"
else
return "#{this}"
end
end






