Module: Daru::Plotly::Initializer::DataFrame
- Defined in:
- lib/initializer/dataframe.rb
Class Method Summary collapse
- .extract_type(plot_type) ⇒ Object
- .generate_data(df, opts) ⇒ Object
- .plot(df, opts = {}) {|plot| ... } ⇒ Object
Class Method Details
.extract_type(plot_type) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/initializer/dataframe.rb', line 17 def self.extract_type(plot_type) supported_types = [:scatter, :bar, :pie, :heatmap] case plot_type when *supported_types plot_type when :histogram raise ArgumentError, 'You can plot histogram only with Daru::Vector, not Daru::DataFrame.' when nil :scatter else raise ArgumentError, "Type must be included in #{supported_types}" end end |
.generate_data(df, opts) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/initializer/dataframe.rb', line 32 def self.generate_data(df, opts) case type = extract_type(opts[:type]) when :pie labels = df[opts[:labels] || :labels].to_a values = df[opts[:values] || :values].to_a [{ labels: labels, values: values, type: :pie }.merge(opts[:opts] || {})] when :heatmap [{ z: df.data.map { |vector| vector.to_a }.transpose, x: df.vectors.to_a, y: df.index.to_a, type: type }] else x = df[opts[:x] || :x].to_a mode = (Array(opts[:mode]) || [:markers]).map(&:to_s).join('+') Array(opts[:y] || :y).map do |vector_name| vector = df[vector_name] { x: x, y: vector.to_a, type: type, mode: mode, name: vector_name }.merge(opts[:opts] || {}) end end end |
.plot(df, opts = {}) {|plot| ... } ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/initializer/dataframe.rb', line 7 def self.plot(df, opts={}) raise ArgumentError, "first argument should be Daru::DataFrame, not #{df.class}." unless df.is_a?(Daru::DataFrame) data = opts[:data] || generate_data(df, 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 |