Skip to content

Commit 0d75e8e

Browse files
committed
Multiple charts in a row
Added docs
1 parent 3a36663 commit 0d75e8e

File tree

5 files changed

+1036
-11
lines changed

5 files changed

+1036
-11
lines changed

lib/daru/view/adapters/googlecharts.rb

+21-7
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,28 @@ module GooglechartsAdapter
5757
# data << query
5858
# options = {type: :area}
5959
# chart = Daru::View::Plot.new(data, options)
60+
#
61+
# @example Multiple Charts in a row
62+
# Draw the Daru::View::Plot object with the data as an array of
63+
# Daru::View::Plots(s) or Daru::View::Table(s) or both
64+
# combined = Daru::View::Plot([line_chart, bar_chart])
6065
def init(data=[], options={})
61-
@table = GoogleVisualr::DataTable.new
62-
@table = get_table(data) unless data.is_a?(String)
63-
validate_url(data) if data.is_a?(String)
64-
@chart_type = extract_chart_type(options)
65-
@chart = GoogleVisualr::Interactive.const_get(
66-
@chart_type
67-
).new(@table, options)
66+
# When multiple charts are shown in a row, @chart will contain the
67+
# instance of GoogleVisular::BaseChart so that its data can contain
68+
# the array of plots (this will be used in display.rb). Further,
69+
# @chart will be used to call show_in_iruby and to_html.
70+
if data.is_a?(Array) &&
71+
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
72+
@chart = GoogleVisualr::BaseChart.new(GoogleVisualr::DataTable.new)
73+
else
74+
@table = GoogleVisualr::DataTable.new
75+
@table = get_table(data)
76+
validate_url(data) if data.is_a?(String)
77+
@chart_type = extract_chart_type(options)
78+
@chart = GoogleVisualr::Interactive.const_get(
79+
@chart_type
80+
).new(@table, options)
81+
end
6882
@chart.data = data
6983
@chart
7084
end

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

+47-4
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,61 @@ def get_html_spreadsheet(data, dom)
7575
end
7676

7777
def to_html(id=nil, options={})
78+
if data.is_a?(Array) &&
79+
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
80+
id, chart_script, path = extract_multiple_charts_id_script_path
81+
else
82+
id, chart_script, path = extract_chart_id_script_path(id)
83+
end
84+
template = File.read(path)
85+
ERB.new(template).result(binding)
86+
end
87+
88+
def show_in_iruby(dom=SecureRandom.uuid)
89+
IRuby.html(to_html(dom))
90+
end
91+
92+
# @return [Array] Array of IDs of the multiple charts, Array of scripts
93+
# of the multiple charts, path of the template used to render the
94+
# multiple charts
95+
def extract_multiple_charts_id_script_path
96+
path = File.expand_path(
97+
'../../templates/googlecharts/multiple_charts_div.erb', __dir__
98+
)
99+
id = []
100+
chart_script = []
101+
data.each_with_index do |plot, index|
102+
id[index] ||= SecureRandom.uuid
103+
chart_script[index] = set_chart_script(plot, id[index])
104+
end
105+
[id, chart_script, path]
106+
end
107+
108+
# @param id [String] The ID of the DIV element that the Google Chart
109+
# should be rendered in
110+
# @return [Array] ID of the div element, script of the chart, path of
111+
# the template which will be used to render the chart
112+
def extract_chart_id_script_path(id=nil)
78113
path = File.expand_path(
79114
'../../templates/googlecharts/chart_div.erb', __dir__
80115
)
81-
template = File.read(path)
82116
id ||= SecureRandom.uuid
83117
@html_id = id
84118
chart_script = show_script(id, script_tag: false)
85-
ERB.new(template).result(binding)
119+
[id, chart_script, path]
86120
end
87121

88-
def show_in_iruby(dom=SecureRandom.uuid)
89-
IRuby.html(to_html(dom))
122+
# @param plot [Daru::View::Plot, Daru::View::Table] one of the plot or
123+
# table objects that will be shown in a row
124+
# @param id [String] The ID of the DIV element that the Google Chart
125+
# should be rendered in
126+
# @return [String] Javascript of the table or chart
127+
def set_chart_script(plot, id)
128+
if plot.is_a?(Daru::View::Plot)
129+
plot.chart.show_script(id, script_tag: false)
130+
else
131+
plot.table.show_script(id, script_tag: false)
132+
end
90133
end
91134
end
92135

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>

spec/adapters/googlecharts/display_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
new(data_table.table, area_chart_options)}
3030
let(:column_chart_chart) {Daru::View::Plot.
3131
new(data_table.table, column_chart_options)}
32+
let(:combined) {
33+
Daru::View::Plot.new([data_table, area_chart_chart, column_chart_chart])
34+
}
3235

3336
describe "#to_html" do
3437
it "generates valid JS of the Area Chart" do

0 commit comments

Comments
 (0)