Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defined show and show_in_iruby methods #236

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lazy_high_charts.gemspec
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3"

s.add_development_dependency "bundler", ">= 1.0"
s.add_development_dependency "iruby"
s.add_dependency "hash-deep-merge"

s.description = <<-DESC
Expand Down
1 change: 1 addition & 0 deletions lib/lazy_high_charts.rb
Expand Up @@ -5,6 +5,7 @@
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts options_key_filter])
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts layout_helper])
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts high_chart])
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts iruby_notebook])
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts high_chart_globals])
if defined?(::Rails::Railtie)
require File.join(File.dirname(__FILE__), *%w[lazy_high_charts railtie])
Expand Down
39 changes: 38 additions & 1 deletion lib/lazy_high_charts/high_chart.rb
Expand Up @@ -50,7 +50,7 @@ def method_missing(meth, opts = {})
end

# Add a simple series to the graph:
#
#
# data = [[0,5], [1,5], [2,5]]
# @high_chart.series :name=>'Updated', :data=>data
# @high_chart.series :name=>'Updated', :data=>[5, 1, 6, 1, 5, 4, 9]
Expand All @@ -74,6 +74,43 @@ def to_json
data
end

# @example
# chart = LazyHighCharts::HighChart.new('graph') do |f|
# f.title({ :text=>"Combination chart"})
# f.options[:xAxis][:categories] = ['Apples', 'Oranges', 'Pears', 'Bananas', 'Plums']
# f.labels(:items=>[:html=>"Total fruit consumption", :style=>{:left=>"40px", :top=>"8px", :color=>"black"} ])
# f.series(:type=> 'column',:name=> 'Jane',:data=> [3, 2, 1, 3, 4])
# f.series(:type=> 'column',:name=> 'John',:data=> [2, 3, 5, 7, 6])
# f.series(:type=> 'column', :name=> 'Joe',:data=> [4, 3, 3, 9, 0])
# f.series(:type=> 'spline',:name=> 'Average', :data=> [3, 2.67, 3, 6.33, 3.33])
# f.series(:type=> 'pie',:name=> 'Total consumption',
# :data=> [
# {:name=> 'Jane', :y=> 13, :color=> 'red'},
# {:name=> 'John', :y=> 23,:color=> 'green'},
# {:name=> 'Joe', :y=> 19,:color=> 'blue'}
# ],
# :center=> [100, 80], :size=> 100, :showInLegend=> false)
# end
#
# To display the html code use `show`. To see the same in IRuby notebook
# use `show_in_iruby`.
# User can also use :
# `IRubyRuby.html chart.show` (or)
# `IRuby.html chart.show.to_s` (or)
# `IRuby.display chart.show, mime: 'text/html'`
# to get the same chart in IRuby notebook.
#
# chart.show
# chart.show_in_iruby
#
def show
high_chart(random_canvas_id, self)
end

def show_in_iruby(placeholder=random_canvas_id)
IRuby.html high_chart(placeholder, self).to_s
end

private

def random_canvas_id
Expand Down
57 changes: 57 additions & 0 deletions lib/lazy_high_charts/iruby_notebook.rb
@@ -0,0 +1,57 @@
module LazyHighCharts

@@dep_libraries = {
# Iruby notebook already uses Jquery
# jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
highcharts: 'http://code.highcharts.com/highcharts.js'
}
@@additional_libraries = {}

# Load extension library to IRuby notebook after LazyHighCharts js is loaded
def self.add_dependency(name, url)
@@dep_libraries[name]=url;
end

# Load extension library to IRuby notebook before LazyHighCharts js is loaded
def self.add_additional_library(name, url)
@@additional_libraries[name]=url
end

# generate initializing code
def self.generate_init_code(assets=:inline)
dep_libraries = @@dep_libraries
additional_libraries = @@additional_libraries
js_dir = File.expand_path("../../../vendor/assets/javascripts/highcharts", __FILE__)
case assets
when :cdn
path = File.expand_path("../../templates/init.cdn.js.erb", __FILE__)
when :inline
path = File.expand_path("../../templates/init.inline.js.erb", __FILE__)
end
template = File.read(path)
ERB.new(template).result(binding)
end

# Enable to show plots on IRuby notebook
def self.init_iruby
js = self.generate_init_code
IRuby.display(IRuby.javascript(js))
end

def self.load_notebook(assets=:inline)
init_code = generate_init_code
case assets
when :cdn
IRuby.display(IRuby.javascript(init_code))
when :inline
IRuby.display(IRuby.html(<<END_HTML))
<script type="application/javascript">
#{init_code}
</script>
END_HTML
end
true
end
self.init_iruby if defined? IRuby

end
47 changes: 47 additions & 0 deletions lib/templates/init.cdn.js.erb
@@ -0,0 +1,47 @@
if(window['jquery'] === undefined ||
window['highcharts'] === undefined){
var path = <%= dep_libraries.merge(additional_libraries).to_json %>;

<%
shim = dep_libraries.merge(additional_libraries).inject({}){|hash, (k, v)| hash[k]={exports: k};next hash}
%>

var shim = <%= shim.to_json %>;

require.config({paths: path, shim:shim});

<%
str=""
dep_libraries.each do |key, val|
str.concat("require(['%s'], function(%s){window['%s']=%s;console.log('finished loading %s');"% Array.new(5, key))
end
%>
<%= str %>

var script = jquen.select("head")
.append("script")
.attr("src", "http://code.highcharts.com/highcharts.js")
.attr("async", true);

script[0][0].onload = script[0][0].onreadystatechange = function(){
<%
str=""
additional_libraries.each do |key, val|
str.concat("require(['%s'], function(%s){window['%s']=%s;console.log('finished loading %s');"% Array.new(5, key))
end
%>
<%= str %>
var event = document.createEvent("HTMLEvents");
event.initEvent("load_highcharts",false,false);
window.dispatchEvent(event);
console.log('Finished loading highchartsjs');
<%=
str = Array.new(additional_libraries.length, "});").join("")
%>
};

<%
str = Array.new(dep_libraries.length, "});").join("")
%>
<%= str %>
}
17 changes: 17 additions & 0 deletions lib/templates/init.inline.js.erb
@@ -0,0 +1,17 @@
if (window['jquery'] === undefined || window['highcharts'] === undefined) {
<%
all_libraries = dep_libraries.merge(additional_libraries)
all_libraries.each do |name, path|
basename = File.basename(path)
contents = IO.read(File.join(js_dir, basename), mode: 'r:UTF-8')
%>
/* BEGIN <%= name %>.js */
<%= contents %>
/* END <%= name %>.js */
<% end %>

var event = document.createEvent("HTMLEvents");
event.initEvent("load_highcharts", false, false);
window.dispatchEvent(event);
console.log("Finish loading highchartsjs");
}