Skip to content

Instantly share code, notes, and snippets.

@patio11
Created June 3, 2010 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patio11/423485 to your computer and use it in GitHub Desktop.
Save patio11/423485 to your computer and use it in GitHub Desktop.
def open_flash_chart(height, width, data_url, div_name = nil)
options = {}
options[:height] = height
options[:width] = width
options[:swf_file_name] = "flash/open-flash-chart.swf"
options[:div_name] = div_name unless div_name.nil?
open_flash_chart_object_from_hash(data_url, options)
end
def Stat.signups_per_day
Rails.cache.fetch("Stat.signups_per_day", :expires_in => 1.day) do
signups = {}
%w{trial guest registered}.each do |role|
recs = User.find_by_sql(["select date(users.created_on) as date, count(*) as signup_count from users where role = ? group by date order by date ASC", role])
signups[role] = {}
recs.map {|rec| signups[role][rec.date] = rec.signup_count}
end
signups
end
end
def signups_per_day
#irrelevant code elided
@flash_chart_trials = open_flash_chart(550, 530, "/stats/signups-per-day-chart/trial", "trial_signups_chart")
@flash_chart_guests = open_flash_chart(550, 530, "/stats/signups-per-day-chart/guest", "guests_signups_chart")
end
#route is /stats/signups-per-day-chart/:role
def signups_per_day_chart
raise "That is not a valid role!" unless %w{trial guest}.include? params[:role]
title = Title.new("BCC Signups (#{params[:role].titleize.pluralize})")
signups = Stat.signups_per_day[params[:role]]
first_day = Date.parse("2009-07-05")
bar = BarGlass.new
day_values = Rails.cache.fetch("StatController.signups_per_day:#{params[:role]}", :expires_in => 1.day) do
(first_day..(Date.today)).map {|date| (signups[date.to_s] || 0).to_i }
end
maximum_signups = 0
week_values = day_values.in_groups_of(7).map do |week|
signups_in_this_week = week.reject {|a| a.nil?}.sum
maximum_signups = signups_in_this_week if signups_in_this_week > maximum_signups
tooltip_for_this_week = (["Total: #{signups_in_this_week}"] + week.map {|day| day || 0}).join("<br>")
val = BarValue.new(signups_in_this_week)
val.set_tooltip(tooltip_for_this_week)
val
end
bar.values = week_values
x_axis = XAxis.new
x_axis.steps = 7
labels = XAxisLabels.new
day_labels = (first_day..(Date.today)).map {|date| date.to_s }
week_labels = day_labels.in_groups_of(7).map {|week| "#{week.first} ~ #{week.last}"}
labels.labels = week_labels
labels.set_vertical
x_axis.labels = labels
y_axis = YAxis.new
scaling_factor = 100
maximum_signups = ((maximum_signups / scaling_factor) + 1) * scaling_factor
y_axis.set_range(0, maximum_signups, scaling_factor)
chart = OpenFlashChart.new
chart.set_title(title)
chart.y_axis = y_axis
chart.x_axis = x_axis
chart.add_element bar
render :text => chart.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment