Class: Charty::Backends::PlotlyHelpers::HtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/backends/plotly_helpers/html_renderer.rb

Direct Known Subclasses

NotebookRenderer

Constant Summary collapse

PLOTLY_URL =
"https://plot.ly".freeze
PLOTLY_LATEST_CDN_URL =
"https://cdn.plot.ly/plotly-latest.min.js".freeze
MATHJAX_CDN_URL =
("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js").freeze
DEFAULT_WIDTH =
"100%".freeze
DEFAULT_HEIGHT =
525

Instance Method Summary collapse

Constructor Details

#initialize(use_cdn: true, full_html: false, requirejs: true) ⇒ HtmlRenderer

Returns a new instance of HtmlRenderer.


9
10
11
12
13
14
15
# File 'lib/charty/backends/plotly_helpers/html_renderer.rb', line 9

def initialize(use_cdn: true,
               full_html: false,
               requirejs: true)
  @use_cdn = use_cdn
  @full_html = full_html
  @requirejs = requirejs
end

Instance Method Details

#render(figure, element_id: nil, post_script: nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/charty/backends/plotly_helpers/html_renderer.rb', line 24

def render(figure, element_id: nil, post_script: nil)
  element_id = SecureRandom.uuid if element_id.nil?
  plotly_html_div = build_plotly_html_div(figure, element_id, post_script)

  if @full_html
    <<~END_HTML % {div: plotly_html_div}
      <!DOCTYPE html>
      <html>
      <head><meta charset="utf-8" /></head>
      <body>
        %{div}
      </body>
      </html>
    END_HTML
  else
    plotly_html_div
  end
end