Skip to content

Instantly share code, notes, and snippets.

@lenagroeger
lenagroeger / fastercsv
Created August 28, 2012 18:38
Faster CSV for bar graphs
require 'rubygems'
require 'fastercsv'
data = []
html = <<-HTML
HTML
FasterCSV.foreach("fda.csv", :headers => true) do |row|
data << row
@lenagroeger
lenagroeger / templ
Created August 28, 2012 18:58
tooltip templ
<script id="drone-templ" type="text/jst">
<div id="drones-source">According to: <%=source%></div>
<div id="drones-claim"><%=claim%></div>
<div id="drones-outlet">– <%=outlet%></div>
</script>
<script type="text/javascript">
$(document).ready(function() {
var stateNames = {
"AL":"Alabama",
"AK":"Alaska",
"AZ":"Arizona",
"AR":"Arkansas",
"CA":"California",
"CO":"Colorado",
"CT":"Connecticut",
"DE":"Delaware",
"DC":"District of Columbia",
@lenagroeger
lenagroeger / timeline bars
Created August 28, 2012 19:04
generating start dates for bars on a horizontal timeline
data.each do |row|
el=<<-HTML
<div class="drones-bar" style="width:#{(row["duration"].to_f/5)}px; margin-left: #{(row["days_from_start"].to_f/5)}px;" data-id="#{row["number"]}"><p class="deaths"><a href="#{row["link"]}">"#{row["deaths"]}"</a></p></div>
HTML
html << el
end
@lenagroeger
lenagroeger / fmt_stat (helper)
Created August 28, 2012 19:06
helper to turn $5,758,937 into $5M
def fmt_stat(stat, prefix = "$")
stat = case stat
when -999999999999..-10000 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when -9999..9999 then number_with_delimiter(stat)
when 10000..999999999999 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when nil then number_with_delimiter(0)
else stat
end
stat = stat === "N/A" ? stat : "#{prefix}#{stat}"
end
@lenagroeger
lenagroeger / sed find replace
Created August 28, 2012 19:08
mass find and replace
sed -i '' -e's/\*\*NOTE-\ TERMS\ IN\ BRACKETS\ HAVE\ BEEN\ EDITED\ TO\ PROTECT\ CONFIDENTIALITY\*\*/ /g' regionALL.csv
@lenagroeger
lenagroeger / cat csv
Created August 28, 2012 19:09
cat csv files together
cat reg1.csv reg2.csv reg3.csv reg4.csv reg5.csv reg6.csv reg7.csv reg8.csv reg9.csv reg10.csv > regionALL.csv
@lenagroeger
lenagroeger / map click function
Created August 29, 2012 19:23
homeowner map click/show function
CSS:
.blurb {
width:200px;
height:60px;
background:#444;
color:white;
display:none;
}
HTML:
@lenagroeger
lenagroeger / fmt_stat frack
Created September 11, 2012 21:12
fmt_stat for fracking
def fmt_stat(stat, prefix = stat > 0 ? "$" : "")
stat = case stat
when -999999999999..-10000 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when -9999..-1 then number_with_delimiter(stat)
when 0 then "<em>No Data.</em>"
when 1..9999 then number_with_delimiter(stat)
when 10000..999999999999 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when nil then number_with_delimiter(0)
else stat
end
@lenagroeger
lenagroeger / statefaceize
Created September 14, 2012 19:00
stateface helper
<link rel="stylesheet" href="http://propublica.org/static/stateface/stateface.css" type="text/css" charset="utf-8" />
STATEFACE = {
"USA" => "z",
"AL" => "B",
"AK" => "A",
"AZ" => "D",
"AR" => "C",
"CA" => "E",