Skip to content

Commit

Permalink
Multiple charts in a row
Browse files Browse the repository at this point in the history
Added docs
  • Loading branch information
Prakriti-nith committed Jun 16, 2018
1 parent 3a36663 commit 0d75e8e
Show file tree
Hide file tree
Showing 5 changed files with 1,036 additions and 11 deletions.
28 changes: 21 additions & 7 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,28 @@ module GooglechartsAdapter
# data << query
# options = {type: :area}
# chart = Daru::View::Plot.new(data, options)
#
# @example Multiple Charts in a row
# Draw the Daru::View::Plot object with the data as an array of
# Daru::View::Plots(s) or Daru::View::Table(s) or both
# combined = Daru::View::Plot([line_chart, bar_chart])
def init(data=[], options={})
@table = GoogleVisualr::DataTable.new
@table = get_table(data) unless data.is_a?(String)
validate_url(data) if data.is_a?(String)
@chart_type = extract_chart_type(options)
@chart = GoogleVisualr::Interactive.const_get(
@chart_type
).new(@table, options)
# When multiple charts are shown in a row, @chart will contain the
# instance of GoogleVisular::BaseChart so that its data can contain
# the array of plots (this will be used in display.rb). Further,
# @chart will be used to call show_in_iruby and to_html.
if data.is_a?(Array) &&
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
@chart = GoogleVisualr::BaseChart.new(GoogleVisualr::DataTable.new)
else
@table = GoogleVisualr::DataTable.new
@table = get_table(data)
validate_url(data) if data.is_a?(String)
@chart_type = extract_chart_type(options)
@chart = GoogleVisualr::Interactive.const_get(
@chart_type
).new(@table, options)
end
@chart.data = data
@chart
end
Expand Down
51 changes: 47 additions & 4 deletions lib/daru/view/adapters/googlecharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,61 @@ def get_html_spreadsheet(data, dom)
end

def to_html(id=nil, options={})
if data.is_a?(Array) &&
(data[0].is_a?(Daru::View::Plot) || data[0].is_a?(Daru::View::Table))
id, chart_script, path = extract_multiple_charts_id_script_path
else
id, chart_script, path = extract_chart_id_script_path(id)
end
template = File.read(path)
ERB.new(template).result(binding)
end

def show_in_iruby(dom=SecureRandom.uuid)
IRuby.html(to_html(dom))
end

# @return [Array] Array of IDs of the multiple charts, Array of scripts
# of the multiple charts, path of the template used to render the
# multiple charts
def extract_multiple_charts_id_script_path
path = File.expand_path(
'../../templates/googlecharts/multiple_charts_div.erb', __dir__
)
id = []
chart_script = []
data.each_with_index do |plot, index|
id[index] ||= SecureRandom.uuid
chart_script[index] = set_chart_script(plot, id[index])
end
[id, chart_script, path]
end

# @param id [String] The ID of the DIV element that the Google Chart
# should be rendered in
# @return [Array] ID of the div element, script of the chart, path of
# the template which will be used to render the chart
def extract_chart_id_script_path(id=nil)
path = File.expand_path(
'../../templates/googlecharts/chart_div.erb', __dir__
)
template = File.read(path)
id ||= SecureRandom.uuid
@html_id = id
chart_script = show_script(id, script_tag: false)
ERB.new(template).result(binding)
[id, chart_script, path]
end

def show_in_iruby(dom=SecureRandom.uuid)
IRuby.html(to_html(dom))
# @param plot [Daru::View::Plot, Daru::View::Table] one of the plot or
# table objects that will be shown in a row
# @param id [String] The ID of the DIV element that the Google Chart
# should be rendered in
# @return [String] Javascript of the table or chart
def set_chart_script(plot, id)
if plot.is_a?(Daru::View::Plot)
plot.chart.show_script(id, script_tag: false)
else
plot.table.show_script(id, script_tag: false)
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/daru/view/templates/googlecharts/multiple_charts_div.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table class='columns'>
<tr>
<% id.each do |div_id| %>
<td><div id='<%= div_id %>'></div></td>
<% end %>
</tr>
</table>
<script>
<% chart_script.each do |ch_script| %>
<%= ch_script %>
<% end %>
</script>
3 changes: 3 additions & 0 deletions spec/adapters/googlecharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
new(data_table.table, area_chart_options)}
let(:column_chart_chart) {Daru::View::Plot.
new(data_table.table, column_chart_options)}
let(:combined) {
Daru::View::Plot.new([data_table, area_chart_chart, column_chart_chart])
}

describe "#to_html" do
it "generates valid JS of the Area Chart" do
Expand Down
Loading

0 comments on commit 0d75e8e

Please sign in to comment.