Class: Nyaplot::Plot

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

Overview

Jsonizable Object to which diagrams are registered Properties of Nyaplot::Plot are embeded into the JSON object as a part of property ‘panes’ by Nyaplot::Frame

Direct Known Subclasses

CircularPlot, MapPlot

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Jsonizable

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

Constructor Details

#initializePlot

Returns a new instance of Plot.



40
41
42
43
44
45
46
# File 'lib/nyaplot/plot.rb', line 40

def initialize
  init_properties
  set_property(:diagrams, [])
  set_property(:options, {})
  set_property(:width, nil)
  set_property(:legend, nil)
end

Instance Attribute Details

#bg_colorString

Returns the code of color which background is filled in.

Returns:

  • (String)

    the code of color which background is filled in



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#grid_colorString

Returns the code of color which grid lines are filled in.

Returns:

  • (String)

    the code of color which grid lines are filled in



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#heightNumeric

Returns the height.

Returns:

  • (Numeric)

    the height



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#legendBoolean

Returns whether to show legend or not.

Returns:

  • (Boolean)

    whether to show legend or not



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#legend_optionsHash

Returns the name of width set.

Returns:

  • (Hash)

    the name of width set



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#legend_widthNumeric

Returns the width of legend area.

Returns:

  • (Numeric)

    the width of legend area



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#marginHash

Returns the margin.

Returns:

  • (Hash)

    the margin



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#rotate_x_labelNumeric

Returns the angle to rotate x label (radian).

Returns:

  • (Numeric)

    the angle to rotate x label (radian)



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#rotate_y_labelNumeric

Returns the angle to rotate y label (radian).

Returns:

  • (Numeric)

    the angle to rotate y label (radian)



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#widthNumeric

Returns the width.

Returns:

  • (Numeric)

    the width



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#x_labelString

Returns the name of label placed along x-axis.

Returns:

  • (String)

    the name of label placed along x-axis



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#xrangeArray<Numeric>, ...

Returns the name of width set.

Returns:

  • (Array<Numeric>, Array<String>, Array<Symbol>)

    the name of width set



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#y_labelString

Returns the name of label placed along y-axis.

Returns:

  • (String)

    the name of label placed along y-axis



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#yrangeArray<Numeric>, ...

Returns the name of width set.

Returns:

  • (Array<Numeric>, Array<String>, Array<Symbol>)

    the name of width set



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

#zoomBoolean

Returns whether to enable zooming.

Returns:

  • (Boolean)

    whether to enable zooming



37
# File 'lib/nyaplot/plot.rb', line 37

define_properties(:diagrams, :filter)

Instance Method Details

#add(type, *data) ⇒ Object

Add diagram with Array

Examples:

plot.add(:scatter, [0,1,2], [0,1,2])

Parameters:

  • type (Symbol)

    the type of diagram to add

  • *data (Array<Array>)

    array from which diagram is created



53
54
55
56
57
58
# File 'lib/nyaplot/plot.rb', line 53

def add(type, *data)
  labels = data.map.with_index{|d, i| 'data' + i.to_s}
  raw_data = data.each.with_index.reduce({}){|memo, (d, i)| memo[labels[i]]=d; next memo}
  df = DataFrame.new(raw_data)
  return add_with_df(df, type, *labels)
end

#add_with_df(df, type, *labels) ⇒ Object

Add diagram with DataFrame

Examples:

df = Nyaplot::DataFrame.new({x: [0,1,2], y: [0,1,2]})
plot.add(df, :scatter, :x, :y)

Parameters:

  • DataFrame (DataFrame)

    from which diagram is created

  • type (Symbol)

    the type of diagram to add

  • *labels (Array<Symbol>)

    column labels for x, y or some other dimension



67
68
69
70
71
72
# File 'lib/nyaplot/plot.rb', line 67

def add_with_df(df, type, *labels)
  diagram = Diagram.new(df, type, labels)
  diagrams = get_property(:diagrams)
  diagrams.push(diagram)
  return diagram
end

#before_to_jsonObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nyaplot/plot.rb', line 100

def before_to_json
  diagrams = get_property(:diagrams)
  return if diagrams.length == 0

  # set default values when not specified by users
  zoom(true) if diagrams.all?{|d| d.zoom?}

  if width.nil?
    if legend == true
      width(800)
    else
      width(700)
    end
  end

  [:xrange, :yrange].each do |symbol|
    if get_property(:options)[symbol].nil?
      range = []
      diagrams.each{|diagram| range.push(diagram.send(symbol))}

      if range.all? {|r| r.length == 2} # continuous data
        range = range.transpose
        range = [range[0].min, range[1].max]
        self.send(symbol, range)
      else # discrete data
        range.flatten!.uniq!
        self.send(symbol, range)
      end
    end
  end
end

#configure(&block) ⇒ Object

Shortcut method to configure plot

Examples:

plot = Nyaplot::Plot.new
plot.configure do
  width(700)
  height(700)
end


139
140
141
# File 'lib/nyaplot/plot.rb', line 139

def configure(&block)
  self.instance_eval(&block) if block_given?
end

#df_listArray<String>

Returns names of dataframe used by diagrams belog to this plot.

Returns:

  • (Array<String>)

    names of dataframe used by diagrams belog to this plot



93
94
95
96
97
98
# File 'lib/nyaplot/plot.rb', line 93

def df_list
  arr=[]
  diagrams = get_property(:diagrams)
  diagrams.each{|d| arr.push(d.df_name)}
  return arr
end

#export_html(path = nil) ⇒ Object

export html file



86
87
88
89
90
# File 'lib/nyaplot/plot.rb', line 86

def export_html(path=nil)
  require 'securerandom'
  path = "./plot-" + SecureRandom.uuid().to_s + ".html" if path.nil?
  Frame.new.tap {|f| f.add(self) }.export_html(path)
end

#showObject

Show plot on IRuby notebook



81
82
83
# File 'lib/nyaplot/plot.rb', line 81

def show
  Frame.new.tap {|f| f.add(self) }.show
end

#to_irubyObject

Show plot automatically on IRuby notebook



76
77
78
# File 'lib/nyaplot/plot.rb', line 76

def to_iruby
  Frame.new.tap {|f| f.add(self) }.to_iruby
end