Skip to content

Commit 3069da9

Browse files
committed
Merge #8
2 parents 394ccc7 + 2c16e61 commit 3069da9

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

lib/nyaplot/core.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ def self.extension_lists
1010
@@extension_lists
1111
end
1212

13-
# Enable to show plots on IRuby notebook
14-
# @raise [RuntimeError] Raise error when being ran environment except IRuby notebook.
15-
def self.init_iruby
16-
raise "IRuby notebook is not loaded." unless defined? IRuby
17-
18-
path = File.expand_path("../templates/init.html.erb", __FILE__)
19-
template = File.read(path)
20-
dep_libraries = @@dep_libraries
21-
additional_libraries = @@additional_libraries
22-
html = ERB.new(template).result(binding)
23-
return IRuby.html(html)
24-
end
25-
2613
# Tell JavaScript back-end library to load some extension libraries
2714
# @param [String] name The name of JavaScript extension library to load
2815
def self.add_extension(name)
@@ -34,8 +21,15 @@ def self.add_dependency(name, url)
3421
@@dep_libraries[name]=url;
3522
end
3623

37-
# Load extension library to IRuby notebook before Nyaplotjs is loaded
38-
def self.add_additional_library(name, url)
39-
@@additional_libraries[name]=url
24+
# Enable to show plots on IRuby notebook
25+
def self.init_iruby
26+
path = File.expand_path("../templates/init.js.erb", __FILE__)
27+
template = File.read(path)
28+
dep_libraries = @@dep_libraries
29+
additional_libraries = @@additional_libraries
30+
js = ERB.new(template).result(binding)
31+
IRuby.display(IRuby.javascript(js))
4032
end
33+
34+
init_iruby if defined? IRuby
4135
end

lib/nyaplot/frame.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ def export_html
3333
template = File.read(path)
3434
model = self.to_json
3535
html = ERB.new(template).result(binding)
36-
return html
36+
html
3737
end
3838

39-
# show plot on IRuby notebook
40-
def show
39+
# show plot automatically on IRuby notebook
40+
def to_iruby
4141
path = File.expand_path("../templates/iruby.erb", __FILE__)
4242
template = File.read(path)
4343
id = SecureRandom.uuid()
4444
model = self.to_json
45-
html = ERB.new(template).result(binding)
46-
return IRuby.html(html)
45+
['text/html', ERB.new(template).result(binding)]
46+
end
47+
48+
# show plot on IRuby notebook
49+
def show
50+
IRuby.display(self)
4751
end
4852

4953
def before_to_json

lib/nyaplot/plot.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ def add_with_df(df, type, *labels)
7171
return diagram
7272
end
7373

74+
75+
# Show plot automatically on IRuby notebook
76+
def to_iruby
77+
Frame.new.tap {|f| f.add(self) }.to_iruby
78+
end
79+
7480
# Show plot on IRuby notebook
7581
def show
76-
frame = Frame.new
77-
frame.add(self)
78-
frame.show
82+
Frame.new.tap {|f| f.add(self) }.show
7983
end
8084

8185
# @return [Array<String>] names of dataframe used by diagrams belog to this plot

lib/nyaplot/templates/init.html.erb renamed to lib/nyaplot/templates/init.js.erb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<script>
21
if(window['d3'] === undefined ||
32
window['Nyaplot'] === undefined){
43
var path = <%= dep_libraries.merge(additional_libraries).to_json %>;
@@ -46,4 +45,3 @@ end
4645
%>
4746
<%= str %>
4847
}
49-
</script>

lib/nyaplot/templates/iruby.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
if(window['Nyaplot']==undefined){
99
window.addEventListener('load_nyaplot', render, false);
1010
return;
11+
} else {
12+
render();
1113
}
12-
render();
1314
})();
1415
</script>

0 commit comments

Comments
 (0)