Skip to content

Commit 14efb07

Browse files
committed
Multiple charts in a row
1 parent 67c6df6 commit 14efb07

File tree

7 files changed

+1033
-20
lines changed

7 files changed

+1033
-20
lines changed

lib/daru/view/adapters/googlecharts.rb

+29-13
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@ module GooglechartsAdapter
1515
# and google_visualr : http://googlevisualr.herokuapp.com/
1616
#
1717
# TODO : this docs must be improved
18+
# @example Multiple Charts in a row
19+
# TODO: Do after spreadsheet PR.
1820
def init(data=[], options={})
19-
@table = GoogleVisualr::DataTable.new
20-
@table =
21-
if data.is_a?(Daru::View::Table) && data.table.is_a?(GoogleVisualr::DataTable)
22-
data.table
23-
elsif data.is_a?(GoogleVisualr::DataTable)
24-
data
25-
else
26-
add_data_in_table(data)
27-
end
28-
@chart_type = extract_chart_type(options)
29-
@chart = GoogleVisualr::Interactive.const_get(
30-
@chart_type
31-
).new(@table, options)
21+
# When multiple charts are shown in a row, @chart will contain the
22+
# instance of GoogleVisular::BaseChart so that its data can contain
23+
# the array of plots (this will be used in display.rb). Further,
24+
# @chart will be used to call show_in_iruby and to_html.
25+
if data.is_a?(Array) &&
26+
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
27+
@chart = GoogleVisualr::BaseChart.new(GoogleVisualr::DataTable.new)
28+
else
29+
@table = GoogleVisualr::DataTable.new
30+
@table = get_table(data)
31+
@chart_type = extract_chart_type(options)
32+
@chart = GoogleVisualr::Interactive.const_get(
33+
@chart_type
34+
).new(@table, options)
35+
end
36+
@chart.data = data
3237
@chart
3338
end
3439

@@ -47,10 +52,21 @@ def init_table(data=[], options={})
4752
# }
4853
# then directly DatTable is created using options. Use data=[] or nil
4954
@table = GoogleVisualr::DataTable.new(options)
55+
@table.data = data
5056
add_data_in_table(data)
5157
@table
5258
end
5359

60+
def get_table(data)
61+
if data.is_a?(Daru::View::Table) && data.table.is_a?(GoogleVisualr::DataTable)
62+
data.table
63+
elsif data.is_a?(GoogleVisualr::DataTable)
64+
data
65+
else
66+
add_data_in_table(data)
67+
end
68+
end
69+
5470
def init_script
5571
GoogleVisualr.init_script
5672
end

lib/daru/view/adapters/googlecharts/display.rb

+39-4
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,59 @@ def show_script(dom=SecureRandom.uuid, options={})
3636
end
3737

3838
def to_html(id=nil, options={})
39-
path = File.expand_path('../../templates/googlecharts/chart_div.erb', __dir__)
39+
if data.is_a?(Array) &&
40+
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
41+
id, chart_script, path = extract_multiple_charts_id_script_path
42+
else
43+
id, chart_script, path = extract_chart_id_script_path(id)
44+
end
4045
template = File.read(path)
41-
id ||= SecureRandom.uuid
42-
@html_id = id
43-
chart_script = show_script(id, script_tag: false)
4446
ERB.new(template).result(binding)
4547
end
4648

4749
def show_in_iruby(dom=SecureRandom.uuid)
4850
IRuby.html(to_html(dom))
4951
end
52+
53+
def extract_multiple_charts_id_script_path
54+
path = File.expand_path(
55+
'../../templates/googlecharts/multiple_charts_div.erb', __dir__
56+
)
57+
id = []
58+
chart_script = []
59+
data.each_with_index do |plot, index|
60+
id[index] ||= SecureRandom.uuid
61+
chart_script[index] = set_chart_script(plot, id[index])
62+
end
63+
[id, chart_script, path]
64+
end
65+
66+
def extract_chart_id_script_path(id=nil)
67+
path = File.expand_path(
68+
'../../templates/googlecharts/chart_div.erb', __dir__
69+
)
70+
id ||= SecureRandom.uuid
71+
@html_id = id
72+
chart_script = show_script(id, script_tag: false)
73+
[id, chart_script, path]
74+
end
75+
76+
def set_chart_script(plot, id)
77+
if plot.is_a?(Daru::View::Plot)
78+
plot.chart.show_script(id, script_tag: false)
79+
else
80+
plot.table.show_script(id, script_tag: false)
81+
end
82+
end
5083
end
5184

5285
class DataTable
86+
attr_accessor :data
5387
include Display
5488
end
5589

5690
class BaseChart
91+
attr_accessor :data
5792
include Display
5893
end
5994
end

lib/daru/view/adapters/nyaplot.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
require_relative 'nyaplot/iruby_notebook'
32
require 'daru'
43
require 'nyaplot'

lib/daru/view/plot.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
module Daru
32
module View
43
class Plot

lib/daru/view/table.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
module Daru
32
module View
43
class Table
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<table class='columns'>
2+
<tr>
3+
<% id.each do |div_id| %>
4+
<td><div id='<%= div_id %>'></div></td>
5+
<% end %>
6+
</tr>
7+
</table>
8+
<script>
9+
<% chart_script.each do |ch_script| %>
10+
<%= ch_script %>
11+
<% end %>
12+
</script>

0 commit comments

Comments
 (0)