-
Notifications
You must be signed in to change notification settings - Fork 19
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
Multiple charts in a row #97
Changes from all commits
0d75e8e
e456894
a291110
759b218
132cbf6
3f2c9a2
3bf1399
6a2d31d
20dfdf4
2671cd7
7de361b
027b79d
5253623
3e18eef
0688924
4bbbddf
e58a7e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,6 @@ def to_html(placeholder=random_canvas_id) | |
|
||
def show_in_iruby(placeholder=random_canvas_id) | ||
# TODO : placeholder pass, in plot#div | ||
load_dependencies('iruby') | ||
IRuby.html to_html_iruby(placeholder) | ||
end | ||
|
||
|
@@ -89,6 +88,7 @@ def show_in_iruby(placeholder=random_canvas_id) | |
# `high_chart_iruby` which doesn't use `onload` in chart script. | ||
def to_html_iruby(placeholder=random_canvas_id) | ||
# TODO : placeholder pass, in plot#div | ||
load_dependencies('iruby') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay in this case we have to load it during the generation of body html code. |
||
chart_hash_must_be_present | ||
script = high_chart_css(placeholder) | ||
script << high_chart_iruby(extract_chart_class, placeholder, self) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
require 'erb' | ||
require 'daru/view/adapters/highcharts/display' | ||
require 'daru/view/adapters/nyaplot/display' | ||
require 'daru/view/adapters/googlecharts/display' | ||
|
||
# Otherwise Daru::IRuby module was used and IRuby.html method was not working. | ||
# Have disabled rubocop Style/Mixin for this in .rubocop.yml file | ||
include IRuby::Utils if defined?(IRuby) | ||
|
||
module Daru | ||
module View | ||
class PlotList | ||
attr_reader :data | ||
|
||
# @param data [Daru::View::Plot, Daru::View::Table] data to visualize | ||
# @return [void] initialize PlotList with data | ||
# @example | ||
# | ||
# df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]}) | ||
# plot1 = Daru::View::Plot.new( | ||
# df, type: :bar, x: :a, y: :b, adapter: :googlecharts | ||
# ) | ||
# plot2 = Daru::View::Plot.new( | ||
# df, chart: { type: 'line' }, adapter: :highcharts | ||
# ) | ||
# plots = Daru::View::PlotList.new([plot1, plot2]) | ||
# | ||
def initialize(data=[]) | ||
unless data.is_a?(Array) && data.all? { |plot| | ||
plot.is_a?(Daru::View::Plot) || | ||
plot.is_a?(Daru::View::Table) | ||
} | ||
raise ArgumentError, 'Invalid Argument Passed! Valid Arguments '\ | ||
'consists an Array of: Daru::View::Plot or '\ | ||
'Daru::View::Table (Right now, it is not '\ | ||
'implemented for DataTables)' | ||
end | ||
@data = data | ||
end | ||
|
||
# @return [void] display in IRuby notebook | ||
def show_in_iruby | ||
IRuby.html(div) | ||
end | ||
|
||
# @return [String] generates html code to include in body tag | ||
def div | ||
path = File.expand_path('templates/multiple_charts_div.erb', __dir__) | ||
template = File.read(path) | ||
charts_id_div_tag = [] | ||
charts_script = extract_charts_script(charts_id_div_tag) | ||
ERB.new(template).result(binding) | ||
end | ||
|
||
# @return [void] writes a html file to disk | ||
def export_html_file(path='./plot.html') | ||
path = File.expand_path(path, Dir.pwd) | ||
str = generate_html | ||
File.write(path, str) | ||
end | ||
|
||
private | ||
|
||
def extract_charts_script(charts_id_div_tag=[]) | ||
charts_script = '' | ||
@data.each do |plot| | ||
chart_script = extract_chart_script(plot) | ||
charts_id_div_tag << chart_script.partition(%r{<div(.*?)<\/div>}ixm)[1] | ||
chart_script.sub!(%r{<div(.*?)<\/div>}ixm, '') | ||
charts_script << chart_script | ||
end | ||
charts_script | ||
end | ||
|
||
def extract_chart_script(plot) | ||
# TODO: Implement this for datatables too | ||
return plot.div unless defined?(IRuby.html) && | ||
plot.is_a?(Daru::View::Plot) && | ||
plot.chart.is_a?(LazyHighCharts::HighChart) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
plot.chart.to_html_iruby | ||
end | ||
|
||
def generate_html | ||
path = File.expand_path( | ||
'templates/static_html_multiple_charts.erb', __dir__ | ||
) | ||
template = File.read(path) | ||
charts_script = div | ||
set_init_script = {} | ||
initial_script = '' | ||
@data.each do |plot| | ||
adapter = plot.adapter | ||
unless set_init_script[adapter] | ||
set_init_script[adapter] = true | ||
initial_script << plot.adapter.init_script | ||
end | ||
end | ||
ERB.new(template).result(binding) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<table> | ||
<tr> | ||
<% charts_id_div_tag.each do |chart_div_tag| %> | ||
<td><%= chart_div_tag %></td> | ||
<% end %> | ||
</tr> | ||
</table> | ||
<%= charts_script %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html lang='en'> | ||
<head> | ||
<!-- TODO: extend and display with table and chart both in html table --> | ||
<title>Multiple Charts</title> | ||
<%= initial_script %> | ||
</head> | ||
<body> | ||
<%= charts_script %> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated target ruby version to 2.2 in .rubocoop.yml file as rubocop was producing this error: