Class: GChart::Map

Inherits:
Base
  • Object
show all
Defined in:
lib/gchart/map.rb

Constant Summary collapse

AREAS =
%w[africa asia europe middle_east south_america usa world]

Instance Attribute Summary collapse

Attributes inherited from Base

#axes, #chart_background, #colors, #data, #entire_background, #extras, #height, #legend, #max, #title, #width

Instance Method Summary collapse

Methods inherited from Base

#axis, #fetch, #size, #size=, #to_url, #write

Constructor Details

#initialize(*args, &block) ⇒ Map

Returns a new instance of Map.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gchart/map.rb', line 8

def initialize(*args, &block)
  super(*args, &block)
  # Set some sane defaults so that the only requirement is data
  @area = 'world' #default
  @background = 'dfdfff' #make it look like water
  @colors = ['ffffff','f8fcf8','006c00']
  #Set the maximum size for maps (this is a better default because
  # it is also the proper aspect ratio)
  @width = '440'
  @height = '220'
end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



4
5
6
# File 'lib/gchart/map.rb', line 4

def area
  @area
end

#area_codesObject

Returns the value of attribute area_codes.



5
6
7
# File 'lib/gchart/map.rb', line 5

def area_codes
  @area_codes
end

#backgroundObject

Returns the value of attribute background.



6
7
8
# File 'lib/gchart/map.rb', line 6

def background
  @background
end

Instance Method Details

#data=(data) ⇒ Object

Map data can be in the form “VA’=>5,‘NY’=>1 or [[‘VA’,5],] Raises ArgumentError if data does not fit these forms.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gchart/map.rb', line 22

def data=(data)
  if data.is_a?(Array) && data.any?{ |pair| pair.size != 2 }
    raise ArgumentError, "Data array must contain [area],[value] pairs"
  end
  # 'unzip' the data into separate arrays
  area_data, values = data.to_a.transpose

  # Reject malformed area codes
  if area_data.any?{ |code| code !~ /^\w\w$/}
    raise ArgumentError, "Area data must have exactly two characters" 
  end
  @area_codes = area_data.join.upcase
  super(values)
end

#query_params(params = {}) ⇒ Object

overrides GChart::Base#query_params



47
48
49
50
51
52
# File 'lib/gchart/map.rb', line 47

def query_params(params={}) #:nodoc:
  params["chtm"] = area unless area.empty?
  params["chld"] = area_codes if area_codes
  params["chf"] = "bg,s,#{@background}" if @background
  super(params)
end

#render_chart_typeObject

:nodoc:



37
38
39
# File 'lib/gchart/map.rb', line 37

def render_chart_type #:nodoc:
  "t"
end

#render_data(params) ⇒ Object

overrides GChart::Base#render_data



55
56
57
58
59
60
# File 'lib/gchart/map.rb', line 55

def render_data(params)
  super(params)
  # Maps require at least one data point. Add a "missing value".
  # It may be better to refactor the base class.
  params["chd"] << '__' if params["chd"] =~ /e:$/
end

#render_legend(params) ⇒ Object

:nodoc:



66
67
68
# File 'lib/gchart/map.rb', line 66

def render_legend(params) #:nodoc:
  nil #N/A for map
end

#render_title(params) ⇒ Object

:nodoc:



62
63
64
# File 'lib/gchart/map.rb', line 62

def render_title(params) #:nodoc:
  nil #N/A for map
end