Class: Plotlyjs::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/plotlyjs/chart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*ids, data:, layout:, config:) ⇒ Chart

Returns a new instance of Chart.



5
6
7
8
9
10
11
# File 'lib/plotlyjs/chart.rb', line 5

def initialize(*ids, data:, layout:, config:)
  @element_id = "plotlyjs-ruby-#{ids.join("_")}"

  @data = data
  @layout = layout
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/plotlyjs/chart.rb', line 3

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/plotlyjs/chart.rb', line 3

def data
  @data
end

#element_idObject (readonly)

Returns the value of attribute element_id.



3
4
5
# File 'lib/plotlyjs/chart.rb', line 3

def element_id
  @element_id
end

#layoutObject (readonly)

Returns the value of attribute layout.



3
4
5
# File 'lib/plotlyjs/chart.rb', line 3

def layout
  @layout
end

Instance Method Details

#to_htmlObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plotlyjs/chart.rb', line 13

def to_html
  js_vars = {
    data: data.to_json,
    layout: layout.to_json,
    config: config.to_json
  }

  createjs = "Plotly.newPlot('#{element_id}', %{data}, %{layout}, %{config});" % js_vars

  html = "    <div id=\"\#{element_id}\"></div>\n    <script>\n      (function() {\n        var createChart = function() { \#{createjs} };\n\n        if (\"Plotly\" in window) {\n          createChart();\n        } else {\n          window.addEventListener('load', createChart);\n        };\n      })();\n    </script>\n  HTML\n\n  html.respond_to?(:html_safe) ? html.html_safe : html\nend\n"