Skip to content

Instantly share code, notes, and snippets.

@zackster
Created November 21, 2016 17:47
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 zackster/0e45d24314c8d4dcca44a7da0b61f159 to your computer and use it in GitHub Desktop.
Save zackster/0e45d24314c8d4dcca44a7da0b61f159 to your computer and use it in GitHub Desktop.
Zidisha growth charts
require 'open-uri'
require 'nokogiri'
require 'groupdate'
require 'gruff'
nok = Nokogiri(open("https://www.zidisha.org/reports/loans-funded"))
countries = nok.search("select[name='country']").search('option').map{|x|[x.children.text, x.attributes['value'].value.to_sym]}.reject{|x|x.first.blank?}
countries.each do |country, code|
begin
nok = Nokogiri(open("https://www.zidisha.org/reports/loans-funded?fromDate=10%2F17%2F2010&toDate=10%2F23%2F2020&country=#{code.to_s}&newBorrowersOnly=on&Submit=Submit"))
puts "Grabbed data for #{country}"
user_rows = nok.search('tr')[1..-1].map{|tr| tr.search('td').map{|x|x.text} }
graph_data = user_rows.group_by_week{|u| Date.parse(u[3]) }.map{|k,v| [k,v.size]}
g = Gruff::Line.new(1600)
g.title = "#{country} Growth"
g.marker_font_size = 8
g.dot_radius = 2
graph_labels = {}
i = 0
line_frequency = (graph_data.size / 10).ceil
if line_frequency.zero?
line_frequency = 1
end
graph_data.each_with_index do |arr,idx|
if idx % line_frequency == 0
graph_labels[i] = arr.first.strftime("%b %d %Y")
else
graph_labels[i] = nil
end
i+=1
end
g.labels = graph_labels
g.data(country.to_sym, graph_data.map{|a|a.last})
g.write("zidisha/zidisha_growth_#{code}.png")
puts "Wrote graph for #{country}"
rescue
puts "Could not generate graph for #{country}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment