Class: Plotlyjs::Chart
- Inherits:
-
Object
- Object
- Plotlyjs::Chart
- Defined in:
- lib/plotlyjs/chart.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#element_id ⇒ Object
readonly
Returns the value of attribute element_id.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
Instance Method Summary collapse
-
#initialize(*ids, data:, layout:, config:) ⇒ Chart
constructor
A new instance of Chart.
- #to_html ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/plotlyjs/chart.rb', line 3 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/plotlyjs/chart.rb', line 3 def data @data end |
#element_id ⇒ Object (readonly)
Returns the value of attribute element_id.
3 4 5 |
# File 'lib/plotlyjs/chart.rb', line 3 def element_id @element_id end |
#layout ⇒ Object (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_html ⇒ Object
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" |