Class: Nyaplot::Plot
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
Instance Attribute Summary collapse
-
#bg_color ⇒ String
The code of color which background is filled in.
-
#grid_color ⇒ String
The code of color which grid lines are filled in.
-
#height ⇒ Numeric
The height.
-
#legend ⇒ Boolean
Whether to show legend or not.
-
#legend_options ⇒ Hash
The name of width set.
-
#legend_width ⇒ Numeric
The width of legend area.
-
#margin ⇒ Hash
The margin.
-
#rotate_x_label ⇒ Numeric
The angle to rotate x label (radian).
-
#rotate_y_label ⇒ Numeric
The angle to rotate y label (radian).
-
#width ⇒ Numeric
The width.
-
#x_label ⇒ String
The name of label placed along x-axis.
-
#xrange ⇒ Array<Numeric>, ...
The name of width set.
-
#y_label ⇒ String
The name of label placed along y-axis.
-
#yrange ⇒ Array<Numeric>, ...
The name of width set.
-
#zoom ⇒ Boolean
Whether to enable zooming.
Instance Method Summary collapse
-
#add(type, *data) ⇒ Object
Add diagram with Array.
-
#add_with_df(df, type, *labels) ⇒ Object
Add diagram with DataFrame.
- #before_to_json ⇒ Object
-
#configure(&block) ⇒ Object
Shortcut method to configure plot.
-
#df_list ⇒ Array<String>
Names of dataframe used by diagrams belog to this plot.
-
#export_html(path = nil) ⇒ Object
export html file.
-
#initialize(&block) ⇒ Plot
constructor
A new instance of Plot.
-
#show ⇒ Object
Show plot on IRuby notebook.
-
#to_iruby ⇒ Object
Show plot automatically on IRuby notebook.
Methods included from Jsonizable
#get_property, included, #init_properties, #set_property, #to_json
Constructor Details
#initialize(&block) ⇒ Plot
Returns a new instance of Plot.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/nyaplot/plot.rb', line 40 def initialize(&block) init_properties set_property(:diagrams, []) set_property(:options, {}) set_property(:width, nil) set_property(:legend, nil) set_property(:zoom, nil) yield if block_given? end |
Instance Attribute Details
#bg_color ⇒ String
Returns the code of color which background is filled in.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#grid_color ⇒ String
Returns the code of color which grid lines are filled in.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#height ⇒ Numeric
Returns the height.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#legend ⇒ Boolean
Returns whether to show legend or not.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#legend_options ⇒ Hash
Returns the name of width set.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#legend_width ⇒ Numeric
Returns the width of legend area.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#margin ⇒ Hash
Returns the margin.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#rotate_x_label ⇒ Numeric
Returns the angle to rotate x label (radian).
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#rotate_y_label ⇒ Numeric
Returns the angle to rotate y label (radian).
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#width ⇒ Numeric
Returns the width.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#x_label ⇒ String
Returns the name of label placed along x-axis.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#xrange ⇒ Array<Numeric>, ...
Returns the name of width set.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#y_label ⇒ String
Returns the name of label placed along y-axis.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#yrange ⇒ Array<Numeric>, ...
Returns the name of width set.
37 |
# File 'lib/nyaplot/plot.rb', line 37 define_properties(:diagrams, :filter) |
#zoom ⇒ Boolean
Returns 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
56 57 58 59 60 61 |
# File 'lib/nyaplot/plot.rb', line 56 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
70 71 72 73 74 75 |
# File 'lib/nyaplot/plot.rb', line 70 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_json ⇒ Object
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 131 132 133 |
# File 'lib/nyaplot/plot.rb', line 103 def before_to_json diagrams = get_property(:diagrams) return if diagrams.length == 0 # set default values when not specified by users zoom(true) if zoom.nil? && 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
142 143 144 |
# File 'lib/nyaplot/plot.rb', line 142 def configure(&block) self.instance_eval(&block) if block_given? end |
#df_list ⇒ Array<String>
Returns names of dataframe used by diagrams belog to this plot.
96 97 98 99 100 101 |
# File 'lib/nyaplot/plot.rb', line 96 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
89 90 91 92 93 |
# File 'lib/nyaplot/plot.rb', line 89 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 |