Skip to content

Instantly share code, notes, and snippets.

@gilesbowkett
Created January 24, 2010 04:50
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 gilesbowkett/285011 to your computer and use it in GitHub Desktop.
Save gilesbowkett/285011 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'fastercsv'
require 'active_support'
require File.dirname(__FILE__) + "/gruff/lib/gruff" # had some glitches with rubygems gruff
# cargo cult: http://blog.nikosd.com/2008/10/beautiful-bar-charts-with-ruby-and.html
g = Gruff::Bar.new('700x400') # Define a custom size
g.sort = false # Do NOT sort data based on values
g.theme_keynote # Declare a theme from the presets available
g.title = 'Clickbank Sales'
# parse
class Purchase < Struct.new(:date, :currency, :amount, :vendor) ; end
@purchases = (FasterCSV.read("clickbank.csv").collect do |array|
next if "Date" == array[0]
purchase = Purchase.new
purchase.date = array[0]
purchase.currency = array[5]
purchase.amount = array[8]
purchase.vendor = array[9]
purchase
end).compact
# normalize prices
# current as of 01/23/10
exchange_rate_usd_gbp = 1.6116
# http://www.ruby-forum.com/topic/132357
class Numeric
def round_to(decimals)
factor = 10.0 ** decimals
(self * factor).round / factor
end
end
@purchases.each do |purchase|
purchase.amount = purchase.amount.gsub(/\$/, "").to_f
if "GBP" == purchase.currency
purchase.amount = (exchange_rate_usd_gbp * purchase.amount).round_to(2)
purchase.currency = "USD"
end
end
# if it isn't GBP or USD, then I'm just fucked.
lazy = @purchases.select{|purchase| "LAZYMRKETR" == purchase.vendor}.sort_by &:date
making = @purchases.select{|purchase| "MAKINGUP" == purchase.vendor}.sort_by &:date
# bars.collect{|bar| [bar.name, bar.values(:order_by => :date)]}
[
['LAZY', lazy.map(&:amount)],
['MAKING', making.map(&:amount)]
].each do |bar|
g.data(bar[0], bar[1])
end
g.write('clickbank.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment