Class: USAmap

Inherits:
Object
  • Object
show all
Includes:
UsaCountiesSvg
Defined in:
lib/usamap.rb

Overview

Author:

Instance Method Summary collapse

Methods included from UsaCountiesSvg

svg

Constructor Details

#initialize(stats_h = {}, colors_h = {}) ⇒ USAmap

Returns a new instance of USAmap.

Parameters:

  • stats_h (Hash) (defaults to: {})

    stats to plot by county FIPS value eg stats_h [ā€˜35001ā€™] = 3

  • colors_h (Hash) (defaults to: {})

    colors to plot for numeric ranges eg colors_h [0..5] = ā€˜#CD5C5Cā€™



8
9
10
# File 'lib/usamap.rb', line 8

def initialize(stats_h = {}, colors_h = {})
  @stats_h, @colors_h = stats_h, colors_h
end

Instance Method Details

#to_sObject

Returns:

A colorized SVG (Scalable Vector Graphics) map of the USA



14
15
16
17
18
19
20
21
22
# File 'lib/usamap.rb', line 14

def to_s
  css = '' 
  @stats_h.each do |fips, val|
    colr = @colors_h.select { |range, hex| range.member?(val) }.values.first  
    css << ".c#{fips} {fill:#{colr}}\n"
  end
  svg = UsaCountiesSvg.svg
  css.empty? ? svg : svg.gsub!(/\/\*CSS insertion point\*\//, "/*CSS insertion point*/\n#{css}") 
end