Class: Nyaplot::MapPlot

Inherits:
Plot
  • Object
show all
Includes:
Jsonizable
Defined in:
lib/mapnya/plot.rb

Overview

Plot Object for Map visualization

Instance Attribute Summary collapse

Attributes inherited from Plot

#bg_color, #grid_color, #height, #legend, #legend_options, #legend_width, #margin, #rotate_x_label, #rotate_y_label, #width, #x_label, #xrange, #y_label, #yrange, #zoom

Instance Method Summary collapse

Methods included from Jsonizable

#get_property, included, #init_properties, #set_property, #to_json

Methods inherited from Plot

#add, #add_with_df, #configure, #export_html, #show, #to_iruby

Constructor Details

#initializeMapPlot

Returns a new instance of MapPlot



20
21
22
23
24
25
# File 'lib/mapnya/plot.rb', line 20

def initialize
  super()
  set_property(:axis_extra_options, {})
  map_data(nil)
  extension('Mapnya')
end

Instance Attribute Details

#centerArray<Numeric>

Returns the center of the map

Returns:

  • (Array<Numeric>)

    the center of the map



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

#colorArray<String>

Returns the array of colors which countries are fill in

Returns:

  • (Array<String>)

    the array of colors which countries are fill in



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

#map_dataHash

Returns the geojson map data

Returns:

  • (Hash)

    the geojson map data



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

#no_data_colorString

Returns the color which no-data countries are fill in when specified fill_by

Returns:

  • (String)

    the color which no-data countries are fill in when specified fill_by



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

#scaleNumeric

Returns the scale of the map

Returns:

  • (Numeric)

    the scale of the map



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

#stroke_colorString

Returns the color which borders are fill in

Returns:

  • (String)

    the color which borders are fill in



18
# File 'lib/mapnya/plot.rb', line 18

define_group_properties(:axis_extra_options, [:map_data, :color, :stroke_color, :center, :scale, :no_data_color, :df_id, :cca3, :fill_by])

Instance Method Details

#add_map(name, data = nil) ⇒ Object

Add the map of individual countries instead of world map

Examples:

plot = Nyaplot::MapPlot.new
plot.add_map(JPN) #-> add the map of Japan to the plot

Parameters:

  • name (String)

    the name of country to add

  • data (Hash) (defaults to: nil)

    your own geojson data

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mapnya/plot.rb', line 34

def add_map(name, data=nil)
  if data.nil?
    path = File.expand_path("../datasets/countries/data/" + name.downcase + ".geo.json", __FILE__)
    map_data = JSON.parse(File.read(path))
    # pre-processing
    map_data["features"].push(map_data["features"][0]) if map_data["features"].length == 1
    country_data = Countries.df.filter{|row| row[:cca3] == name.upcase}.row(0)
    center([country_data[:lng], country_data[:lat]])
    map_data(map_data)
  else
    map_data(data)
  end
end

#before_to_jsonObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mapnya/plot.rb', line 66

def before_to_json
  zoom(true)
  width(800) if width.nil?
  height(800) if height.nil?

  if map_data.nil?
    path = File.expand_path("../datasets/geo-boundaries-world-110m/countries.geojson", __FILE__)
    map_data(JSON.parse(File.read(path)))
  end

  self.options[:axis_extra_options] = axis_extra_options
end

#df_listObject



48
49
50
51
52
# File 'lib/mapnya/plot.rb', line 48

def df_list
  arr = super()
  return arr if df_id.nil?
  return arr.push(df_id)
end

#fill_map_with_df(df, id_column, fill_by_column) ⇒ Object

Fill countries in different colors according to some data

Parameters:

  • df (DataFrame)
  • id_column (Symbol)

    the column that includes cca3

  • fill_by_column (Symbol)

    the column that includes some values

See Also:



59
60
61
62
63
64
# File 'lib/mapnya/plot.rb', line 59

def fill_map_with_df(df, id_column, fill_by_column)
  cca3(id_column)
  fill_by(fill_by_column)
  df_id(df.name)
  DataBase.instance.add(df)
end