Module: Nyaplot::Exportable

Included in:
Plot
Defined in:
lib/nyaplot/exportable.rb

Overview

Base class for Plot. Export methods are implemented here. !! Classes to include PlotBase should have #to_json. !!

Constant Summary collapse

PATH_NYAPLOT =
"https://cdn.rawgit.com/domitry/Nyaplotjs/bb48705474f7878c0a83f267675e019658421b52/release/nyaplot"

Instance Method Summary collapse

Instance Method Details

#export_html(path = "./plot.html", to_png = false) ⇒ Object

export static html file



24
25
26
27
28
29
30
31
# File 'lib/nyaplot/exportable.rb', line 24

def export_html(path="./plot.html", to_png=false)
  path = File.expand_path(path, Dir::pwd)
  body = generate_html("../templates/iruby.erb")
  temp_path = File.expand_path("../templates/static_html.erb", __FILE__)
  template = File.read(temp_path)
  num = File.write(path, ERB.new(template).result(binding))
  "Plot was saved to " + path
end

#generate_html(temp_path) ⇒ String

generate static html file

Returns:

  • (String)

    generated html



14
15
16
17
18
19
20
21
# File 'lib/nyaplot/exportable.rb', line 14

def generate_html(temp_path)
  path = File.expand_path(temp_path, __FILE__)
  url = Nyaplot.get_url
  id = SecureRandom.uuid
  model = to_json
  template = File.read(path)
  ERB.new(template).result(binding)
end

#raise_display_failedObject



8
9
10
# File 'lib/nyaplot/exportable.rb', line 8

def raise_display_failed
  raise "This method works only on IRuby. Use #export_html or install IRuby."
end

#showObject

show plot on IRuby notebook



47
48
49
50
# File 'lib/nyaplot/exportable.rb', line 47

def show
  raise_display_failed unless defined? IRuby
  IRuby.display(self)
end

#to_irubyObject

show plot automatically on IRuby notebook



40
41
42
43
44
# File 'lib/nyaplot/exportable.rb', line 40

def to_iruby
  raise_display_failed unless defined? IRuby
  html = generate_html("../templates/iruby.erb")
  ['text/html', html]
end

#to_pngObject



33
34
35
36
37
# File 'lib/nyaplot/exportable.rb', line 33

def to_png
  raise_display_failed unless defined? IRuby
  html = generate_html("../templates/to_png.erb")
  IRuby.display html, mime: 'text/html'
end