Class: Seer::Geomap

Inherits:
Object
  • Object
show all
Includes:
Chart
Defined in:
lib/seer/geomap.rb

Overview

Geomap creates a map of a country, continent, or region, with colors and values assigned to specific regions. Values are displayed as a color scale, and you can specify optional hovertext for regions.

USAGE=

In your view:

<div id="my_geomap_container" class="chart"></div>

<%= Seer::visualize(
      @locations,
      :as => :geomap,
      :in_element => 'my_geomap_container',
      :series => {
        :series_label => 'name',
        :data_label => '# widgets',
        :data_method => 'widget_count'
      },
      :chart_options => {
        :data_mode => 'regions',
        :region => 'US',
      }  
    )
-%>

@locations==

A collection of objects (ActiveRecord or otherwise) that must respond to the following methods:

latitude  # => returns the latitude in decimal format
longitude # => returns the longitude in decimal format
geocoded? # => result of latitude && longitude

For details on the chart options, see the Google API docs at code.google.com/apis/visualization/documentation/gallery/geomap.html

Constant Summary collapse

COUNTRY_CODES =
['world', 'AX', 'AF', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CD', 'CG', 'CK', 'CR', 'CI', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GN', 'GW', 'GY', 'HT', 'HM', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IL', 'IT', 'JM', 'JP', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'SH', 'KN', 'LC', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'CS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'us_metro', 'UM', 'UY', 'UZ', 'VU', 'VA', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW', '005', '013', '021', '002', '017', '015', '018', '030', '034', '035', '143', '145', '151', '154', '155', '039']

Constants included from Chart

Chart::DEFAULT_COLORS, Chart::DEFAULT_HEIGHT, Chart::DEFAULT_LEGEND_LOCATION, Chart::DEFAULT_WIDTH

Instance Attribute Summary collapse

Attributes included from Chart

#chart_element, #colors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chart

#in_element=, #options

Constructor Details

#initialize(args = {}) ⇒ Geomap

:nodoc:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/seer/geomap.rb', line 53

def initialize(args={}) #:nodoc:

  # Standard options
  args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }

  # Chart options
  args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }

  # Handle defaults
  @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
  @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
  @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
  @width  ||= args[:chart_options][:width]  || DEFAULT_WIDTH

end

Instance Attribute Details

#dataObject

Graph data



51
52
53
# File 'lib/seer/geomap.rb', line 51

def data
  @data
end

#data_labelObject

Graph data



51
52
53
# File 'lib/seer/geomap.rb', line 51

def data_label
  @data_label
end

#data_methodObject

Graph data



51
52
53
# File 'lib/seer/geomap.rb', line 51

def data_method
  @data_method
end

#data_modeObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def data_mode
  @data_mode
end

#data_tableObject

:nodoc:



51
52
53
# File 'lib/seer/geomap.rb', line 51

def data_table
  @data_table
end

#enable_tooltipObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def enable_tooltip
  @enable_tooltip
end

#heightObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def height
  @height
end

#label_methodObject

Graph data



51
52
53
# File 'lib/seer/geomap.rb', line 51

def label_method
  @label_method
end

#legendObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def legend
  @legend
end

#legend_background_colorObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def legend_background_color
  @legend_background_color
end

#legend_font_sizeObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def legend_font_size
  @legend_font_size
end

#legend_text_colorObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def legend_text_color
  @legend_text_color
end

#regionObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def region
  @region
end

#show_legendObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def show_legend
  @show_legend
end

#show_zoom_outObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def show_zoom_out
  @show_zoom_out
end

#titleObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def title
  @title
end

#title_colorObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def title_color
  @title_color
end

#title_font_sizeObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def title_font_size
  @title_font_size
end

#title_xObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def title_x
  @title_x
end

#title_yObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def title_y
  @title_y
end

#tooltip_font_sizeObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def tooltip_font_size
  @tooltip_font_size
end

#tooltip_heightObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def tooltip_height
  @tooltip_height
end

#tooltip_widthObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def tooltip_width
  @tooltip_width
end

#widthObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def width
  @width
end

#zoom_out_labelObject

Chart options accessors



48
49
50
# File 'lib/seer/geomap.rb', line 48

def zoom_out_label
  @zoom_out_label
end

Class Method Details

.render(data, args) ⇒ Object

Class Methods =========================================


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/seer/geomap.rb', line 150

def self.render(data, args)
  map = Seer::Geomap.new(
    :region         => args[:chart_options][:region],
    :data_mode      => args[:chart_options][:data_mode],
    :data           => data,
    :label_method   => args[:series][:series_label],
    :data_method    => args[:series][:data_method],
    :chart_options  => args[:chart_options],
    :chart_element  => args[:in_element] || 'chart'
  )
  map.to_js
end

Instance Method Details

#data_columnsObject

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/seer/geomap.rb', line 69

def data_columns #:nodoc:
  _data_columns = "data.addRows(#{data_table.size});"
  if self.data_mode == 'markers'
    _data_columns << %{
      data.addColumn('number', 'LATITUDE');
      data.addColumn('number', 'LONGITUDE');
      data.addColumn('number', '#{data_method}');
      data.addColumn('string', '#{label_method}');
    }
  else
    _data_columns << "          data.addColumn('string', '#{label_method}');"
    _data_columns << "          data.addColumn('number', '#{data_method}');"
    _data_columns
  end
  _data_columns
end

#formatted_colorsObject

Because Google is not consistent in their @#!$ API…



112
113
114
# File 'lib/seer/geomap.rb', line 112

def formatted_colors
  "[#{@colors.map{|color| "'#{color.gsub(/\#/,'0x')}'"} * ','}]"
end

#nonstring_optionsObject

:nodoc:



116
117
118
# File 'lib/seer/geomap.rb', line 116

def nonstring_options  #:nodoc:
  [:colors, :enable_tooltip, :height, :legend_font_size, :title_font_size, :tooltip_font_size, :tooltip_width, :width]
end

#string_optionsObject

:nodoc:



120
121
122
# File 'lib/seer/geomap.rb', line 120

def string_options #:nodoc:
  [:data_mode, :legend, :legend_background_color, :legend_text_color, :title, :title_x, :title_y, :title_color, :region]
end

#to_jsObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/seer/geomap.rb', line 124

def to_js
  %{
    <script type="text/javascript">
      google.load('visualization', '1', {'packages':['geomap']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
#{data_columns}
#{data_table.join("\r")}
        var options = {};
#{options}
        var container = document.getElementById('#{self.chart_element}');
        var geomap = new google.visualization.GeoMap(container);
        geomap.draw(data, options);
      }
    </script>
  }
end