Class: Seer::Geomap
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
-
#data ⇒ Object
Graph data.
-
#data_label ⇒ Object
Graph data.
-
#data_method ⇒ Object
Graph data.
-
#data_mode ⇒ Object
Chart options accessors.
-
#data_table ⇒ Object
:nodoc:.
-
#enable_tooltip ⇒ Object
Chart options accessors.
-
#height ⇒ Object
Chart options accessors.
-
#label_method ⇒ Object
Graph data.
-
#legend ⇒ Object
Chart options accessors.
-
#legend_background_color ⇒ Object
Chart options accessors.
-
#legend_font_size ⇒ Object
Chart options accessors.
-
#legend_text_color ⇒ Object
Chart options accessors.
-
#region ⇒ Object
Chart options accessors.
-
#show_legend ⇒ Object
Chart options accessors.
-
#show_zoom_out ⇒ Object
Chart options accessors.
-
#title ⇒ Object
Chart options accessors.
-
#title_color ⇒ Object
Chart options accessors.
-
#title_font_size ⇒ Object
Chart options accessors.
-
#title_x ⇒ Object
Chart options accessors.
-
#title_y ⇒ Object
Chart options accessors.
-
#tooltip_font_size ⇒ Object
Chart options accessors.
-
#tooltip_height ⇒ Object
Chart options accessors.
-
#tooltip_width ⇒ Object
Chart options accessors.
-
#width ⇒ Object
Chart options accessors.
-
#zoom_out_label ⇒ Object
Chart options accessors.
Attributes included from Chart
Class Method Summary collapse
-
.render(data, args) ⇒ Object
Class Methods =========================================.
Instance Method Summary collapse
-
#data_columns ⇒ Object
:nodoc:.
-
#formatted_colors ⇒ Object
Because Google is not consistent in their @#!$ API…
-
#initialize(args = {}) ⇒ Geomap
constructor
:nodoc:.
-
#nonstring_options ⇒ Object
:nodoc:.
-
#string_options ⇒ Object
:nodoc:.
- #to_js ⇒ Object
Methods included from Chart
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
#data ⇒ Object
Graph data
51 52 53 |
# File 'lib/seer/geomap.rb', line 51 def data @data end |
#data_label ⇒ Object
Graph data
51 52 53 |
# File 'lib/seer/geomap.rb', line 51 def data_label @data_label end |
#data_method ⇒ Object
Graph data
51 52 53 |
# File 'lib/seer/geomap.rb', line 51 def data_method @data_method end |
#data_mode ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def data_mode @data_mode end |
#data_table ⇒ Object
:nodoc:
51 52 53 |
# File 'lib/seer/geomap.rb', line 51 def data_table @data_table end |
#enable_tooltip ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def enable_tooltip @enable_tooltip end |
#height ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def height @height end |
#label_method ⇒ Object
Graph data
51 52 53 |
# File 'lib/seer/geomap.rb', line 51 def label_method @label_method end |
#legend ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def legend @legend end |
#legend_background_color ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def legend_background_color @legend_background_color end |
#legend_font_size ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def legend_font_size @legend_font_size end |
#legend_text_color ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def legend_text_color @legend_text_color end |
#region ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def region @region end |
#show_legend ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def show_legend @show_legend end |
#show_zoom_out ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def show_zoom_out @show_zoom_out end |
#title ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def title @title end |
#title_color ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def title_color @title_color end |
#title_font_size ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def title_font_size @title_font_size end |
#title_x ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def title_x @title_x end |
#title_y ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def title_y @title_y end |
#tooltip_font_size ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def tooltip_font_size @tooltip_font_size end |
#tooltip_height ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def tooltip_height @tooltip_height end |
#tooltip_width ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def tooltip_width @tooltip_width end |
#width ⇒ Object
Chart options accessors
48 49 50 |
# File 'lib/seer/geomap.rb', line 48 def width @width end |
#zoom_out_label ⇒ Object
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_columns ⇒ Object
: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_colors ⇒ Object
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_options ⇒ Object
:nodoc:
116 117 118 |
# File 'lib/seer/geomap.rb', line 116 def #:nodoc: [:colors, :enable_tooltip, :height, :legend_font_size, :title_font_size, :tooltip_font_size, :tooltip_width, :width] end |
#string_options ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/seer/geomap.rb', line 120 def #:nodoc: [:data_mode, :legend, :legend_background_color, :legend_text_color, :title, :title_x, :title_y, :title_color, :region] end |
#to_js ⇒ Object
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 = {}; #{} var container = document.getElementById('#{self.chart_element}'); var geomap = new google.visualization.GeoMap(container); geomap.draw(data, options); } </script> } end |