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.


5
6
7
8
9
10
11
# File 'app/helpers/visualizations_helper.rb', line 5

def initialize(config = {})
  @config = config
  @categories_colors = @config.delete(:categories_colors)
  @config[:backgrounds] = MapLayer.available_backgrounds.collect(&:to_json_object)
  @config[:overlays] = MapLayer.available_overlays.collect(&:to_json_object)
  @config[:async_url] = @config.delete(:async_url)
end

Instance Method Details

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


13
14
15
16
17
18
19
20
# File 'app/helpers/visualizations_helper.rb', line 13

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


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

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

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


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

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

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


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

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

#center(point) ⇒ Object


114
115
116
117
# File 'app/helpers/visualizations_helper.rb', line 114

def center(point)
  @config[:view] ||= {}.with_indifferent_access
  @config[:view][:center] = point
end

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


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

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

#control(name, options = true) ⇒ Object

Add a control


105
106
107
108
# File 'app/helpers/visualizations_helper.rb', line 105

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

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


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

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


35
36
37
38
39
40
41
42
# File 'app/helpers/visualizations_helper.rb', line 35

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(layer, options = {}) ⇒ Object


22
23
24
25
26
27
28
# File 'app/helpers/visualizations_helper.rb', line 22

def overlay(layer, options = {})
  options[:name] = layer.name if layer.name?
  options[:url] = layer.url if layer.url?
  options[:opacity] = layer.opacity if layer.opacity
  @config[:overlays] ||= []
  @config[:overlays] << options
end

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


72
73
74
# File 'app/helpers/visualizations_helper.rb', line 72

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

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


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

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

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


80
81
82
# File 'app/helpers/visualizations_helper.rb', line 80

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

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


76
77
78
# File 'app/helpers/visualizations_helper.rb', line 76

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

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


84
85
86
# File 'app/helpers/visualizations_helper.rb', line 84

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

#serie(name, data) ⇒ Object

Add a serie of geo data

Raises:

  • (StandardError)

93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/visualizations_helper.rb', line 93

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


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

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

#to_json(_options = {}) ⇒ Object


110
111
112
# File 'app/helpers/visualizations_helper.rb', line 110

def to_json(_options = {})
  @config.jsonize_keys.to_json
end