Module: Daru::Plotly::Initializer::Vector
- Defined in:
- lib/initializer/vector.rb
Class Method Summary collapse
- .extract_type(plot_type) ⇒ Object
- .generate_data(vector, opts) ⇒ Object
- .plot(vector, opts = {}) {|plot| ... } ⇒ Object
Class Method Details
.extract_type(plot_type) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/initializer/vector.rb', line 17 def self.extract_type(plot_type) supported_types = [:scatter, :bar, :histogram, :pie] case plot_type when *supported_types plot_type when nil :scatter else raise ArgumentError, "Type must be included in #{supported_types}." end end |
.generate_data(vector, opts) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/initializer/vector.rb', line 30 def self.generate_data(vector, opts) type = extract_type opts[:type] case type when :histogram [{ x: vector.to_a, type: :histogram }.merge(opts[:opts] || {})] when :scatter, :bar [{ x: vector.index.to_a, y: vector.to_a, type: type, mode: (opts[:mode]&.join('+')&.to_sym || :markers) }.merge(opts[:opts] || {})] when :pie [{ labels: vector.index.to_a, values: vector.to_a, type: :pie }.merge(opts[:opts] || {})] end end |
.plot(vector, opts = {}) {|plot| ... } ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/initializer/vector.rb', line 7 def self.plot(vector, opts={}) raise ArgumentError, "first argument should be Daru::Vector, not #{vector.class}." unless vector.is_a?(Daru::Vector) data = generate_data(vector, opts) layout = { width: (opts[:width] || 1000), height: (opts[:height] || 500) }.merge(opts[:layout] || {}) plot = Plotly::Plot.new(data: data, layout: layout) yield plot if block_given? plot end |