Class: Visualization::Configuration

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/visualizations_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Configuration

Returns a new instance of Configuration.


3
4
5
6
7
# File 'app/helpers/visualizations_helper.rb', line 3

def initialize(config = {})
  @config = config
  @categories_colors = @config.delete(:categories_colors)
  @config[:backgrounds] = MapBackground.availables.collect(&:to_json_object)
end

Instance Method Details

#background(layer, options = {}) ⇒ Object


9
10
11
12
13
14
15
16
# File 'app/helpers/visualizations_helper.rb', line 9

def background(layer, options = {})
  # allow to add custom background
  options[:name] = layer.name if layer.name?
  options[:url] = layer.url if layer.url?
  options[:by_default] = layer.by_default if layer.by_default?
  @config[:backgrounds] ||= []
  @config[:backgrounds] << options
end

#band(name, serie, options = {}) ⇒ Object


53
54
55
# File 'app/helpers/visualizations_helper.rb', line 53

def band(name, serie, options = {})
  layer(name, serie, options.merge(type: :band))
end

#bubbles(name, serie, options = {}) ⇒ Object


45
46
47
# File 'app/helpers/visualizations_helper.rb', line 45

def bubbles(name, serie, options = {})
  layer(name, serie, options.merge(type: :bubbles))
end

#categories(name, serie, options = {}) ⇒ Object


57
58
59
# File 'app/helpers/visualizations_helper.rb', line 57

def categories(name, serie, options = {})
  layer(name, serie, { colors: @categories_colors }.merge(options.merge(type: :categories)))
end

#choropleth(name, serie, options = {}) ⇒ Object


41
42
43
# File 'app/helpers/visualizations_helper.rb', line 41

def choropleth(name, serie, options = {})
  layer(name, serie, options.merge(type: :choropleth))
end

#control(name, options = true) ⇒ Object

Add a control


90
91
92
93
# File 'app/helpers/visualizations_helper.rb', line 90

def control(name, options = true)
  @config[:controls] ||= {}.with_indifferent_access
  @config[:controls][name.to_s.camelize(:lower)] = options
end

#heatmap(name, serie, options = {}) ⇒ Object


49
50
51
# File 'app/helpers/visualizations_helper.rb', line 49

def heatmap(name, serie, options = {})
  layer(name, serie, options.merge(type: :heatmap))
end

#layer(name, serie, options = {}) ⇒ Object

def layer(name, list = {})

@config[:layers] ||= []
@config[:layers] << {name: name, list: list}

end


28
29
30
31
32
33
34
35
# File 'app/helpers/visualizations_helper.rb', line 28

def layer(name, serie, options = {})
  unless options[:label]
    options[:label] = name.is_a?(String) ? name : name.tl(default: "attributes.#{name}".to_sym)
  end
  name = name.to_s.parameterize.tr('-', '_') unless name.is_a?(Symbol)
  @config[:layers] ||= []
  @config[:layers] << { reference: name.to_s.camelcase(:lower) }.merge(options.merge(name: name, serie: serie.to_s.camelcase(:lower)))
end

#overlay(name, provider_name) ⇒ Object


18
19
20
21
# File 'app/helpers/visualizations_helper.rb', line 18

def overlay(name, provider_name)
  @config[:overlays] ||= []
  @config[:overlays] << { name: name, provider_name: provider_name }
end

#path(name, serie, options = {}) ⇒ Object


65
66
67
# File 'app/helpers/visualizations_helper.rb', line 65

def path(name, serie, options = {})
  layer(name, serie, { colors: @categories_colors }.merge(options.merge(type: :path)))
end

#paths(name, serie, options = {}) ⇒ Object


61
62
63
# File 'app/helpers/visualizations_helper.rb', line 61

def paths(name, serie, options = {})
  layer(name, serie, { colors: @categories_colors }.merge(options.merge(type: :paths)))
end

#points(name, serie, options = {}) ⇒ Object


69
70
71
# File 'app/helpers/visualizations_helper.rb', line 69

def points(name, serie, options = {})
  layer(name, serie, { colors: @categories_colors }.merge(options.merge(type: :points)))
end

#serie(name, data) ⇒ Object

Add a serie of geo data

Raises:

  • (StandardError)

78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/visualizations_helper.rb', line 78

def serie(name, data)
  raise StandardError, 'data must be an array. Got: ' + data.class.name unless data.is_a? Array
  @config[:series] ||= {}.with_indifferent_access
  @config[:series][name] = data.compact.collect do |item|
    next unless item[:shape]
    item
      .merge(shape: Charta.new_geometry(item[:shape]).transform(:WGS84).to_json_object)
      .merge(item[:popup] ? { popup: compile_visualization_popup(item[:popup], item) } : {})
  end.compact
end

#simple(name, serie, options = {}) ⇒ Object


37
38
39
# File 'app/helpers/visualizations_helper.rb', line 37

def simple(name, serie, options = {})
  layer(name, serie, options.merge(type: :simple))
end

#to_jsonObject


95
96
97
# File 'app/helpers/visualizations_helper.rb', line 95

def to_json
  @config.jsonize_keys.to_json
end