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(
      @widgets,
      :as => :geomap,
      :in_element => 'my_geomap_container',
      :series => {
        :series_label => 'name',
        :data_label => '# widgets',
        :data_method => 'quantity'
      },
      :chart_options => {
        :data_mode => 'regions',
        :region => 'US',
      }  
    )
-%>

@widgets==

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

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

- (Geomap) initialize(args = {})

: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

- (Object) data

Graph data



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

def data
  @data
end

- (Object) data_label

Graph data



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

def data_label
  @data_label
end

- (Object) data_method

Graph data



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

def data_method
  @data_method
end

- (Object) data_mode

Chart options accessors



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

def data_mode
  @data_mode
end

- (Object) data_table

:nodoc:



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

def data_table #:nodoc:
  @data_table = []
  data.each_with_index do |datum, column|
    next unless datum.geocoded?
    if data_mode == "markers"
      @data_table << [
        "            data.setValue(#{column}, 0, #{datum.latitude});\r",
        "            data.setValue(#{column}, 1, #{datum.longitude});\r",
        "            data.setValue(#{column}, 2, #{datum.send(data_method)});\r",
        "            data.setValue(#{column}, 3, '#{datum.send(label_method)}');\r"
      ]
    else # Regions
      @data_table << [
        "            data.setValue(#{column}, 0, '#{datum.name}');\r",
        "            data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
      ]
    end
  end
  @data_table
end

- (Object) enable_tooltip

Chart options accessors



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

def enable_tooltip
  @enable_tooltip
end

- (Object) height

Chart options accessors



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

def height
  @height
end

- (Object) label_method

Graph data



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

def label_method
  @label_method
end

- (Object) legend

Chart options accessors



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

def legend
  @legend
end

- (Object) legend_background_color

Chart options accessors



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

def legend_background_color
  @legend_background_color
end

- (Object) legend_font_size

Chart options accessors



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

def legend_font_size
  @legend_font_size
end

- (Object) legend_text_color

Chart options accessors



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

def legend_text_color
  @legend_text_color
end

- (Object) region

Chart options accessors



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

def region
  @region
end

- (Object) show_legend

Chart options accessors



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

def show_legend
  @show_legend
end

- (Object) show_zoom_out

Chart options accessors



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

def show_zoom_out
  @show_zoom_out
end

- (Object) title

Chart options accessors



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

def title
  @title
end

- (Object) title_color

Chart options accessors



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

def title_color
  @title_color
end

- (Object) title_font_size

Chart options accessors



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

def title_font_size
  @title_font_size
end

- (Object) title_x

Chart options accessors



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

def title_x
  @title_x
end

- (Object) title_y

Chart options accessors



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

def title_y
  @title_y
end

- (Object) tooltip_font_size

Chart options accessors



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

def tooltip_font_size
  @tooltip_font_size
end

- (Object) tooltip_height

Chart options accessors



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

def tooltip_height
  @tooltip_height
end

- (Object) tooltip_width

Chart options accessors



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

def tooltip_width
  @tooltip_width
end

- (Object) width

Chart options accessors



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

def width
  @width
end

- (Object) zoom_out_label

Chart options accessors



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

def zoom_out_label
  @zoom_out_label
end

Class Method Details

+ (Object) render(data, args)

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


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

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

- (Object) data_columns

: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

- (Object) formatted_colors

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



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

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

- (Object) nonstring_options

:nodoc:



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

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

- (Object) string_options

:nodoc:



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

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

- (Object) to_js



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

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.to_s}
        var options = {};
#{options}
        var container = document.getElementById('#{self.chart_element}');
        var geomap = new google.visualization.GeoMap(container);
        geomap.draw(data, options);
      }
    </script>
  }
end