Module: Daru::Plotly::DataFrame

Extended by:
Daru::Plotly
Defined in:
lib/daru/plotly/dataframe.rb

Overview

exports #plot and #generate_data for DataFrame

Constant Summary

Constants included from Daru::Plotly

VERSION

Class Method Summary collapse

Methods included from Daru::Plotly

default_layout, extract_mode_string, extract_type, plot

Class Method Details

.generate_data(df, opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/daru/plotly/dataframe.rb', line 11

def generate_data(df, opts)
  case extract_type(opts[:type])
  when :pie
    generate_data_for_pie(df, opts)
  when :heatmap
    generate_data_for_heatmap(df, opts)
  else
    generate_data_for_scatter_and_bar(df, opts)
  end.map { |trace| trace.merge(opts[:opts] || {}) }
end

.generate_data_for_heatmap(df, _opts) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/daru/plotly/dataframe.rb', line 34

def generate_data_for_heatmap(df, _opts)
  [
    {
      z: df.data.map(&:to_a).transpose,
      x: df.vectors.to_a,
      y: df.index.to_a,
      type: :heatmap
    }
  ]
end

.generate_data_for_pie(df, opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/daru/plotly/dataframe.rb', line 22

def generate_data_for_pie(df, opts)
  labels = df[opts[:labels] || :labels].to_a
  values = df[opts[:values] || :values].to_a
  [
    {
      labels: labels,
      values: values,
      type: :pie
    }
  ]
end

.generate_data_for_scatter_and_bar(df, opts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/daru/plotly/dataframe.rb', line 45

def generate_data_for_scatter_and_bar(df, opts)
  x = df[opts[:x] || :x].to_a
  mode = extract_mode_string(opts[:mode])
  Array(opts[:y] || :y).map do |vector_name|
    {
      x: x, y: df[vector_name].to_a,
      type: opts[:type],
      mode: mode,
      name: vector_name
    }
  end
end

.supported_typesObject



58
59
60
# File 'lib/daru/plotly/dataframe.rb', line 58

def supported_types
  %i[scatter bar pie heatmap]
end