Class: Ziya::Maps::Base

Inherits:
Object
  • Object
show all
Includes:
HtmlHelpers::Maps, YamlHelpers::Maps
Defined in:
lib/ziya/maps/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YamlHelpers::Maps

#config, #level, #map

Methods included from YamlHelpers::Base

#clazz, #indent

Methods included from HtmlHelpers::Maps

#_ziya_map, #default_map_options, #gen_sw_path, #maps_swf, #maps_swf_base, #ziya_map

Methods included from HtmlHelpers::Base

#escape_chars, #escape_once, #escape_url, #mime, #plugin_url, #setup_movie_size, #setup_wmode, #tag, #tag_options

Constructor Details

#initialize(map_type, map_id = nil) ⇒ Base

create a new map. chart_id – the name of the map stylesheet. NOTE: If map_id is specified the framework will attempt to load the map styles from public/maps/themes/theme_name/map_id.yml



81
82
83
84
85
86
87
88
89
90
# File 'lib/ziya/maps/base.rb', line 81

def initialize( map_type, map_id=nil ) # :nodoc:
  raise "Invalid map type '#{map_type.inspect}'" unless Ziya::Maps::Base.map_types.include?( map_type )
  @id           = map_id
  @options      = @series = @points = @arcs = @lines = {}
  @line_colors  = @range_colors = @point_colors = @arc_colors = {}
  @theme        = default_theme
  @map_type     = map_type
  initialize_components
  load_helpers( Ziya.helpers_dir ) if Ziya.helpers_dir
end

Instance Attribute Details

#idObject

:nodoc:



74
75
76
# File 'lib/ziya/maps/base.rb', line 74

def id
  @id
end

#map_typeObject (readonly)

:nodoc:



75
76
77
# File 'lib/ziya/maps/base.rb', line 75

def map_type
  @map_type
end

#optionsObject

:nodoc:



74
75
76
# File 'lib/ziya/maps/base.rb', line 74

def options
  @options
end

#themeObject

:nodoc:



74
75
76
# File 'lib/ziya/maps/base.rb', line 74

def theme
  @theme
end

Class Method Details

.componentsObject

class component accessor…



93
94
95
# File 'lib/ziya/maps/base.rb', line 93

def self.components # :nodoc:
  @components
end

Instance Method Details

#add(*args) ⇒ Object

Add chart components to a given chart.

The args must contain certain keys for the chart to be displayed correctly.

Directives

  • :series – Specifies the data points for the map. This must be a hash of region, data pairs. Where the data is also a hash of attributes such as :name, :data, :hover, :url and :target.

  • :range_colors – Specifies the map of ranges of data values from the series to a color on the map. The range may be an exact value such as 10 or an actual range in the form of 1 - 10. The color must be an hexadecimal value of the form ‘ff00ff’.

  • :point_colors – Specifies a map of valid color ranges to color the points on the map. Valid key/value pairs are a number or range for the key and an associated color.

  • :arc_colors – Specifies a map of valid color ranges to color the arcs on the map. Valid key/value pairs are a number or range for the key and an associated color.

  • :line_colors – Specifies a map of valid color ranges to color the lines on the map. Valid key/value pairs are a number or range for the key and an associated color.

  • :lines – Specifies a map of lines to plot on the map. These are key/value pairs. The value represent the name of the line to be displayed. The value is a hash of attributes as follows: :start, the geocode location of the point, :stop, the geocode location of the arc to end at :stroke, the thickness of the line and :data for the range value of the arc.

  • :arcs – Specifies a map of arcs to plot on the map. These are key/value pairs. The value represent the name of the arc to be displayed. The value is a hash of attributes as follows: :start, the geocode location of the point, :stop, the geocode location of the arc to end at and :data for the range value of the arc.

  • :points – Specifies a map of points to plot on the map. These are key/value pairs. The value represent the name of the point to be displayed. The value is a hash of attributes as follows: :loc for the geocode location of the point and :data for the range value of the point.

  • :theme – Specify the use of a given named theme. The named theme must reside in a directory named equaly under your application public/charts/themes.

Examples

Colorize colorado and california on a US map

my_map.add( :series, { :CO => { :name => "Colorado", :data => 1 }, :CA => { :name => "California", :data => 2 } )


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ziya/maps/base.rb', line 161

def add( *args )
  # TODO Validation categories = series, series = labels, etc...
  directive = args.shift
  case directive
    when :series
      if args.first.is_a?( Hash )
        points = args.shift || {}
        raise ArgumentError, "Must specify an hash of data points" if points.empty?
        @series = points
      else
        raise ArgumentError, "Must specify an hash of data points"
      end
    when :range_colors
      @range_colors = args.shift || {}
      raise ArgumentError, "You must specify a hash of data range/color pairs" if @range_colors.empty?          
    when :lines
      @lines = args.shift || {}
      raise ArgumentError, "You must specify a hash of lines attributes" if @lines.empty?
    when :line_colors
      @line_colors = args.shift || {}
      raise ArgumentError, "You must specify a hash of line range/color pairs" if @line_colors.empty?          
    when :arcs
      @arcs = args.shift || {}
      raise ArgumentError, "You must specify a hash of arcs attributes" if @arcs.empty?
    when :arc_colors
      @arc_colors = args.shift || {}
      raise ArgumentError, "You must specify a hash of arc range/color pairs" if @arc_colors.empty?          
    when :points
      @points = args.shift || {}
      raise ArgumentError, "You must specify a hash of points attributes" if @points.empty?
    when :point_colors
      @point_colors = args.shift || {}
      raise ArgumentError, "You must specify a hash of point range/color pairs" if @point_colors.empty?
    when :user_data
      key = args.first.is_a?(Symbol) ? args.shift : ""
      raise ArgumentError, "Must specify a key" if key.to_s.empty?
      value = args.shift
      # raise ArgumentError, "Must specify a value" if value.empty?
      @options[key] = value
    when :theme
      theme = args.first.is_a?(String) ? args.shift : ""
      raise ArgumentError, "Must specify a theme name" if theme.to_s.empty?          
      @theme = "#{Ziya.map_themes_dir}/#{theme}"
    else raise ArgumentError, "Invalid directive must be one of " + 
                             ":series, :theme, :user_data"
  end 
end

#default_themeObject

default ZiYa theme



98
99
100
# File 'lib/ziya/maps/base.rb', line 98

def default_theme # :nodoc:
  File.join( Ziya.map_themes_dir, %w[default] )
end

#load_helpers(helper_dir) ⇒ Object

load up ERB style helpers



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ziya/maps/base.rb', line 103

def load_helpers( helper_dir ) # :nodoc:    
  Dir.foreach(helper_dir) do |helper_file| 
    next unless helper_file =~ /^([a-z][a-z_]*_helper).rb$/
    Ziya.logger.debug( ">>> ZiYa loading custom helper `#{$1}" )        
    # check rails env for autoloader ?
    if defined?(RAILS_ROOT) 
      require_dependency File.join(helper_dir, $1) 
    else
      require File.expand_path( File.join(helper_dir, $1) )
    end
    helper_module_name = "Ziya::" + $1.gsub(/(^|_)(.)/) { $2.upcase }        
    # helper_module_name = $1.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    # if Ziya::Helpers.const_defined?(helper_module_name)
    Ziya.logger.debug( "Include module #{helper_module_name}")
    Ziya::Maps::Base.class_eval("include #{helper_module_name}") 
    # end
  end
end

#to_sObject Also known as: to_xml

spews the map specification to a string



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ziya/maps/base.rb', line 210

def to_s
  out = ''
  @xml = Builder::XmlMarkup.new( :target => out )
  # Forces utf8 encoding on xml stream
  @xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"    
  if map_type == :us
    @xml.us_states do
      setup_lnf
      setup_series
    end
  else
    @xml.countrydata do
      setup_lnf
      setup_series
    end         
  end
  @xml.to_s.gsub( /<to_s\/>/, '' )
  out
end